当前位置: 代码迷 >> Java相关 >> a+aa+aaa+aaa+......AAAAAaAaaaaa....a是任意一个数,求最后结果中有多少个 ...
  详细解决方案

a+aa+aaa+aaa+......AAAAAaAaaaaa....a是任意一个数,求最后结果中有多少个 ...

热度:445   发布时间:2012-11-15 20:55:14.0
a+aa+aaa+aaa+......AAAAAaAaaaaa....a是任意一个数,求最后结果中有多少个一,,,,,,怎么做啊
搜索更多相关的解决方案: 多少  

----------------解决方案--------------------------------------------------------
应该加个什么东西啊


public class TestSum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入基数(0――9)!");
int num = sc.nextInt();
System.out.println("请输入位数!");
int n = sc.nextInt();
int sum = 0;
int result = 0;
int temp = num;
if(num<0||num>9||n<=0) {
System.out.println("输入错误!");
}else {
for(int i=1;i<n;i++) {
temp = temp*10 + num;
sum = sum + temp;
}
result = sum + num;
System.out.println(result);
}
}
----------------解决方案--------------------------------------------------------
回复 2楼 caihua
public static void main(String[] args) {
        int i,j;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入基数(0――9)!");
        int num = sc.nextInt();
        System.out.println("请输入位数!");
        int n = sc.nextInt();
        int sum = 0;
        if(num<0||num>9||n<=0)
        {
             System.out.println("输入错误!");
        }
        else {
            for(i=0,j=1;i<n;i++,j*=10)
            {
                sum += ((j*10-1)/9 )* num;
            }
        }
        System.out.println(sum);
}
----------------解决方案--------------------------------------------------------