int sum=0,a=1,b=1,ab;for(int i=1;i<20;i++) ab=a+b; sum =sum+ab-a; a=b; b=ab;}既然是20个,那就从1循环到20 依次得到该处的值,再进行累加。就可以了。前一位哥们没看清题目int sum = 0;for (int i = 1; 2*i-1 <= 19; i++) int sum1 = 1;int x = 2*i-1;for (int j = 1; j <= x; j++) sum1 = sum1 * j;}sum = sum + sum1;}system.out.println("1~19的奇数阶乘和为"+sum);首先你的循环中i=1在无限循环,要改成i++,就是每循环一次i递增1然后你是要加到19为止,就必须是2*i-1<=19然后1! = 13! = 1*2*35! = 1*2*3*4*5这些必须体现出来,也就是j的用处,j从1递增到2*i-1,然后乘起来形成sum1最后把所有sum1加起来
4,用不同的开发语言实现sum123100
c#include <stdio.h>void main()printf("%d",100/2*(1+100));}PASCALprogram sum;begin writeln(100/2*(1+100));end.#include int main()double sum;int i;for (i=1;i<=49;i=i+2)sum = sum + (double) i / (double)(i+1);} printf("sum=%lf\n",sum);return 0;};vbsub sum()msgbox 100/2*(1+100))end subRuby:class Sum sum=0; (1..100).inject puts sum;endjavaint sum = 0;for(int i =1; i <= 100; i++) sum += i;}System.out.println("sum=" + sum );javascript<script>var sum = 0;var i;for(i = 1;i<=100;i++)sum = sum + i}document.write(sum);</script>直接放在网页里面调试就可以看结果了.
5,编程113151719求前n项的总和
#include int main() int i,n; float r=0; scanf("%d",&n);//输入n值。 for(i = 1; i <= n; i ++)//循环执行n次。 if(i%2==1) r+=1.0/(3*n-2);//奇数项加。 else r-=1.0/(3*n-2);//偶数项减。 } printf("%f\n", r);//输出结果。 }#include<stdio.h> main()int n,i,j,sign=1;float sum=0.;printf("求1-1/3+1/5-1/7+...前n项和\n");scanf("%d",&n);for(i=1;i<=n;i++)j=2*i-1;sum+=(float)(1/(float)j)*sign;sign=-sign;}printf("%f",sum);}import java.util.scanner;public class test40005 public static void main(string[] args) int ri, repeat; int i, n, temp; float sum; scanner in=new scanner(system.in); repeat=in.nextint(); for(ri=1; ri<=repeat; ri++) n=in.nextint(); /*-----------(此处填空)---------*/ int m = 1; sum = 0; for(int j = 0; j < n; j++) // system.out.println(1 + "/" + m); float t = 1.0f / m; sum+=t; m+=2; } /*-----------(此处填空)---------*/ system.out.println(sum); } }}