PAT 7-38 数列求和-加强版
作者:互联网
代码如下:
#include<stdio.h> int main() { int x,n; scanf("%d%d",&x,&n); if(n==0){printf("0");return 0;} int accum[99999]={0},i,j=0,temp=0,p=0; for(i=n;i>0;i--) { temp=i*x+p; p=temp/10; accum[j++]=temp%10; } if(p!=0) printf("%d",p); for(i=j-1;i>=0;i--) printf("%d",accum[i]); }
参考代码:
#include<stdio.h> int main() { int x,n; scanf("%d%d",&x,&n); int data[n],accum[9999]={0},i,j,temp=0; for(i=0;i<n;i++) { temp=i; data[i]=x; while(temp>=0) accum[temp]+=data[temp--]; for(j=0;j<9999;j++) { if(accum[j]==0) j++; else { int p=accum[j]/10; accum[j]=accum[j]%10; accum[j+1]+=p; } } } int len=9998; while(accum[len]==0) len--; for(i=len;i>=0;i--) printf("%d",accum[i]); }
标签:38,PAT,加强版,temp,int,scanf,d%,printf,accum 来源: https://www.cnblogs.com/yunners/p/12488326.html