我是靠谱客的博主 谦让音响,这篇文章主要介绍数字个数依次叠加 s=a+aa+aaa+aaaa+aa...a,现在分享给大家,希望可以做个参考。

题 目 : 求 s=a+aa+aaa+aaaa+aa…a 的 值 , 其 中 a 是 一 个 数 字 。 例 如2+22+222+2222+22222(此时共有 5 个数相加),几个数相加有键盘控制
分析:重点求出每一个数值,利用循环


复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.math.forth; import java.util.Scanner; /*** * 题 目 : 求 s=a+aa+aaa+aaaa+aa...a 的 值 , 其 中 a 是 一 个 数 字 。 例 如 * 2+22+222+2222+22222(此时共有 5 个数相加),几个数相加有键盘控制 * * @author wql * */ public class Math07 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入要打印的数字:"); int num = sc.nextInt(); System.out.println("请输入打印数量:"); int count = sc.nextInt(); method(num, count); } public static void method(int num, int count) { int temp = 0;//临时变量,存储每一个值 int sum = 0;//累加和 for (int i = 0; i < count; i++) { temp = temp * 10 + num; //每循环一次原数便乘上10加5 sum += temp; if (i == count - 1) { System.out.print(temp + "="); } else { System.out.print(temp + "+"); } } System.out.println(sum); } }

这里写图片描述

转载于:https://www.cnblogs.com/wangqilong/p/8279772.html

最后

以上就是谦让音响最近收集整理的关于数字个数依次叠加 s=a+aa+aaa+aaaa+aa...a的全部内容,更多相关数字个数依次叠加内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(84)

评论列表共有 0 条评论

立即
投稿
返回
顶部