java中怎么求10到n 中各位数相等的数

qq_34652652 2016-04-19 07:46:16
10-n中 例如11 222 44444 555555 777777 。不能使用嵌套循环,只能只用,最基础的语法。,求解
...全文
285 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
huntor 2016-04-20
  • 打赏
  • 举报
回复
引用 11 楼 qq_34652652 的回复:
[quote=引用 9 楼 x060508 的回复:] [quote=引用 2 楼 huntor 的回复:]
    public static void main(final String[] args){
        for(int i = 10; i < 1000; i++){
            char[] chars = Integer.toString(i).toCharArray();
            if(CharBuffer.wrap(chars).chars().distinct().count() == 1)
                System.out.printf("%d%n", i);
        }
    }
结果是:
11
22
33
44
55
66
77
88
99
111
222
333
444
555
666
777
888
999
CharBuffer.wrap(chars).chars().为什么CharBuffer 我找不到有chars这个方法?什么版本的[/quote] 我也是。。。[/quote] Java 8 中的 Stream
qq_34652652 2016-04-20
  • 打赏
  • 举报
回复
引用 9 楼 x060508 的回复:
[quote=引用 2 楼 huntor 的回复:]
    public static void main(final String[] args){
        for(int i = 10; i < 1000; i++){
            char[] chars = Integer.toString(i).toCharArray();
            if(CharBuffer.wrap(chars).chars().distinct().count() == 1)
                System.out.printf("%d%n", i);
        }
    }
结果是:
11
22
33
44
55
66
77
88
99
111
222
333
444
555
666
777
888
999
CharBuffer.wrap(chars).chars().为什么CharBuffer 我找不到有chars这个方法?什么版本的[/quote] 我也是。。。
奄灬苟且偷生 2016-04-20
  • 打赏
  • 举报
回复
我是来看看9级的大神的
x060508 2016-04-20
  • 打赏
  • 举报
回复
引用 2 楼 huntor 的回复:
    public static void main(final String[] args){
        for(int i = 10; i < 1000; i++){
            char[] chars = Integer.toString(i).toCharArray();
            if(CharBuffer.wrap(chars).chars().distinct().count() == 1)
                System.out.printf("%d%n", i);
        }
    }
结果是:
11
22
33
44
55
66
77
88
99
111
222
333
444
555
666
777
888
999
CharBuffer.wrap(chars).chars().为什么CharBuffer 我找不到有chars这个方法?什么版本的
家里敷泥呀 2016-04-20
  • 打赏
  • 举报
回复
引用 6 楼 Q80470101 的回复:
[quote=引用 2 楼 huntor 的回复:]
    public static void main(final String[] args){
        for(int i = 10; i < 1000; i++){
            char[] chars = Integer.toString(i).toCharArray();
            if(CharBuffer.wrap(chars).chars().distinct().count() == 1)
                System.out.printf("%d%n", i);
        }
    }
楼主要的只是11 222 44444 555555 777777 这几个数,各位相等,且各位上的值等于总位数。 不过,你这个用法的确很犀利。[/quote] 答完题我才发现看走眼了,楼主要的的确是2楼出的结果,sorry。
家里敷泥呀 2016-04-20
  • 打赏
  • 举报
回复
你就两个需求: 1)各位相同; 2)各位上的值等于总位数。 这事情再简单不过了。 假设是10到100000 1)判断总位数,100000是6位; 2)从2(10的位数)到6进行循环,分别有11,22,333,4444,55555,666666; 3)在循环里面增加一个判断,和最大值比较(因为确定最小值是10,所以不用比),如果大于最大值,剔除掉。可以剔除掉666666 4)得到的就是你要的结果。 只用了length(最大值)-1次循环,都是最基础的语法。
家里敷泥呀 2016-04-20
  • 打赏
  • 举报
回复
引用 2 楼 huntor 的回复:
    public static void main(final String[] args){
        for(int i = 10; i < 1000; i++){
            char[] chars = Integer.toString(i).toCharArray();
            if(CharBuffer.wrap(chars).chars().distinct().count() == 1)
                System.out.printf("%d%n", i);
        }
    }
楼主要的只是11 222 44444 555555 777777 这几个数,各位相等,且各位上的值等于总位数。 不过,你这个用法的确很犀利。
范海辛o 2016-04-20
  • 打赏
  • 举报
回复
2L 对 API 究竟是有多熟.
  • 打赏
  • 举报
回复
2楼的方法好奇特,学习了
qq_34652652 2016-04-20
  • 打赏
  • 举报
回复
引用 13 楼 qq_34652652 的回复:
package te;
import java.util.Scanner;
public class b {
	public static void main(String[] args) {		 
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int aa=11;
		for(int i=10;i<=n;i++){
			if(i>aa*9){
				aa=aa*10+1;
			}
		if(i%aa==0){
			System.out.println(i);
		}
	}
	}
}	
这个正解。。
if(i>aa*9){
qq_34652652 2016-04-20
  • 打赏
  • 举报
回复
package te;
import java.util.Scanner;
public class b {
	public static void main(String[] args) {		 
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int aa=11;
		for(int i=10;i<=n;i++){
			if(i>=aa*9){
				aa=aa*10+1;
			}
		if(i%aa==0){
			System.out.println(i);
		}
	}
	}
}	
这个正解。。
冰心的小屋 2016-04-19
  • 打赏
  • 举报
回复
    public static void main(String[] args) throws Exception {
        Map<Integer, Integer> map = new HashedMap();
        for (int i = 10; i < 1000; i++) {
            Integer key = String.valueOf(i).length();
            Integer divisor = null;
            if(map.containsKey(key)){
                divisor = map.get(key);
            }else{
                char[]  data = new char[key];
                Arrays.fill(data, '1');
                divisor = Integer.parseInt(new String(data));
                map.put(key, divisor);
            }

            if(i % divisor == 0){
                System.out.println(i);
            }

        }

    }
欢迎光临我的博客 http://happyshome.cn
huntor 2016-04-19
  • 打赏
  • 举报
回复
    public static void main(final String[] args){
        for(int i = 10; i < 1000; i++){
            char[] chars = Integer.toString(i).toCharArray();
            if(CharBuffer.wrap(chars).chars().distinct().count() == 1)
                System.out.printf("%d%n", i);
        }
    }
结果是:
11
22
33
44
55
66
77
88
99
111
222
333
444
555
666
777
888
999
Coder_D 2016-04-19
  • 打赏
  • 举报
回复
先判断这是几位数,记为n,再去判断能否被n个1组成的那个数整除。

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧