【c语言编程题】输入a,n输出s=a+aa+aaa+…n项之和。要求定义一个fn(a,n)函数实现
#include
#include
int main()
{
int a,n;
int i;
long sum = 0;
int m = 0;
printf("Input a,n:
");
scanf("%d,%d",&a,&n);
for(i=1; i<=n; i++)
{
m = a*pow(10,i-1) + m;
sum = sum + m;
}
printf("sum=%ld
",sum);
return 0;
}
答案可以过作业
在CB17.12中有可能出现 当i=3时 pow(10,i-1)为99的情况,求大神解答一下
这个题利用循环结构就好了,具体代码如下:
#include #include#include int sum(int n, int a); main(){ int a , n ;printf("Input a,n:
");scanf("%d,%d",&n,&a);printf("sum=%ld
",sum(n,a));system("pause"); } int sum(int a,int n) {int i ;int A=a ;int s=0 ;for(i=1;i<=n;i++){s+=A;A=A+a*pow((double)10,i);}return s; }
#include<stdio.h>
int main()
{
int a,n,s=0;
int fn(int a,int n);
while(1)
{
printf("Please enter a positive integer(0~9):a=");
scanf("%d",&a);
if(a>=0 && a<=9) break;
}
printf("Please enter a positive integer:n=");
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
s+=fn(a,i);
}
printf("s=%d",s);
}
int fn(int a,int n)
{
int t;
t=a;
for(int i=2;i<=n;i++)
{
a=a*10+t;
}
return a;
}
#include<stdio.h>
#include<math.h>
int fn(an,nn)
{
int in;
long int sn=0;
for (in=1; in<=i;i++)
{
sn=sn+an*10^(in-1);
}
}
void main(void)
{
int n;
int a;
int i;
int s=0;
printf("input a n");
scanf ("%d %d",&a,&n);
for(i=1;i<=n;i++)
{
s=s+fn(a,i)
}
}
#include <stdio.h>
long fn(int a, int n);
int main(int argc, char argv)
{
int a = 0, n = 0;
printf(" Please input 2 number(a,n): ");
scanf("%d,%d", &a, &n);
printf("
a=%d, n=%d sun = %d
", a, n, fn(a, n));
return 0;
}
long fn(int a, int n)
{
int i;
long sun = 0, rat = 1;
for ( i = 0; n - i > 0; i++)
{
sun += a * rat * (n-i);
rat *= 10;
}
return sun;
}
S等于二分之N方加上二分之一N 然后整体乘以a
【c语言编程题】输入a,n输出s=a+aa+aaa+…n项之和。要求定义一个fn(a...
int a,n,s=0;int fn(int a,int n);while(1){ printf("Please enter a positive integer(0~9):a=");scanf("%d",&a);if(a>=0 && a<=9) break;} printf("Please enter a positive integer:n=");scanf("%d",&n);for(int i=1;i<=n;i++){ s+=fn(a,i);} printf("s...
输入整数a和n,输出结果为s,其中s与a,n的关系是:s=a+aa+aaa+aaaa+aa...
include <stdio.h>int main() {int i,n,a,sum = 0;while(scanf("%d%d",&n,&a) == 2 && a && n) {sum = 0;for(i = 1; i <= n; ++i) {sum = 10 * sum + a;}printf("sum = %d\\n",sum);}return 0;}
C语言输入一个数字a和一个整数n,s=a+aa+aaa+aa...a最后一项为n个a计 ...
include <stdio.h>int main (){ int a, n; int t = 0, s = 0; scanf("%d %d", &a, &n); while(n--){ t = t*10+a; s += t; } printf("%d\\n", s); return 0;}
输入整数a,输出结果s,其中s与a的关系是:s=a+aa+aaa+aaaa+aa...a,最 ...
for(i=0; i<n; i++){ f = f * 10 + a;s += f;} printf("a + aa + aaa + ... aa..a = %ld\\n", s);}运行测试:请输入a、n(以空格分隔):2 6;a + aa + aaa + .. + aa..a = 246912。如题干中所示,一共需要输入两个数字,第一个是需要求和的数字,第...
C语言输入n的值,求出s;其中s=1 + 2! + 3! + 4!+ …… + n!用循环...
include <stdio.h>long fac(int n);int main(void){ long sum=0; int num; printf("Enter a number:"); scanf("%d",&num); for(int i=1;i<=num;i++) sum+=fac(i); printf("result is %ld\\n",sum); return 0;}long fac(int n){ int sum=1;...
这是一道C语言编程题:求s=a+aa+aaa+⋯⋯+aa⋯a,最后一项为n个a...
n, long a){\/\/计算a+aa+aaa…… int i = 1;long sum = 0;for(; i <= n; i++)sum += getVal(i,a); return sum; } int main(){long n = 0;\/\/存储n long a = 0;\/\/存储a scanf("%ld%ld", &n, &a); printf("%ld\\n", getSum(n, a)); return 0; } ...
c语言编程题 求s=a+aa+aaa+……+aaa…a(有n个a),a,n都由键盘输入。请问...
可以看看 include <stdio.h> main(){ int a;int s=0;int c;int t=0;int i,j;printf("本程序求和s=a+aa+aaa+...\\n");printf("请分别输入a值及求和次数");scanf("%d %d",&a,&c);i = 1;while(i<=c){ t=t*10+a;s=s+t;i++;} printf("%d",s);return 0;} ...
求s=a+a^2+a^3+···+a^n 的C语言程序 (a与n都是正整数)
include <stdio.h> int main(){ int i,s,a,n;\/\/输入a和n scanf("%d%d",&a,&n);int tmp = a;s = 0;for(i = 1; i <= n; i++){ s += tmp;tmp *= a;} printf("%d\\n",s);return 0;}
试编程求s=a+aa+aaa+...+a...a(n个a)的值,其中a和n通过键盘输入
include<stdio.h> void main(){ int a,n,i;long int s=0,t=0;printf("please input a and n\\n");scanf("%d,%d",&a,&n);for(i=0;i<n;i++){ t=t+a;s=s+t;a=a*10;} printf("a+aa+...=%ld\\n",s);}
用C语言编程:从键盘上输入两个正整数n和a, 求Sn=a+aa+aaa+ …a…aa的...
if(n==1){ sum=a;} else { sum=(long)(pow(10,n-1)*a)+fac(n-1,a);} return(sum);} main(){int a,n,i;long temp,sum=0;printf("plz input number a\\n");scanf("%d",&a);printf("plz input number n\\n");scanf("%d",&n);for(i=1;i<=n;i++){ temp=fac(i,...