C語言之非常簡單的幾道題(還是寫寫),比較簡單吧,主要有幾道題的數據類型(如,第三題)和語句順序(如,第二題)需要注意一小下下。
- 1. 求表達式S=1*2*3……*N的值大於150時,最小的N的值
1 /* 2 1. 求表達式S=1*2*3……*N的值大於150時,最小的N的值 3 */ 4 #include<stdio.h> 5 void main(){ 6 int n=1,s=1; 7 while(s<=150){ 8 s*=n; 9 n++; 10 } 11 printf("%d",n-1); 12 getch(); 13 }
- 2. 求表達式1/1+1/2+1/3……+1/n的值大於3時,最少需要多少項
1 /* 2 2. 求表達式1/1+1/2+1/3……+1/n的值大於3時,最少需要多少項 3 */ 4 #include<stdio.h> 5 void main(){ 6 float n=0,s=0; 7 while(s<=3){ 8 n++; 9 s=s+1/n; 10 //這里注意累加和自增的順序,根據n的初值的不同而需要改變順序 11 } 12 printf("%d",(int)n); 13 getch(); 14 }
-
3. 根據媒體發布的信息,2010年中國GDP總量為5.845萬億美元 ,增長率10.1%,
美國GDP總量為14.536萬億美元,增長率3.3%。在兩國GDP保持2010年的速度不變的情況下,
編程計算出最早哪一年中國的GDP總量有望超過美國GDP?1 /* 2 3. 根據媒體發布的信息,2010年中國GDP總量為5.845萬億美元 ,增長率10.1%, 3 美國GDP總量為14.536萬億美元,增長率3.3%。在兩國GDP保持2010年的速度不變的情況下, 4 編程計算出最早哪一年中國的GDP總量有望超過美國GDP? 5 */ 6 // 7 #include<stdio.h> 8 void main(){ 9 float c=5.845,a=14.536,cg=0.101,ag=0.033; 10 int y=2010; 11 while(c<=a){ 12 c+=c*cg; 13 a+=a*ag; 14 y++; 15 } 16 printf("%d",y); 17 getch(); 18 }
- 4. 求表達式s=1+2+3+4……+n的值,n的值由鍵盤輸入
1 /* 2 4. 求表達式s=1+2+3+4……+n的值,n的值由鍵盤輸入 3 */ 4 #include<stdio.h> 5 void main(){ 6 int i,n,s=0; 7 scanf("%d",&n); 8 for(i=1;i<=n;i++){ 9 s+=i; 10 } 11 printf("%d",s); 12 getch(); 13 }
- 5. 求出表達式s=1!+2!+3!……+n!,n由鍵盤鍵入
1 /* 2 5. 求出表達式s=1!+2!+3!……+n!,n由鍵盤鍵入 3 */ 4 #include<stdio.h> 5 void main(){ 6 int i,j=1,s=0,n; 7 scanf("%d",&n); 8 for(i=1;i<=n;i++){ 9 j*=i; 10 s+=j; 11 } 12 printf("%d",s); 13 getch(); 14 }