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 不是哦，我采纳的那位方法 是对的
2 年多之前 回复
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