<tt id="a3jom"></tt>
    1. <tt id="a3jom"><noscript id="a3jom"></noscript></tt>

        <tt id="a3jom"></tt>

        (精品)計算機程序設計基礎(C語言)編程習題

        上傳人:仙*** 文檔編號:188191081 上傳時間:2023-02-18 格式:DOC 頁數:20 大?。?45.01KB
        收藏 版權申訴 舉報 下載
        (精品)計算機程序設計基礎(C語言)編程習題_第1頁
        第1頁 / 共20頁
        (精品)計算機程序設計基礎(C語言)編程習題_第2頁
        第2頁 / 共20頁
        (精品)計算機程序設計基礎(C語言)編程習題_第3頁
        第3頁 / 共20頁
        資源描述:

        《(精品)計算機程序設計基礎(C語言)編程習題》由會員分享,可在線閱讀,更多相關《(精品)計算機程序設計基礎(C語言)編程習題(20頁珍藏版)》請在裝配圖網上搜索。

        1、計算機程序設計基礎(c語言) 習題 編程題計算機程序設計基礎(C語言)編程練習題及參考答案1.輸入2個整數,求兩數的平方和并輸出。 #include main() intt a ,b,s; printf(please input a,b:n); scanf(%d%d”,&a,&b); s=a*a+b*b; printf(the result is %dn,s); 2. 輸入一個圓半徑(r)當r0時,計算并輸出圓的面積和周長,否則,輸出提示信息。#include #define PI 3.14 main() float r ,s , l; printf(please input r:n); sc

        2、anf(%f”,&r);if (r=0) s=pi*r*r; l=2*i*r ; printf(the area is %fn,s);printf(the circumference is %fn,l);else printf(input error!n); 3、函數y=f(x)可表示為: 2x+1 (x0) 編程實現輸入一個x值,輸出y值。 main()int x,y;scanf(“%d”,&x);If(x0)y=2*x-1;If(x=0) y=0;printf(“%d”,y);4、編寫一個程序,從4個整數中找出最小的數,并顯示此數。main( )int a,b,c,d,t; scanf (

        3、“%d,%d,%d,%d ”,&a,&b,&c,&d); if (ab) t=a; a=b; b=t; if (ac) t=a; a=c; c=t; if (ad) t=a; a=d; d=t; printf (“min = %d n”,a);5有一函數當x0時,y=3,當x=0時y=5,編程,從鍵盤輸入一個x值,輸出y值。main()int x,y;scanf(%d,&x);if (x0) y=1;else if(x=0) y=5;else y=3;printf(x=%d,y=%dn,x,y);6從鍵盤輸入兩個數,求出其最大值(要求使用函數完成求最大值,并在主函數中調用該函數)main()f

        4、loat max(float x,float y); 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 (xy) temp=x; x=y; y=temp;return(x);7、從鍵盤輸入你和你朋友的年齡,編成判斷誰的年齡最大,并打印最大者的年齡。#include main()int yourAge, hisAge;printf(Please enter your age:);scanf(%d, &yourAge); /*輸入你的年齡you

        5、rAge*/printf(Please enter your friends age:);scanf(%d, &hisAge); /*輸入你朋友的年齡hisAge*/if (yourAge = hisAge) printf(You are older! Your age is = %dn, yourAge); if (hisAge yourAge)printf(Your friend is older! HisAge age is = %dn, hisAge); 8、鍵盤輸入2個加數,再輸入答案,如果正確,顯示“right”,否則顯示“error”#include “stdio.h”main(

        6、 )int a,b,c; printf(“please input a and bn”);scanf (%d,%d”,&a,&b); printf(“please input the answer for a+bn”);scanf (%d”,&c); if (c=a+b) printf(“rightn”); else printf(“errorn”);9. 編一程序每個月根據每個月上網時間計算上網費用,計算方法如下: 要求當輸入每月上網小時數,顯示該月總的上網費用(6分)main() int hour; float fee; printf(“please input hour:n”); sca

        7、nf(“%d”,&hour); if(hour=10&hour=y) printf(“建議使用全球通”); else printf(“建議使用神州行); 11個人所得稅計算,應納稅款的計算公式如下:收入稅率收入收入1000元的部分53000元收入2000元的部分106000元收入3000元的部分15收入6000元的部分20輸入某人的收入,計算出應納稅額及實際得到的報酬。(7分)(如需連續計算多個人的納稅情況,直到輸入負數為止,程序應如何改進?試寫出程序)#include “stdio.h”main() int grade; float income,tax,money; printf(“ple

        8、ase input your incomen”);scanf (“%f”,&income); if (income0) printf(“the input is error”); else grade=(int)income/1000; switch(grade) case 0 : tax=0;break; case 1 : tax=(income-1000)*0.05;break; case 2 : tax=50+(income-2000)*0.1;break; case 3 :case 4 :case 5 : tax=150+(income-3000)*0.15;break;default

        9、: tax=600+(income-6000)*0.2; money=income-tax; printf(“n tax=%f, money=%f”,tax, money); 12.從鍵盤上輸入一個百分制成績score,按下列原則輸出其等級:score90,等級為A;80score90,等級為B;70score80,等級為C;60score70,等級為D;score60,等級為E。 #include main()int data; char grade; printf(Please enter the score:);scanf(%d”, &data); switch(data/10) cas

        10、e 10: case 9 : grade=A; break; case 8: grade=B; break;case 7: grade=C; break; case 6: grade=D; break; default: grade=E; printf(the grade is %c”,grade);*13. 編程設計一個簡單的計算器程序。從鍵盤輸入2個操作數,1個運算符,當運算符為加(+)、減(-)、乘(*)、除(/)時,輸出計算結果 #include main() int data1, data2; /*定義兩個操作符*/char op; /*定義運算符*/printf(Please en

        11、ter the expression:);scanf(%d%c%d, &data1, &op, &data2); /*輸入運算表達式*/switch(op) /*根據輸入的運算符確定要執行的運算*/ 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 n, data1, data2

        12、, data1 * data2); break;case /: /*處理除法*/if (0 = data2) /*為避免出現溢出錯誤,檢驗除數是否為0*/printf(Division by zero!n);else printf(%d / %d = %d n, data1, data2, data1 / data2); break;default: printf(Unknown operator! n);14. 從鍵盤輸入10個整數,統計其中正數、負數和零的個數,并在屏幕上輸出。main( ) int a10, i,p=0,n=0,z=0; printf(“please input numb

        13、er”); for(i=0;i0) p+;else if (ai0) n+;else z+ printf(“正數:%5d, 負數:%5d,零:%5dn”,p,n,z);15、編程序實現求1-200之間的所有數的乘積并輸出。#include main( ) int i, sum=1 for(i=1; i200 i=i+1) sum=sum*i; printf(“the sum of odd is :%d”,sum);16. 從鍵盤上輸入10個數,求其平均值。 main() int a10,i,s=0; float ave; for(i=0;i10;i+)scanf(“%d”,&ai); for(

        14、i=0;i10;i+) sum+=ai; ave=(float)sum/10;printf(ave = %fn, ave); 17、編程序實現求1-1000之間的所有奇數的和并輸出。 #include main( ) int i, sum=0; for(i=1; i1000; i=i+2) sum=sum+i; printf(“the sum of odd is :%d”,sum);18.有一個分數序列:2/1,3/2,5/3,8/5,13/8,21/13編程求這個序列的前20項之和。main() int i,t,n=20; float a=2,b=1,s=0;for(i=1;i=n;i+)s

        15、=s+a/b;t=a;a=a+b;b=t; printf(sum=%9.6f,s);19. 用數組實現以下功能:輸入5個學生成績,而后求出這些成績的平均值并顯示出來。 main()float a5,i;float s=0;for(i=0;i5;i+)scanf(“%f”,&ai);for(i=0;i5;I+)s=s+ai;printf(“result=%f”,s/5);*20、用循環的方法構造一個5行5列的二維數組,使主對角線上的變量為1,其它為0,并將數組中所有項按行按列顯示出來。main()int a55,i,j, s=0;for(i=0;I5;i+)for(j=0;j5;j+)if(i=

        16、 =j) aij=1;else aij=0;for(i=0;i5;i+)for(j=0;j5;j+)if(j= =0) printf(“n”);printf(“%d ”, aij);21求一個33矩陣對角線元素之和。從鍵盤輸入矩陣元素的值并輸出和的值.main() int a33,sum=0; int i,j; printf(Enter data:n); for(i=0;i3;i+) for(j=0;j3;j+) scanf(%d,&aij); for(i=0;i3;i+) sum=sum+aii; printf(sum=%d,sum);22.輸入n的值,n代表行數,輸出如圖所示的圖形。(6分

        17、) * * * * * * * * * * * * * * * * (此圖為n4時的輸出結果)#include main()int i , j , k; for (i = 1; i = 4; i+) /*控制行數*/ for (k = 1; k = (2 * i - 1); k+) /*控制每行輸出的*號個數*/ printf(*);printf(n); /*輸出一行后換行*/23、從鍵盤輸入30名學生的成績數據,求其中的最高分、最低分和平均分。(提示:用數組存放成績數據) #include #define M 30 main ( ) float scoreM, max , min, aver

        18、; int i ; printf(“please input score: n”); for(i=0; iM ; i+) scanf(“%f”, &scorei); max=score0; min=score0; aver=score0; for(i=1; iM; i+) if (max scorei) min=scorei; aver+=scorei; printf(“max=%f, min=%f,aver=%f”, max, min, aver/M);24. 從鍵盤輸入某班學生某門課的成績及其學號(班級人數最多40人,具體人數由鍵盤輸入),輸出該班最高分和最低分及其學生學號;并輸出該班該課

        19、程的總分和平均分。請編寫程序。#include #define ARR_SIZE 40main() 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; in; i+) scanf(%ld%f, &numi, &scorei); maxScore = score0;minSco

        20、re= score0;maxNum = num0; minNum= num0; sum=score0;for (i=1; i maxScore) maxScore = scorei; maxNum = numi; else if (scorei minScore) minScore = scorei; minNum = numi; sum=sum+scorei;printf(maxScore = %.0f, maxNum = %ldn, maxScore, maxNum); printf(minScore = %.0f, minNum = %ldn, minScore, minNum);pri

        21、ntf(sum = %.1f, average = %.1fn, sum, sum/n);*25.將一個有5個元素的數組中的值(整數)按逆序重新存放。例: 原來順序為:8、6、5、4、1,要求改為1、4、5、6、8 define N 5main() int aN,I,temp; printf(“enter array a:n”); for(I=0;IN;I+) scanf(“%d”,$ai); for(I=0;IN;I+) temp=ai; ai=aN-I-1; aN-I-1=temp; printf(“n Now, array a:n”); for(I=0;IN;I+) printf(“%4

        22、d”,ai); printf(“n”); *26.從鍵盤上輸入一個2*3的矩陣,將其轉置后形成3*2的矩陣輸出。 main() int a23, b32,i,j; for(i=0;i2;i+) for(j=0;j3;j+) scanf(“%d”,&aij); for(i=0;i3;i+) for(j=0;j2;j+) bij=aji; for(i=0;i3;i+) for(j=0;jy) t=x;x=y;y=t;while(zy) t=x;x=y;y=t;z=x;while(z1) if(x%z=0)&(y%z=0) break; z-;return(z);main()int a,b,c;ch

        23、ar ch;printf(nmingb(1)/maxgy(2)?);ch=getchar();printf(ninput:);scanf(%d,%d,&a,&b);if(ch=1) c=mingb(a,b);else if(ch=2) c=maxgy(a,b);printf(the result is %d,c);getch();*28. 輸入一個3*3矩陣,求出其轉置矩陣,并求出兩個矩陣的和.main()int a33;int b33;int c33int i,j;printf(“please input 6 numbers!”)for (i=1;i3;i+)for(j=1;j3;j+)sc

        24、anf(“%d”,&aij);bji=aij;for (i=1;i3;i+)for(j=1;j3;j+)cij=aij+bij;for (i=1;i3;i+)for(j=1;j3;j+) printf(“%d”,aij); 29、從鍵盤輸入10名學生的成績數據,按成績從高到低的順序排列并輸出。(提示:用數組存放成績數據)main() int a10; int i,j,temp; printf(input score:n); for(i=0;i10;i+)scanf(%d,&ai); printf(n);for(i=1;i10;i+)for(j=0;j9;j+)if(ajaj+1)temp=aj

        25、; aj=aj+1; aj+1=temp;for(i=0;i10;i+) printf(%d,ai);30. 定義一個5行3列的數組,從鍵盤輸入各數組元素的值,計算各數組元素之和。#include main( )int i, j ,a53;printf(“Enter data:n”); for(i=0;i5;i+) for(j=0;j3;j+) scanf(“%d”,&aij); for(i=0;i5;i+) for(j=0;j3;j+)sum=sum+aij; printf(“sum=%5dn”,sum);31、編寫程序,交換兩個數組中的對應元素。 #include #define N 20

        26、 main( ) int aN, bN, i, j, temp; printf(“please input a:n”); for(i=0; iN; i+) scanf(“%d”, &ai); printf(“please input b:n”); for(j=0; jN; j+) scanf(“%d”, &bi); for(i=0; iN; i+) temp=ai; ai=bi; bi=temp; for(j=0; jN; j+) printf(“%d,”, aj); printf(“n”); for(j=0; jN; j+) printf(“%d,”,bj ); *32、從鍵盤上輸入一個4*

        27、3的整型數組,找出數組中的最小值及其在數組中的下標。#include main() int a43, i , j ,min,m,n; printf(Please enter data:); for (i=0; i4; i+) for (j=0; j3; j+) scanf(“%d”,& aij); min=a00; m=0; n=0; for (i=0; i4; i+) for (j=0; j3; j+) if (aijmin)min= aij; m=i; n=j;printf(the min is %dn, min);printf(posion is %d %d n, m,n);33編程實現

        28、如下功能:從鍵盤輸入一行字符,統計其中大寫英文字符,小寫英文字符和其他字符的個數。#include #include #define ARR_SIZE 80main()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= a & stri = A & stri = 0 & stri = 9 ) digit +; /*統計數字字符*/ else othe

        29、rs +; /*統計其它字符的個數*/ printf(English character: %dn, letter); printf(digit character: %dn, digit);printf(other character: %dn, others);*34編程實現如下功能:1)在主函數中,實現從鍵盤輸入10名學生某門課的成績,保存在一維數組中;調用排序函數;對排序后的數組中的元素按從高到低打印輸出。2)編寫排序函數,使用數組名做函數參數,實現對該成績的排序。#include #define ARR_SIZE 40void Sort(float score, long num,

        30、int n); main() float scoreARR_SIZE; int n, i; long numARR_SIZE; printf(Please enter total number:); scanf(%d, &n); printf(Please enter the number and score:n); for (i=0; in; i+) scanf(%ld%f,&numi,&scorei); Sort(score, num, n); printf(Sorted results:n); for (i=0;in;i+) printf(%ldt%4.0fn,numi,scorei);

        31、void Sort(float score, long num, int n) int i, j; float temp1; long temp2; for (i=0; in-1; i+) for (j=i+1; j scorei) temp1 = scorej; scorej = scorei; scorei = temp1;/*交換學號*/ temp2 = numj; numj = numi; numi = temp2; *35編程實現如下功能:實現從鍵盤輸入兩個字符串,分別存入兩個不同的字符數組中;將兩個字符串連接為一個字符串,并打印輸出連接后的整個字符。#include #includ

        32、e #define ARR_SIZE 80void MyStrcat(char dstStr, char srcStr);main() char sARR_SIZE, tARR_SIZE;printf(Please enter source string: );gets(s);printf(Please enter destination string: );gets(t);MyStrcat(s,t);printf(The concatenate string is: );puts(s);void MyStrcat(char dstStr, char srcStr) int i = 0, j;

        33、 while (dstStri != 0) i+; for (j=0; srcStrj!=0; j+, i+) dstStri = srcStrj; dstStri = 0;*36、猜數游戲。系統隨機產生一個整數,通過鍵盤輸入數據猜數,猜對為止,并要求統計猜的次數。注:rand()函數可以產生032767間的正整數,程序中需包含stdlib.h。#include #include main() int magic; int guess; int counter; magic = rand() % 100 + 1; counter = 0; doprintf(Please guess a mag

        34、ic number:);scanf(%d, &guess); counter +; if (guess magic) printf(Wrong!Too high!n);else if (guess magic) printf(Wrong!Too low!n);while (guess != magic); printf(Right!n); printf(counter = %dn, counter); 37.輸入兩個整數,利用指針變量作為函數參數,編程實現兩數互換功能,并將交換后的數據重新輸出。 #include void Swap(int *x, int *y);main() int a,

        35、b;printf(Please enter a,b:);scanf(%d,%d, &a, &b); printf(Before swap: a = %d,b = %dn, a,b); Swap(&a, &b); printf(After swap: a = %d,b = %dn, a, b); void Swap(int *x, int *y) int temp;temp = *x; *x = *y; *y = temp; 38.隨機輸入若干個學生的體重,以輸入負數或零結束,分別求最重和最輕的體重,并計算平均體重。 #include main() int n=0; float weight,m

        36、ax=0,min=10,sum=0,ave; printf(“please input the weight:”); scanf(“%f”,& weight); while(weight0) sum=weight+sum; n+; if (weightmax) max=weight; scanf(“%f”,& weight);if (n0) ave=sum/n; printf(maxweight = %fn , max); printf(minweight = %fn, min);printf(ave = %fn,ave); else printf(NO VALID DATA”);39.輸入m

        37、,k的值,編程求下面表達式的值:(要求編寫一個求階乘的函數,調用函數實現本題)#include unsigned long Factorial(unsigned int number);main() unsigned int m, k;double p; printf(Please input m, k:); scanf(%u, %u, &m, &k); p = (double)Factorial(k) / Factorial (m-k); printf(p=%fn, p); unsigned long Factorial(unsigned int number)unsigned long i

        38、, result = 1; for (i=2; i=number; i+)result *= i; return result;*40. 編寫程序,其中自定義一函數,用來判斷一個整數是否為素數,主函數輸入一個數,輸出是否為素數。#include int IsPrimeNumber(int number)int i;if (number = 1)return 0;for (i=2; isqrt(number); i+)if (number % i) = 0)return 0; return 1; main() int n; printf(“Please input n:”); scanf(“%d”,&n); if(IsPrimeNumber(n) printf(“n%d is a Prime Number”,n); else printf(“n%d is not a Prime Number”,n); 20

        展開閱讀全文
        溫馨提示:
        1: 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
        2: 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
        3.本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
        4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
        5. 裝配圖網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
        6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
        7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
        關于我們 - 網站聲明 - 網站地圖 - 資源地圖 - 友情鏈接 - 網站客服 - 聯系我們

        網站客服QQ:2846424093或766697812

        copyright@ 2020-2023  zhuangpeitu.com 裝配圖網版權所有   聯系電話:0512-65154990  

        備案號:蘇ICP備12009002號-6   經營許可證:蘇B2-20200052  蘇公網安備:32050602011098


        本站為文檔C2C交易模式,即用戶上傳的文檔直接被用戶下載,本站只是中間服務平臺,本站所有文檔下載所得的收益歸上傳人(含作者)所有。裝配圖網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對上載內容本身不做任何修改或編輯。若文檔所含內容侵犯了您的版權或隱私,請立即通知裝配圖網,我們立即給予刪除!

        特级毛片a片全部免费播,特级毛片a片全部免费观看,特级毛片免费无码不卡观看,特级全黄a片高清视频

        <tt id="a3jom"></tt>
        1. <tt id="a3jom"><noscript id="a3jom"></noscript></tt>

            <tt id="a3jom"></tt>