帮我写C语言题目:编程实现:s=a+aa+aaa+...+aaa...aaa的值,其中a是1~9之间的某个数字,n是一个正整数。 输入输出样例: 第一行:2表示a的值,3表示n的值,即2+22+222 第二行:求出的结果. *********Begin**********和********** End **********不可删除 */ #include<stdio.h> void bky(); int process(int a,int n) { /********** Begin **********/ /********** End **********/ } void main() { int s=0,a,n; scanf("%d,%d",&a,&n); s=process(a,n); printf("s=%d\n",s); bky(); } void bky() { FILE *IN,*OUT; int i; int a,n,iOUT; IN=fopen("in.dat","r"); if(IN==NULL) { printf("Please Verify The Currernt Dir..it May Be Changed"); } OUT=fopen("out.dat","w"); if(OUT==NULL) { printf("Please Verify The Current Dir.. it May Be Changed"); } for(i=0;i<=2;i++) { fscanf(IN,"%d%d",&a,&n); iOUT=process(a,n); fprintf(OUT,"%d\n",iOUT); } fclose(IN); fclose(OUT); }
时间: 2023-07-24 15:16:36 浏览: 343
#include<stdio.h>
void bky();
int process(int a,int n) {
int sum = 0, temp = a, times = 1; // sum用于保存累加和,temp保存原始输入的a,times用于计算a, aa, aaa...的值
for(int i = 1; i <= n; i++) {
sum += temp * times; // 累加a, aa, aaa...的值
temp = temp * 10 + a; // 计算下一个a, aa, aaa...
times *= 10; // 计算对应a, aa, aaa...的位数
}
return sum; // 返回累加和
}
void main() {
int s=0,a,n;
scanf("%d,%d",&a,&n);
s=process(a,n);
printf("s=%d\n",s);
bky();
}
void bky() {
FILE *IN,*OUT;
int i;
int a,n,iOUT;
IN=fopen("in.dat","r");
if(IN==NULL)
{
printf("Please Verify The Currernt Dir..it May Be Changed");
}
OUT=fopen("out.dat","w");
if(OUT==NULL)
{
printf("Please Verify The Current Dir.. it May Be Changed");
}
for(i=0;i<=2;i++)
{
fscanf(IN,"%d%d",&a,&n);
iOUT=process(a,n);
fprintf(OUT,"%d\n",iOUT);
}
fclose(IN);
fclose(OUT);
}
阅读全文