解题思路:
比如给定a=2,n=3:就是2+22+222 = 246;
246 = 6 + 40 + 200 = 3*2 + 2*20 + 1*200
那么有:给定a、n: 结果sum = n*a + (n-1)*a*10 + (n-2)*a*10*10 + ……+ 1 *a*10^(n-1)
我们可以用循环搞定它:sum = 0;
while(n)
{
sum = sum + n*a;
a = a*10;
n--;
}
注意事项:
参考代码:
#include <cstdio> #include <cstring> #include <string> #include <cmath> #include <functional> #include <iostream> #include <algorithm> using namespace std; int main() { int a, n, sum = 0; scanf("%d%d", &a, &n); while(n) { sum += n*a; a *= 10; n--; } printf("%d\n", sum); return 0; }
0.0分
0 人评分
2005年春浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:714 |
开心的金明 (C++代码)浏览:1188 |
人见人爱A+B (C语言代码)浏览:776 |
分糖果 (C++代码)浏览:865 |
汽水瓶 (C语言代码)怎么答案错误?????浏览:800 |
C语言程序设计教程(第三版)课后习题9.3 (C语言代码)浏览:576 |
上车人数 (C语言代码)浏览:1180 |
钟神赛车 (C语言代码)浏览:878 |
简单的a+b (C语言代码)浏览:717 |
简单的a+b (C语言代码)浏览:530 |