C語言基本編程題_第1頁
C語言基本編程題_第2頁
C語言基本編程題_第3頁
已閱讀5頁,還剩10頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、復(fù)習(xí)題C程序設(shè)計編程題1. 輸入2個整數(shù),求兩數(shù)的平方和并輸出。#in elude <>void mai n() intt a ,b,s;prin tf("please in put a,b:n");scanf("%d%d ” ,&a,&b);s=a*a+b*b;prin tf("the result is %dn",s);2. 輸入一個圓半徑(r),當(dāng)r> = 0時,計算并輸出圓的面積和周長,否則,輸出 提示信息。#in clude <>#define PIvoid mai n() float r

2、 ,s , l;prin tf("please in put r:n ”);scanf("%f ” ,&r);if (r>=0)s=pi*r*r;l=2*i*r ;prin tf("the area is %fn",s);prin tf("the circumfere nee is %fn ",l);elseprin tf("i nput error!n");3. 函數(shù)y=f(x)可表示為:2x+1 (x<0)y= 0 (x=0)2x-1 (x>0)編程實(shí)現(xiàn)輸入一個x值,輸出y值。#in

3、clude <>void mai n() int x,y; scanf( “%d” ,&x); if(x<0) y=2*x+1; else if(x>0) y=2*x-1; else y=0; printf( “ %d”,y);4、編寫一個程序 ,從 4個整數(shù)中找出最小的數(shù) ,并顯示此數(shù)。#include <> void main( ) int a,b,c,d,t;scanf ( “ %d,%d,%d,%d ” ,&a,&b,&c,&d);if (a>b)t=a; a=b; b=t;if (a>c)t=a;

4、 a=c; c=t;if (a>d)t=a; a=d; d=t; printf (“ min = %d n ” ,a);5 .有一函數(shù)當(dāng) x<0時y=1,當(dāng)x>0時,y=3,當(dāng)x=0時y=5,編程,從鍵盤輸入一個 x 值,輸出 y 值。#include <> void main() int x,y; scanf("%d",&x);if (x<0) y=1;else if(x=0) y=5;else y=3; printf("x=%d,y=%dn",x,y);6. 從鍵盤輸入兩個數(shù),求出其最大值(要求使用函數(shù)完成

5、求最大值,并在主函數(shù)中 調(diào)用該函數(shù))#include <>float max(float x,float y);void main() float a,b,m;scanf("%f,%f",&a,&b); m=max(a,b);printf("Max is %fn",m);float max(float x,float y)float temp;if (x<y)temp=x;x=y; y=temp;return(x);7、從鍵盤輸入你和你朋友的年齡,編成判斷誰的年齡最大,并打印最大者的年齡。 #include <&g

6、t; void main() int yourAge, hisAge; printf("Please enter your age:"); scanf("%d", &yourAge); /* 輸入你的年齡 yourAge*/ printf("Please enter your friend's age:");scanf("%d", &hisAge); /* 輸入你朋友的年齡 hisAge*/ if (yourAge >= hisAge)printf("You are olde

7、r! Your age is = %dn", yourAge);if (hisAge > yourAge)printf("Your friend is older! HisAge age is = %dn", hisAge);error8、鍵盤輸入 2 個加數(shù),再輸入答案,如果正確,顯示“right ”,否則顯示“#include “” void main( )int a,b,c;printf( “ please input a and bn” );scanf (%d,%d ” ,&a,&b);printf( “ please input t

8、he answer for a+bn” );scanf (%d ” ,&c);if (c=a+b)printf(“right'n ” );elseprintf(“error'n ” );9. 編一程序每個月根據(jù)每個月上網(wǎng)時間計算上網(wǎng)費(fèi)用,計算方法如下:要求當(dāng)輸入每月上網(wǎng)小時數(shù),顯示該月總的上網(wǎng)費(fèi)用(6分)#in elude <>void mai n() int hour;float fee;printf( “ please in put hour:' n”);scanf( “ %d ,&hour);if(hour<=10)fee=30;

9、else if(hour>=10&&hour<=50)fee=3*hour;else fee=hour*;printf(“The total fee is %f ” ,fee);10. 神州行用戶無月租費(fèi),話費(fèi)每分鐘元,全球通用戶月租費(fèi)50元,話費(fèi)每分鐘0.4元。輸入一個月的通話時間,分別計算出兩種方式的費(fèi)用,判斷哪一種合適。#in elude <>void mai n() float a,x,y;printf( “n請輸入您的話費(fèi):”);seanf(“ %f, ” ,&a);x= *a;y=50+*a;printf ("神州行話費(fèi)為n

10、” ,x);printf ("全球通話費(fèi)為n” ,y);if (x>=y)printf("建議使用全球通”);else printf("建議使用神州行);11 個人所得稅計算,應(yīng)納稅款的計算公式如下:收入稅率收入=1000元部分0 %2000元 =收入1000元的部分5 %3000元 =收入2000元的部分10%6000元 =收入3000元的部分15%收入6000元的部分20%輸入某人的收入,計算出應(yīng)納稅額及實(shí)際得到的報酬。(7分)(如需連續(xù)計算多個人的納稅情況,直到輸入負(fù)數(shù)為止,程序應(yīng)如何改進(jìn)?試寫出程序)#i nclude "”void mai

11、 n()int grade;float in come,tax,m on ey;printf( “ please in put your in comen”);sca nf (“ f',&in come);if (in come<0)printf(“ the in put is error ” );else grade=(i nt)i ncome/1000;switch(grade) case 0 : tax=O;break;case 1 : tax=(i ncome-1000)*;break;case 2 : tax=50+(i ncome-2000)*;break;c

12、ase 3 :case 4 :case 5 : tax=150+(i ncome-3000)*;break;default: tax=600+(i ncome-6000)*;money=in come-tax;printf(“ n tax=%f, money=%f ” ,tax, money);12.從鍵盤上輸入一個百分制成績 score ,按下列原則輸出其等級:score > 90,等級 為 A 80 < score<90,等級為 B; 70w score<80,等級為 C; 60< score<70,等級為 D; score<60,等級為 E。#i

13、nclude <>void main()int data;char grade;printf("Please enter the score:"); scanf("%d ” , &data); switch(data/10) case 10:case 9 : grade='A'break;case 8: grade='B'break;case 7: grade='C'break;case 6: grade='D'break;default: grade='E'pri

14、ntf("the grade is %c,grade);*13. 編程設(shè)計一個簡單的計算器程序。從鍵盤輸入 2 個操作數(shù), 1 個運(yùn)算符,當(dāng)運(yùn) 算符為加( +)、減( -)、乘( *)、除( / )時,輸出計算結(jié)果#include <>void main() int data1, data2;/* 定義兩個操作符 */char op; /* 定義運(yùn)算符 */printf("Please enter the expression:");scanf("%d%c%d", &data1, &op, &data2);

15、/*輸入運(yùn)算表達(dá)式 */switch(op) /* 根據(jù)輸入的運(yùn)算符確定要執(zhí)行的運(yùn)算 */ case '+': /* 處理加法 */printf("%d + %d = %d n", data1, data2, data1 + data2); break;case '-': /* 處理減法 */ printf("%d - %d = %d n", data1, data2, data1 - data2); break;case '*': /* 處理乘法 */ printf("%d * %d = %d

16、n", data1, data2, data1 * data2); break;case '/': /* 處理除法 */if (0 = data2) /* 為避免出現(xiàn)溢出錯誤,檢驗(yàn)除數(shù)是否為 0*/printf("Division by zero!n");else printf("%d / %d = %d n", data1, data2, data1 / data2);break;default: printf("Unknown operator! n");14. 從鍵盤輸入 10 個整數(shù),統(tǒng)計其中正數(shù)、負(fù)

17、數(shù)和零的個數(shù),并在屏幕上輸出。 #include <>void main( )int a10, i,p=0,n=0,z=0; printf(“ please input number ” );for(i=0;i<10;i+) scanf( “ %d,” ,&ai);if (ai>0)p+; else if (ai<0)n+;else z+printf("正數(shù):5d,負(fù)數(shù):5d,零:n”,p,n,z);15、編程序?qū)崿F(xiàn)求 1-10 之間的所有數(shù)的乘積并輸出。#include <>void main( ) int i ;long sum=

18、1;for(i=1; i<=10; i=i+1)sum=sum*i;printf( “the sum of odd is :% l d” ,sum);16. 從鍵盤上輸入 10 個數(shù),求其平均值。#include <>void main()int a10,i,s=0;float ave; for(i=0;i<10;i+)scanf( “%d” ,&ai); for(i=0;i<10;i+)sum+=ai; ave=(float)sum/10; printf("ave = %fn", ave);17、編程序?qū)崿F(xiàn)求 1-1000 之間的所有

19、奇數(shù)的和并輸出。#include <>void main( ) int i, sum=0;for(i=1; i<1000; i=i+2) sum=sum+i;printf(“ the sum of odd is :%d” ,sum);18. 有一個分?jǐn)?shù)序列:2/1 , 3/2 , 5/3 , 8/5 , 13/8 , 21/13 編程求這個序列的前 20 項(xiàng)之和。#include <>void main()int i,t,n=20;float a=2,b=1,s=0;for(i=1;i<=n;i+)s=s+a/b;t=a;a=a+b;b=t; printf(

20、"sum=%",s);19. 用數(shù)組實(shí)現(xiàn)以下功能:輸入 5 個學(xué)生成績,而后求出這些成績的平均值并顯示 出來。#include <> void main() int a5, s=0; int i; for(i=0;i<5;i+) scanf( “%d” ,&ai);for(i=0;i<5;i+) s=s+ai;printf(“ result=%f ”,s/;20、用循環(huán)的方法構(gòu)造一個5 行 5 列的二維數(shù)組, 使主對角線上的變量為1,其它為0,并將數(shù)組中所有項(xiàng)按行按列顯示出來。#include <>void main() int

21、a55,i,j, s=0;for(i=0;i<5;i+) for(j=0;j<5;j+) if(i=j) aij=1;else aij=0;for(i=0;i<5;i+) for(j=0;j<5;j+)printf(“ %5d” , aij);printf(“ n ”);21 求一個3X 3矩陣主對角線元素之和。從鍵盤輸入矩陣元素的值并輸出和的值.#include <>void main() int a33,sum=0;int i,j;printf("Enter data:n"); for(i=0;i<3;i+) for(j=0;j

22、<3;j+) scanf("%d",&aij);for(i=0;i<3;i+) sum=sum+aii;printf("sum=%d",sum);22. 輸入 n 的值,*n 代表行數(shù),輸出如圖所示的圖形。* * * * * * * * * * * * (此圖為n = 6時的輸出結(jié)杲)#include <> void main() int i , k , n;printf("Enter number:n");scanf("%d",&n);for (i = 1; i <=

23、 n; i+)/*控制行數(shù) */ for (k = 1; k <= i; k+) /*控制每行輸出的 * 號個數(shù) */printf("*");printf("n");/*輸出一行后換行 */23、從鍵盤輸入 30 名學(xué)生的成績數(shù)據(jù),求其中的最高分、最低分和平均分。 (提示:用數(shù)組存放成績數(shù)據(jù))#include<>#define M 30void main ( ) float scoreM, max , min, aver;int i ;printf(“please input score: n” );for(i=0; i<M ;

24、i+)scanf(“ %f” , &scorei);max=score0;min=score0;aver=score0;for(i=1; i<M; i+) if (max < scorei) max= scorei;if (min>scorei) min=scorei;aver+=scorei;printf( “max=%f, min=%f,aver=%f ” , max, min, aver/M);24. 從鍵盤輸入某班學(xué)生某門課的成績及其學(xué)號(班級人數(shù)最多 40 人,具體人數(shù)由 鍵盤輸入),輸出該班最高分和最低分及其學(xué)生學(xué)號;并輸出該班該課程的總分和平 均分。請編

25、寫程序。#include <>#define ARR_SIZE 40void main() float scoreARR_SIZE, maxScore,minScore,sum;int n, i;long maxNum, minNum,numARR_SIZE;printf("Please enter total number:");scanf("%d", &n);printf("Please enter the number and score:n");for (i=0; i<n; i+)scanf(&quo

26、t;%ld%f", &numi, &scorei);maxScore = score0;minScore= score0;maxNum = num0; minNum= num0;sum=score0;for (i=1; i<n; i+) if (scorei > maxScore) maxScore = scorei; maxNum = numi;else if (scorei < minScore) minScore = scorei; minNum = numi; sum=sum+scorei;printf("maxScore = %.

27、0f, maxNum = %ldn", maxScore, maxNum); printf("minScore = %.0f, minNum = %ldn", minScore, minNum);printf("sum = %.1f, average = %.1fn", sum, sum/n);25. 將一個有 5 個元素的數(shù)組中的值 ( 整數(shù)) 按逆序重新存放。例: 原來順序?yàn)?:8 、6、5、4、1,要求改為 1、4、5、6、8 #define N 5 #include <> void main() int aN,i,temp;

28、printf( “ enter array a:n” );for(i=0;i<N;i+) scanf( “ %d” ,&ai);for(i=0;i<N;i+) temp=ai; ai=aN-i-1; aN-i-1=temp;printf(“n Now, array a:n” );for(i=0;i<N;i+)printf(“ %4d” ,ai);printf(“n ” );26. 從鍵盤上輸入一個 2*3 的矩陣,將其轉(zhuǎn)秩后形成 3*2 的矩陣輸出。#include <>void main()int a23, b32,i,j;for(i=0;i<2;

29、i+) for(j=0;j<3;j+) scanf(“ %d” ,&aij);for(i=0;i<3;i+) for(j=0;j<2;j+) bij=aji;for(i=0;i<3;i+) for(j=0;j<2;j+) printf("%5d",bij); printf("n” );29、從鍵盤輸入 10 名學(xué)生的成績數(shù)據(jù),按成績從高到低的順序排列并輸出。 (提示: 用數(shù)組存放成績數(shù)據(jù))#include <>void main() int a10;int i,j,temp;printf("input s

30、core:n"); for(i=0;i<10;i+)scanf("%d",&ai);printf("n");for(i=1;i<10;i+)for(j=0;j<9;j+)if(aj<aj+1)temp=aj;aj=aj+1;aj+1=temp; for(i=0;i<10;i+) printf("%d,",ai);30. 定義一個 5 行 3 列的數(shù)組,從鍵盤輸入各數(shù)組元素的值,計算各數(shù)組元素之和。 #include <>void main( ) int i, j ,a53;p

31、rintf( “ Enter data:n ” );for(i=0;i<5;i+)for(j=0;j<3;j+)scanf(“ %d” ,&aij);for(i=0;i<5;i+)for(j=0;j<3;j+) sum=sum+aij;printf(“ sum=%5dn” ,sum);32、從鍵盤上輸入一個 4*3 的整型數(shù)組, 找出數(shù)組中的最小值及其在數(shù)組中的下標(biāo)。 #include <>void main() int a43, i , j ,min,m,n; printf("Please enter data:"); for

32、(i=0; i<4; i+) for (j=0; j<3; j+) scanf(“ %d” ,& aij);min=a00;m=0; n=0; for (i=0; i<4; i+) for (j=0; j<3; j+) if (aij<min) min= aij; m=i; n=j; printf("the min is %dn, min); printf("posion is %d %d n, m,n); 33編程實(shí)現(xiàn)如下功能:從鍵盤輸入一行字符,統(tǒng)計其中大寫英文字符,小寫英文 字符和其他字符的個數(shù)。#include <>

33、#include <>#define ARR_SIZE 80 void main()char strARR_SIZE;int len, i, letter = 0, digit = 0, space = 0, others = 0;printf("Please input a string:");gets(str);len = strlen(str);for (i=0; i<len; i+) if (stri >= 'a' && stri <= 'z' | stri >= 'A

34、9; && stri<= 'Z')letter +;/* 統(tǒng)計英文字符 */else if (stri >= '0' && stri <= '9' )digit +; /* 統(tǒng)計數(shù)字字符 */elseothers +;/*統(tǒng)計其它字符的個數(shù) */printf("English character: %dn", letter); printf("digit character: %dn", digit);printf("other character: %dn", others);*36 、猜數(shù)游戲。系統(tǒng)隨機(jī)產(chǎn)生一個整數(shù),通過鍵盤輸入數(shù)據(jù)猜數(shù),猜對為止,并要 求統(tǒng)計猜的次數(shù)。注:rand()函數(shù)可以產(chǎn)生032767間的正整數(shù),程序中需包含。 #include <> #include <>void main()int magic;int guess;int counter;magic = rand() % 100 + 1;counter = 0;doprintf("Please guess a magic

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論