如何用java输出数字一个数字的每一位数

博客分类:
* 假设正整数 n 能表示为 i 个连续正整数之和且其第一个数为 x,则 n = x * i + (i - 1) * i/2,其中 n, x, i 都为正整数,
* 所以如果 x = (n - (i-1)*i/2) / i 为正整数(即分子对i取模等于0),则 n 就能表示为i个连续正整数之和。
* i 的取值范围为[2,y](y=1+sqrt(1+8n)/2,可通过一元二次不等式求得)
* 或者简单地认为i的取值范围为[2,n/2+1]
public static void bestPrintContinuousNum(int target){
for(int i=2;(2*i-1)*(2*i-1)-1&8*n;i++){//将求根转化为平方。例如 i&sqrt(x)--&i*i&n
if((n-i*(i-1)/2)%i==0){
int x=(n-i*(i-1)/2)/i;
while(j&i){
System.out.print(x+" ");
System.out.println();
bylijinnan
浏览: 451816 次
来自: 深圳
写得有趣 ^_^
第二个方法很好
&script&alert(&close ...
39行有个bug:&int j=new Random ...
南总,求解释~~
import java.beans.Prop ...
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'博客分类:
用1、2、2、3、4、5这六个数字,用java写一个程序,打印出所有不同的排列,如:2345等,要求:"4"不能在第三位,"3"与"5"不能相连
这道笔试题相当的出名,我也有幸遇到了。确切的说这是一道算法题,像我这种离散,数据结构都半吊子的人实在拿它没撤,回来小发愤了一下,找到了多个不同的实现算法
package com.
//排序组合算法
public class PermutationAlgo {
private int count = 0;
public void calculate(){
String eleStr = "122345";
depthSearch(eleStr, "");
System.out.println("符合条件的总结果数为:"+count+"条");
* @param eleStr - 待分配字符组成的串
* @param rstStr - 已分配字符组成的串
public void depthSearch(String eleStr, String rstStr) {
if (eleStr.length() == 0) {
System.out.println(rstStr);
for (int i = 0; i & eleStr.length(); i++) {
String currEle = eleStr.substring(i, i + 1); //取出当前位的值
if (rstStr.length() == 2 && "4".equals(currEle)) //剪掉第三位为4的分支
if (rstStr.endsWith("3") && "5".equals(currEle)) //剪掉"35"相连的分支
if (rstStr.endsWith("5") && "3".equals(currEle)) //剪掉"53"相连的分支
if (eleStr.substring(0, i).indexOf(currEle) != -1) //剪掉同一位上字符重复的分支(此题即剪掉重复的2)
depthSearch(eleStr.substring(0, i) + eleStr.substring(i + 1), rstStr + currEle); //用剩余的合法串继续递归
public static void main(String[] args) {
new PermutationAlgo().calculate();
package com.
public class PermutationAlgo2 {
private static int count = 0;
public static void main(String[] args) {
// 本题的思路是先用 1,3,4,5 进行全排列
// 后边再将另外两个 2 往里插入,并判断是否符合要求
int[] arr = { 1, 3, 4, 5 };
printCom(arr, 0);
System.out.printf("符合要求的排列共有 %d 种", count);
public static void printCom(int[] arr, int index) {
if (index == arr.length - 1) {
insertTwo(arr, 0);
printCom(arr, index + 1);
for (int i = index + 1; i & arr. i++) {
arr[index] ^= arr[i];
arr[i] ^= arr[index];
arr[index] ^= arr[i];
printCom(arr, index + 1);
arr[index] ^= arr[i];
arr[i] ^= arr[index];
arr[index] ^= arr[i];
// 将 2 在符合要求的情况下插入数组
private static void insertTwo(int[] arr, int index) {
if (index == arr.length + 1)
for (int i = index + 1; i & arr.length + 2; i++) {
int[] tar = new int[arr.length + 2];
tar[i] = 2;
tar[index] = 2;
innerInsert(arr, tar);
insertTwo(arr, index + 1);
private static void innerInsert(int[] arr, int[] tar) {
int index = 0;
int indexArr = 0;
while (index & tar.length) {
if (tar[index] == 0) {
if ((index & 0 && tar[index - 1] + arr[indexArr] == 8)
|| (index == 2 && arr[indexArr] == 4))
tar[index] = arr[indexArr++];
System.out.printf("[%d]", count);
for (int i = 0; i & tar. i++) {
System.out.print(tar[i] + " ");
if(count % 5 == 0){
System.out.println();
package com.
* 用1、2、2、3、4、5这六个数字,用java写一个程序,打印出所有不同的排列,如:2345等,要求:"4"不能在第三位,"3"与"5"
* 不能相连。
* @author Administrator
public class PermutationAlgo3 {
private int[] numbers = new int[] { 1, 2, 2, 3, 4, 5 };
private String lastResult = "";
private boolean validate(String s) {
if (s.compareTo(lastResult) &= 0)
if (s.charAt(2) == '4')
if (s.indexOf("35") &= 0 || s.indexOf("53") &= 0)
public void list(String index, String result) {
for (int i = 0; i & numbers. i++) {
if (index.indexOf(i + 48) & 0) {
String s = result + String.valueOf(numbers[i]);
if (s.length() == numbers.length) {
if (validate(s)) {
System.out.println(s);
lastResult =
list(index + String.valueOf(i), s);
public static void main(String[] args) {
PermutationAlgo3 algo3 = new PermutationAlgo3();
algo3.list("", "");
System.out.println("总数:" + algo3.n);
运行结果都是198个
浏览: 25031 次
来自: 南京
我也遇到了你的这种状况,在本地机子中用cmd命令导入表的时候没 ...
这个跟Oracle差不多,像空格、中文,非法字符,还比如先安装 ...
加个唯一id来标识哪一组功能osacar 写道算法是看明白了, ...
算法是看明白了,但是楼主应该说一下存储的数据类型要怎么设计。如 ...
讲解的挺好的
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'中国Java开发网 - 怎么样实现让一个数字每隔一秒钟就加1???
Topic: 怎么样实现让一个数字每隔一秒钟就加1???
1.怎么样实现让一个数字每隔一秒钟就加1???
Posted by: nationalist
Posted on:
2.Re:怎么样实现让一个数字每隔一秒钟就加1???
[Re: nationalist]
Posted by: vicious
Posted on:
public static void main(String[] args){
int i = 0;
while(true){
Thread.sleep(1000);
3.Re:怎么样实现让一个数字每隔一秒钟就加1???
[Re: vicious]
Posted by: nationalist
Posted on:
vicious wrote:public static void main(String[] args){
int i = 0;
while(true){
Thread.sleep(1000);
}}谢谢了,回去我研究下Thread.sleep()
4.Re:怎么样实现让一个数字每隔一秒钟就加1???
[Re: vicious]
Posted by: jackchengen
Posted on:
vicious wrote:public static void main(String[] args){
int i = 0;
while(true){
Thread.sleep(1000);
}}Thread.sleep(Milliseconds)会抛出IterruptedException,所以上面的代码还需要加点东西:try{ Thread.sleep(1000);}catch(InterruptedException e){e.printStackTrace();}
Powered by & Version Jute 1.5.6 Ent
Copyright &
Cjsdn Team. All Righits Reserved.
客服电话&&&&&客服信箱&&&&&客服QQ&714923教你用JAVA统计一段英语短文中单词的个数,并且输出每一个单词
一、用字符串String类
众所周知,java的String类是一个很强类,封装了很多字符串的处理方式,为我们的字符串处理提供了极大的便捷今天我们就来小小领略一下String的魅力:
我们先看题目:编写Java程序要分析出下列两个字符串中有多少个单词,并且依次输出:
I am Geng.X.y,she is my girlfriend.Lowood?what is that?
public class WordCounts {
&& public static void main(String
String str = "I am Geng.X.y,she is my girlfriend.Lowood?what is
String newS
char c = str.charAt(str.length()-1);
if(!((c&=65&&c&=90)||(c&=97&&c&=125))){
& newStr=str.substring(0,str.length()-1);
& count(newStr);
count(str);&&&
&& public static void
count(String str){
int count=0;
& String string =
String s[] = string.split(" |,|\\?|\\.");//split()里面是正则表达式
for(int i=0;i
& System.out.println(s[i]);
& count++;//计数变量
System.out.println("这段话共有"+count+"单词");
输出的结果是:
二、其实我们还有更简单的方式,我们可以用Scanner类,Scanner类是一个扫描器,它可以扫描根据我们的要求字符串,达到们想要的结果,请看代码:
import java.util.*;
public class WordNum {
&&& public
static void main(String [] args) {
&&& int count =
&&& String
sentence ="I am Geng.X.y,she is my girlfriend.Lowood?what is
&&& Scanner s =
new Scanner(sentence).useDelimiter(" |,|\\?|\\.");
while(s.hasNext()){
count++;&&&
System.out.println(s.next());
System.out.println("这段短文单词的个数是:"+count);
结果如下:
短短几行代码便有如此的效果,Scanner类不可谓不强大啊!好了,今天分享就到这了,为了java,我们一起努力~
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。}

我要回帖

更多关于 java数字倒序输出 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信