求s=a+aa+aaa+aaaa+aa...a的值,

李云top 2011-06-04 08:33:42
菜鸟:请问各位给予思路,我不会做这道题目
题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
...全文
2788 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mourinho 2011-06-06
  • 打赏
  • 举报
回复

import java.util.Scanner;

public class Test {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please Enter two numbers:");//第一个为a,第二个为数量
int num = scanner.nextInt();
int length = scanner.nextInt();
getResult(num,length);
}

static void getResult(int a,int length){
int sum = 0,num = a;
for(int i = 0; i < length;i++){
sum += num;
num = num * 10 + a;
}
System.out.println("result = " + sum);
}
}


Please Enter two numbers:
2
5
result = 24690
TKD03072010 2011-06-05
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lpy3654321 的回复:]

这题用递归做是不是能更好点啊
[/Quote]
递归 代码简短 易理解
但效率不高
Zeus 2011-06-05
  • 打赏
  • 举报
回复
这题用递归做是不是能更好点啊
KPRF2009 2011-06-05
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 fkanf 的回复:]
以前做的:

Java code

/*【程序8】
*题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
*程序分析:关键是计算出每一项的值。
*/
import javax.swing.JOptionPane;
public class Prog……
[/Quote]

+1
凡心未灭 2011-06-05
  • 打赏
  • 举报
回复
以前做的:

/*【程序8】
*题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
*程序分析:关键是计算出每一项的值。
*/
import javax.swing.JOptionPane;
public class Program8{
public static void main(String[]args){
int a,numOfAddend,sum=0;
String str="";
a=Integer.parseInt(JOptionPane.showInputDialog("输入a"));
numOfAddend=Integer.parseInt(JOptionPane.showInputDialog("多少个数相加?"));

StringBuffer[] addend=new StringBuffer[numOfAddend];//一字符串形式储存加数
for(int i=0 ; i<addend.length;i++)//初始化
addend[i]=new StringBuffer();
for(int i=0;i<addend.length;i++)
for(int j=0;j<=i;j++)
addend[i].append(a);

//以字符串形式建立等式
for(int i=0;i<addend.length;i++){
if(i!=0)str+="+";
str+=addend[i].toString();
}

//计算等式左端
for(int i=0;i<addend.length;i++)
sum+=Integer.parseInt(addend[i].toString());

JOptionPane.showMessageDialog(null,str+"= "+sum);
}
}
TKD03072010 2011-06-05
  • 打赏
  • 举报
回复
楼主参考一下:

/*
* 算法: 定义一个变量b, 赋初值为0;定义一变量sum, 赋初值为0,
* 进入循环后,将a + b 的值赋给b,将sum + b 的值赋给sum;
* 同时,将a 增加十倍, ++ i; 继续循环;
* 循环结束后,输出sum 的值。
*/
package cn.com.flywater.FiftyAlgorthm;
import java.util.Scanner;
public class EightPlus {
static long a = 2, b = 0;
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int i = 0;
long sum = 0;
while(i < n) {
b = b + a;
sum = sum + b;
a = a * 10;
++ i;
}
System.out.println("input number: " + n);
System.out.println(sum);
}
}

龙四 2011-06-05
  • 打赏
  • 举报
回复
jdk1.6
package cal;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class Test {
/**
* 注意最大的加数不能超过Integer.MAX_VALUE
* @param count 加数的个数
* @param num 组成加数的数字
* @return 相加的结果
* @throws ScriptException
*/
public static Object calculate(int count, int num) throws ScriptException {
String temp = "aaaaaaaaaaaaaaaaaaaaaaaa".replaceAll("a", num + "");
if(count >= temp.length()) {
throw new IllegalArgumentException("最大值位数太多了,太多了,太多。。。");
}

StringBuilder builder = new StringBuilder("");
for(int i=1; i<=count; i++) {
builder.append(temp.substring(0,i+1) + "+");
}
builder.append("0");

String exp = builder.toString();
System.out.println(exp);
ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("js");
return scriptEngine.eval(exp);
}
public static void main(String[] args) {
try {
System.out.println(calculate(19, 9));
} catch (ScriptException e) {
e.printStackTrace();
}
}
}

Jeelon 2011-06-05
  • 打赏
  • 举报
回复
大数 最好不要用int 用bigInteger吧
meadow 2011-06-05
  • 打赏
  • 举报
回复
怎么不用c
仲少_帆 2011-06-05
  • 打赏
  • 举报
回复

public class a_aa_aaa {

/**
* 题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222=24690(
* 此时共有5个数相加), 几个数相加有键盘控制。
*/
public static void main(String[] args) {
System.out.println("请输入几个数相加");
Scanner input = new Scanner(System.in);
int n = input.nextInt();// 几个数相加
int result = 0;// 结果
int a = 2;// 赋a值,可以改变
int num = 0;
for (int i = 0; i < n; i++) {
num = num * 10 + a;
result = result + num;// 将每次得到的结果累加
}
System.out.println(result);
}

}
WPooh 2011-06-05
  • 打赏
  • 举报
回复

import java.util.*;
public class ok{
public static void main(String[] args){
int a,b,sum=0,c;
String line;
Scanner sc=new Scanner(System.in);
System.out.println("Please Enter two numbers:");
a=sc.nextInt();c=a;
b=sc.nextInt();
while(b>0){
sum+=a;
a*=10;
a+=c;
b--;
}
System.out.println("sum="+sum);
}
}

Please Enter two numbers:
2 5
sum=24690
qqlwq123 2011-06-05
  • 打赏
  • 举报
回复

public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int a = s.nextInt();
int b = s.nextInt();
System.out.println(n(a,b));
}

public static int n(int a, int b) {
if(0==b) {
return 0;
}
else
return m(a,b)+n(a,b-1);
}

public static int m(int a, int b) {
int news = 0;
if(0==b) {
return a;
}
else {
for(int i = b - 1; i>=0; i--) {
int c = a * (int)(Math.pow(10, i));
news = news + c;
}
return news;
}

}
likeFedor 2011-06-05
  • 打赏
  • 举报
回复
public static void main(String[] args) {
int sum=0;//求和的值
/**
* 两个变量
*/
String i="2";
String j="3";

/**
* 执行求和
*/
int k=Integer.parseInt(j);
add(i,k,sum);
}

/**
* 递归方法,如果不是个位数就继续调用
*/
static void add(String i,int j,int sum){
String s=new String();
for(int h=0;h<j;h++){
s=s+i;
}
int e=Integer.parseInt(s);//只转换最大的数
if(j>1){
add(i,--j,sum+e);
}else{
System.out.println(sum+e);//到个位了就打印出来
}
};
riyuezhizhi 2011-06-05
  • 打赏
  • 举报
回复
Integer.valueOf(String)和Integer.parseInt(String)应该功能都是一样的,都是将字符串转化为带符号的十进制整数
pl3121605999 2011-06-05
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 javaxiaochouyu 的回复:]

大数 最好不要用int 用bigInteger吧
[/Quote]
有道理。。。
江清清 2011-06-05
  • 打赏
  • 举报
回复
楼主 试试下递归方法
FW 2011-06-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 pl3121605999 的回复:]

Java code


int number = Integer.valueOf(JOptionPane.showInputDialog("请输入数字"));
int views = Integer.valueOf(JOptionPane.showInputDialog("请输入次数"));
String temp="";
int ……
[/Quote]
views = Integer.valueOf(JOptionPane.showInputDialog("请输入次数"));这个,
跟views = Integer.parseInt(s);两个的区别在哪里?
还有这个了, a=Integer.parseInt(JOptionPane.showInputDialog("输入a"));这几个的区别是什么,求解?
pl3121605999 2011-06-04
  • 打赏
  • 举报
回复


int number = Integer.valueOf(JOptionPane.showInputDialog("请输入数字"));
int views = Integer.valueOf(JOptionPane.showInputDialog("请输入次数"));
String temp="";
int sum = 0;

for (int i = 0;i<views; i++) {
temp+=number;
sum+=Integer.valueOf(temp);
}
}

62,615

社区成员

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

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