解题思路:
设n=5;则sum=a+aa+aaa+aaaa+aaaaa;
分析:
1.n个数相加,考虑到进位如99999+9999,需要长度为n+1的数组存放结果;
2.如图,个位结果为n-0个a的和,十位结果为n-1个a的和...万位为n-4个a的和;
for ( int i = n; i > 0; i-- ) sum[i] += i * a;
3.求出每位的和后,处理进位:从个位数开始,每一位先向前面一位进位,然后再求这一位对应数值(图中先蓝色,后绿色)
for ( int i = n; i > 0; i-- ) { sum[i - 1] += (sum[i] / 10); sum[i] %= 10; }
4.输出结果:如果sum[0]==0的话,最高位没有收到进位,输出sum[1]到sum[n],否则输出sum[0]到sum[n];
if ( sum[0] == 0 ) { for ( int i = 1; i <= n; i++ ) printf( "%d", sum[i] ); }else for ( int i = 0; i <= n; i++ ) printf( "%d", sum[i] );
注意事项:
开辟数组长度为n+1;最高位数下标为0,个位数下标为n;
参考代码: a为正
#include <stdio.h> #include <malloc.h> void format( int *sum, int n ); void function( int *sum, int a, int n ); /*===================================================*/ int main() { int a, n; int *sum; scanf( "%d%d", &a, &n ); sum = (int *) malloc( (n + 1) * sizeof(int) ); format( sum, n ); function( sum, a, n ); return(0); } /*===================================================*/ void format( int *sum, int n ) { for ( int i = 0; i <= n; i++ ) sum[i] = 0; } /*===================================================*/ void function( int *sum, int a, int n ) { for ( int i = n; i > 0; i-- ) sum[i] += i * a; /*------------------------------------*/ for ( int i = n; i > 0; i-- ) { sum[i - 1] += (sum[i] / 10); sum[i] %= 10; } /*------------------------------------*/ if ( sum[0] == 0 ) { for ( int i = 1; i <= n; i++ ) printf( "%d", sum[i] ); }else for ( int i = 0; i <= n; i++ ) printf( "%d", sum[i] ); return ; }
等哪天上面代码不正确了:说明测试数据有a为负数,那么用下面代码正负a都对,a为负,会加负号;
#include <stdio.h> #include <malloc.h> #include <math.h> void format( int *sum, int n ); void function( int *sum, int a, int n ); /*===================================================*/ int main() { int a, n; int *sum; scanf( "%d%d", &a, &n ); sum = (int *) malloc( (n + 1) * sizeof(int) ); format( sum, n ); function( sum, a, n ); return(0); } /*===================================================*/ void format( int *sum, int n ) { for ( int i = 0; i <= n; i++ ) sum[i] = 0; } /*---------------------------------------------------*/ void function( int *sum, int a, int n ) { for ( int i = n; i > 0; i-- ) sum[i] += i * fabs( a ); /*------------------------------------*/ for ( int i = n; i > 0; i-- ) { sum[i - 1] += (sum[i] / 10); sum[i] %= 10; } /*------------------------------------*/ if ( sum[0] == 0 ) { if ( a < 0 ) printf( "-" ); for ( int i = 1; i <= n; i++ ) printf( "%d", sum[i] ); }else{ if ( a < 0 ) printf( "-" ); for ( int i = 0; i <= n; i++ ) printf( "%d", sum[i] ); } return; }
别忘点赞哦-.-
0.0分
19 人评分
#include<stdio.h> #include<math.h> int main() { int a,n,s=0,i,amount; double b,c; scanf("%d %d",&a,&n); c=b=a; for(i=1;i<=n;i++) { amount=c*pow(10,(i-1)); c=c+b*pow(10,-i); s=s+amount; } printf("%d\n",s); } 大佬求解,为什么我这个代码是错的
#include<bits/stdc++.h> using namespace std; int main() { int a,n; long long s,d; s=0; d=0; cin>>a>>n; for(int i=0;i<n;i++) { d=d+a*pow(10,i); s+=d; } cout<<s; }
5022邓亚冰 2022-01-24 22:30:09 |
为什么用longlong型呀
#include<stdio.h> int main() { int a, n, i, S; printf("please input a and n:"); scanf("%d %d", &a, &n); if(a <= 9 && n >= 1) { S = a; for(i = 2; i <= n; i++) { a = (10 * a) + (a % 10); S += a; printf("a = %d\n",a); } printf("S = %d\n", S); } return 0; }
1 #include<stdio.h> 2 3 4 int main() 5 { 6 int a, n, i, S; 7 printf("please input a and n:"); 8 scanf("%d %d", &a, &n); 9 if(a <= 9 && n >= 1) 10 { 11 S = a; 12 for(i = 2; i <= n; i++) 13 { 14 a = (10 * a) + (a % 10); 15 S += a; 16 printf("a = %d\n",a); 17 } 18 19 printf("S = %d\n", S); 20 21 } 22 23 } ~
简单的a+b (C语言代码)浏览:564 |
大神老白 (C语言代码)浏览:690 |
WU-字符串比较 (C++代码)浏览:824 |
Wu-求圆的面积 (C++代码)浏览:1994 |
C语言程序设计教程(第三版)课后习题6.8 (C语言代码)浏览:544 |
【计算直线的交点数】 (C语言代码)浏览:1501 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:624 |
C语言训练-数字母 (C语言代码)浏览:648 |
回文数字 (C语言代码)浏览:2539 |
sizeof的大作用 (C语言代码)浏览:1593 |