zpc10cs 于 2015.05.26 17:14 提问

public class ClassMat26 {
/*
* 题目：求s=a+aa+aaa+aaaa+aa...a的值，其中a是一个数字。
* 例如2+22+222+2222+22222(此时共有5个数相加)，几个数相加有键盘控制。
*/

``````public static int Count;

int test(int a,int i) {
int aa;
for ( int j = 0; j <=i; j++) {
aa = a * 10 + a;
Count = a + aa;
}
System.out.println("Count="+Count);
return 0;
}

public static void main(String[] args) {
ClassMat26 cm = new ClassMat26();
cm.test(2,5);
}
``````

}

10个回答

Tiger_Zhao   2015.05.26 17:20

``````    int aa = 0;
Count = 0;
for ( int j = 0; j <i; j++) {
aa = aa * 10 + a;
Count += aa;
}
``````
ffjdd   2015.05.26 17:17

( int j = 0; j <=i; j++)

bdmh      2015.05.26 17:18

for循环里，应该是a*10的i次方才对吧

zpc10cs 不是哦，我采纳的那位方法 是对的

caozhy      2015.05.26 17:18
`````` int test(int a,int i) {
int sum = 0;
for(int x = 1; x <= i; i++)
{
int mul = 1;
for (int y = 1; y <=x; y++)
{
sum += a * mul;
mul *= 10;
}
}
System.out.println("Count="+sum);
return sum;
}
``````
caozhy      2015.05.26 17:24
``````/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
int test(int a,int i) {
if (i == 1) return a;
int sum = 0;
for(int x = 1; x <= i; x++)
{
int mul = 1;
for (int y = 1; y <= x; y++)
{
sum += a * mul;
mul *= 10;
}
}
return sum;
}
public static void main (String[] args) throws java.lang.Exception
{
Ideone cm = new Ideone();
System.out.println("Count="+cm.test(2,5));
}
}
``````
caozhy      2015.05.26 17:24

Count=24690
http://ideone.com/vQX2sy

LSL1618   2015.05.26 17:30

s=(ax10^0)+(ax10^1+ax10^0)+(ax10^2+ax10^1+ax10^0)+(ax10^3+ax10^2+ax10^1+ax10^0)+...+(ax10^(n-1)+ax10^(n-2)+...+ax10^0)

u014749886   2015.05.26 17:31

public static int Count;

int test(int a,int i) {
int aa;
count = a;
if(i>1)
{

for ( int j =2; j <=i; j++) {
aa = a * 10 + a;
Count += aa;
}
}
System.out.println("Count="+Count);
return 0;
}

public static void main(String[] args) {
ClassMat26 cm = new ClassMat26();
cm.test(2,5);
}

u012271952   2015.05.26 17:32

int aa = 0;
Count = 0;
for ( int j = 0; j <i; j++) {
aa = aa * 10 + a;
Count += aa;
}

u014749886   2015.05.26 17:32

#define _CRT_SECURE_NO_WARNINGS #include void main() { int a = 2, n = 3; int sum = 0; int m = 0; while (n--!=0) { //n=2 sum=2 a=22 n=1 sum=2+22 a= 222 n=0 sum=22+ 220 sum+=a; pr

import java.util.Scanner; public class SumDemo3 { public static void main(String[] args) { // 求2+22+222+2222+..... for (;;) { Scanner scanner = new Scanner(System.in); System.out.println("
java中求2+22+222+2222+22222.........

1.求2+22+222+2222+…+22…22(精确计算).
#include#define m 200main(){           long a[m],b,d;            int n,i,j,r;             printf("please input the number:");            scanf("%d",&n);            for(i=1;i         a[i]=0;

a+aa+aaa+...+(aaa...a)
1.问题描述    求Sn=a+aa+aaa+⋯+(aaa…a)S_n=a+aa+aaa+⋯+(aaa…a) n个a的值，其中a是一个数字，a和n都由键盘输入。例如，2+22+222+2222+22222（此时a=2，n=5）2.思路分析 首先，这个题目中Sn求和形式告诉我们，这道题一定要用到循环结构，用到循环结构就要定义循环变量，这道题我们定义循环变量为i以及循环退出的条件为i>n。 其次是当a=