1074: 求1+1/2+1/3+...+1/n
Description
输入一个正整数 repeat (0<repeat<10),做repeat 次下列运算:
读入 1 个正整数 n(n<=50),计算并输出1+1/2+1/3+...+1/n (保留3 位小数)。Input
见sample
Output
见sample
Sample Input
2
2
10
Sample Output
1.500
2.929
#include<stdio.h> int main() {int repeat,a,i,n,flag;double item,sum;scanf("%d",&repeat);for(a=1;a<=repeat;a++){sum=0;item=1;flag=1;scanf("%d",&n);for(i=1;i<=n;i++){sum=sum+flag*1/item;item=item+1;}printf("%.3f\n",sum);}return 0; }
1075: 求n!
Description
输入一个正整数 repeat (0<repeat<10),做repeat 次下列运算:
读入 1 个正整数 n (n<=22),计算并输出 n! 。Input
见sample
Output
见sample
Sample Input
3
1
10
5
Sample Output
1
3628800
120
#include<stdio.h> double fact(int n); int main() {int i,r,n;scanf("%d",&r);if(r>0&&r<10){for(i=1;i<=r;i++){scanf("%d",&n);if(n<=22)printf("%.0f\n",fact(n)); }}return 0; } double fact(int n) {double result,x;result=1;for(x=1;x<=n;x++){result=result*x; }return result; }
1076: 求x 的n 次幂
Description
输入一个正整数 repeat (0<repeat<10),做repeat 次下列运算:
读入 1 个正实数x 和1 个正整数 n(n<=10),计算并输出x 的n 次幂(保留2 位小数)。Input
见sample
Output
见sample
Sample Input
2
1.5 2
2.0 10
Sample Output
2.25
1024.00
#include<stdio.h> #include<math.h> int main() {int repeat,i,n;double x,y;scanf("%d",&repeat);for(i=1;i<=repeat;i++){scanf("%lf%d",&x,&n);y=pow(x,n);printf("%.2lf\n",y);}
1077: 求1-1/2+1/3-1/4+...
Description
输入一个正整数 repeat (0<repeat<10),做repeat 次下列运算:
读入 1 个正整数 n(n<=100),计算并输出1-1/2+1/3-1/4+⋯⋯的前n 项和(保留3 位小数)。Input
见sample
Output
见sample
Sample Input
1
3
Sample Output
0.833
#include<stdio.h> int main() {int repeat,a,i,n,flag;double item,sum;scanf("%d",&repeat);for(a=1;a<=repeat;a++){sum=0;item=1;flag=1;scanf("%d",&n);for(i=1;i<=n;i++){sum=sum+flag*1/item;item=item+1;flag=-flag;}printf("%.3f\n",sum);}return 0; }
1078: 求1+1/3+1/5+1/7+...
Description
输入一个正整数 repeat (0<repeat<10),做repeat 次下列运算:
读入 1 个正整数 n(n<=100),计算并输出1+1/3+1/5+1/7+⋯⋯的前n 项和(保留3 位小数)。Input
见sample
Output
见sample
Sample Input
1
3
Sample Output
1.533
#include<stdio.h> int main() {int repeat,a,i,n,flag;double item,sum;scanf("%d",&repeat);for(a=1;a<=repeat;a++){sum=0;item=1;flag=1;scanf("%d",&n);for(i=1;i<=n;i++){sum=sum+flag*1/(2*item-1);item=item+1;}printf("%.3f\n",sum);}return 0; }
1079: 求1-1/3+1/5-1/7+...
Description
输入一个正整数 repeat (0<repeat<10),做repeat 次下列运算:
读入 1 个正实数eps,计算并输出1-1/3+1/5-1/7+⋯⋯,直到最后一项的绝对值小于eps(保留6 位小数,不包括最后一项)。Input
见sample
Output
见sample
Sample Input
1
1E-4
Sample Output
0.785348
#include<stdio.h> #include<math.h> int main() {int a,repeat;int flag,d,i;double eps,item,pi;scanf("%d",&repeat);for(a=1;a<=repeat;a++){scanf("%lf",&eps);i=1;flag=1;d=1;item=1;pi=0;while(fabs(item)>=eps){pi=pi+item;i++;flag=-flag;d=d+2;item=flag*1.0/d;}printf("%.6lf\n",pi);}return 0; }