res-轉(zhuǎn)載-2012春學(xué)期語言上機練習(xí)參考答案_第1頁
res-轉(zhuǎn)載-2012春學(xué)期語言上機練習(xí)參考答案_第2頁
res-轉(zhuǎn)載-2012春學(xué)期語言上機練習(xí)參考答案_第3頁
res-轉(zhuǎn)載-2012春學(xué)期語言上機練習(xí)參考答案_第4頁
res-轉(zhuǎn)載-2012春學(xué)期語言上機練習(xí)參考答案_第5頁
免費預(yù)覽已結(jié)束,剩余64頁可下載查看

下載本文檔

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

文檔簡介

第1周(1-4題略 顯示"ProgramminginCis 顯示圖案(復(fù)習(xí)printf()的字符串輸出 第2 20011求華氏溫度100°F對應(yīng)的攝氏溫度 20012求華氏溫度150°F對應(yīng)的攝氏溫度 求攝氏溫度26°C對應(yīng)的華氏溫 計算三門課程的平均成 20015當n為152時,分別求出n的個位數(shù)字(digit1)、十位數(shù)字(digit2)和百位數(shù)字(digit3)的值 20016計算x的平方(改錯題 20021計算分段函數(shù)的值(調(diào)試示例 20023計算存款利 20024計算分段函 200262num1num2數(shù) 第3 求1+2+3+......+100(調(diào)試示例 求 求 求1-1/4+1/7-1/10+……的前n項之 求x的n次 生成3的乘方 求 計算物體自由下落的距 求 階梯電 第4 20025計算分段函數(shù)的值(改錯題 20027計算旅途時間 20028數(shù)字加 求1+1/3+1/5+......的前n項 輸出華氏-攝氏溫度轉(zhuǎn)換表(改錯題 使用函數(shù)求n!/(m!*(n- 求1-2/3+3/5-4/7+5/9- 求 求 第5 求一元二次方程的 求分段函數(shù)的 顯示五級記分制成績所對應(yīng)的百分制成績區(qū)間(使 顯示水果的價格(使用 求三角形的面積和周 計算個人所得 統(tǒng)計學(xué)生成 分段計算水費(使用嵌套的if-else語句 出租車計 30062輸出21世紀所有閏 第6 求最小公倍數(shù)和最大公約數(shù)(調(diào)試示例 求 求整數(shù)的位 求 換硬 找出各位數(shù)字的立方和等于它本身的 找完數(shù)(改錯題 驗證歌德巴赫猜想(選作 從高位開始逐位輸出一個整數(shù)的各位數(shù)字(選作 求平均成績并統(tǒng)計不及格人 第7 使用函數(shù)判斷數(shù)的符 使用函數(shù)求奇數(shù) 使用函數(shù)統(tǒng)計素數(shù)并求 使用函數(shù)統(tǒng)計一個整數(shù)中數(shù)字的個 使用函數(shù)找水仙花 使用函數(shù)求1!+2!+…+m!(改錯題 使用函數(shù)求余弦函數(shù)的近似 使用函數(shù)找最大 使用函數(shù)輸出指定范圍內(nèi)的Fibonacci 使用函數(shù)找出指定范圍內(nèi)的完 第8 求x+x*x/2!+x*x*x/3!+x*x*x*x/4!+……的 使用函數(shù)計算兩點間的距 50051數(shù)字金字 使用函數(shù)求 使用函數(shù)求PI(調(diào)試示例源程序 整數(shù)的十進制、八進制和十六進制表現(xiàn)形 分類統(tǒng)計字 使用函數(shù)輸出整數(shù)的逆序 統(tǒng)計單 簡單計算 1周(1-4題略 顯示"ProgramminginCis編寫程序,在屏幕上顯示一個短句"ProgramminginCisintmain(void){printf("ProgramminginCisfun!");return0;} 顯示圖案(printf()的字符串輸出*******intmain(void){printf("***"***\n" * }220011求華氏溫度100°F對應(yīng)的攝氏溫度。#include#include<stdio.h>intmain(void){intcelsius, printf("fahr=%d,celsius=%d\n",fahr,}}20012150°F計算公式:C5*F/9-5*32/9,式中:C表示攝氏溫度,F(xiàn)表示華氏溫度。intmain(void){intcelsius,celsius=5*fahr/9-printf("fahr=%d,celsius=%d\n",fahr,celsius);return0;} 求攝氏溫度26°C對應(yīng)的華氏溫度攝氏溫度,f表示華氏溫度。celsius=26,fahr=#include#include<stdio.h>intmain(void){intcelsius, celsius=26;fahr=fahr=9*celsius/5+printf("celsius=%d,fahr=%d\n",celsius,} 計算三門課程的平均成績math=87,eng=72,comp=93,average=intmain(void){intmath,eng,comp,average;math=87;eng=72;comp=93; average=printf("math=%d,eng=%d,comp=%d,average=%d\n",math,eng,comp,}intmain(void){printf("整數(shù)%d的個位數(shù)字是%d,十位數(shù)字是%d,百位數(shù)字是%d\n",n,digit2,return}x的值為3xy,并分別以"yx*x"和"x*xy"xy的9=3*3*3=#include#include<stdio.h>intmain(void){printf("%d=%d*%d\n",y,x,x);printf("%d*%d=%d\n",x,x,y);return0;}20021計算分段函數(shù)的值(error02_3)輸入一個正整數(shù)repeat(0<repeat<10),做repeat次下列運算:輸入x,計算并輸出下列分段函數(shù)f(x)的值(1位小數(shù))。x0時,yf(x)1/xx0時,yf(x0。 f(10.00)=f(0.00)=#include#include<stdio.h>intmain(void){intrepeat,ri;doublex,scanf("%d",&repeat);for(ri=1;ri<=repeat;ri++){scanf("%lf",if(x!=0)y=1/x; printf("f(%.2f)=%.1f\n",x,}}20023輸入存款金額moneyyearrate,根據(jù)下列公式計算存款到期時的利息interest(稅前),輸出時保留2位小數(shù)。100030.025(money=1000,year=3,rate=interest=#include#include<stdio.h>#include<math.h>intmain(void){intmoney,year;doubleinterest,rate;interest=money*pow((1+rate),year)-money;printf("interest=%.2f\n",interest);}20024xf(x)的值(2位小數(shù))sqrt函數(shù)求平方根,調(diào)用pow函數(shù)求冪。x0時,f(xx^0.5x0時,f(xx+1)^22x1/x。-0f(10.00)=f(-0.50)=-f(0.00)=#include#include<stdio.h>#include<math.h>intmain(void){intrepeat,doubledoublex,scanf("%d",&repeat);for(ri=1;ri<=repeat;ri++){if(x>=0)y=elsey=pow((x+1),2)+2*x+1.0/x;printf("f(%.2f)=%.2f\n",x,y);}}200262num1num2printf("%d%d%d\n",num1,num2,5 5+3=85-3=25*3=5/3=15%3=2#include#include<stdio.h>intmain(void){int printf("%d+%d=%d\n",num1,num2,num1+num2);printf("%d-%d=%d\n",num1,num2,num1-num2);printf("%d*%d=%d\n",num1,num2,num1*num2);printf("%d/%d=%d\n",num1,num2,num1/num2);printf("%d%%%d=%d\n",num1,num2,num1%num2);return}3 求1+2+3100(調(diào)試示例123100sum=intmain(void){inti,sum;} 求 0+1+2+. (計算 (計算 sum=sum=sum=intmain(void){inti,m,sum;for(ri=1;ri<=ri++){scanf("%d",&m);sum=0; }} 求 2mn(m<=n),1/m1/(m+1)1/(m+2) 5101sumsumsumintmain(void){inti,m,n;doublesum;for(ri=1;ri<=ri++){&m,&n);sum=0;for(i=m;i<=n;i++)}} 求1-1/4+1/7-1/10+……的前n項之n1-1/4+1/7-1/10n留3位小數(shù)。 3sum=sum=intmain(void){intflag,i,n,t;intrepeat,doubleitem,for(ri=1;ri<=ri++){scanf("%d",&n); }}intmain(void){intflag,i,n,t;intrepeat,ri;doubleitem,for(ri=1;ri<=ri++){scanf("%d",&n);{}}} xn不允許調(diào)用pow函數(shù)求冪。222intmain(void){inti,intrepeat,ri;for(ri=1;ri<=ri++){scanf("%lf%d",&x,&n);mypow=1; printf("%.2f\n",}} 3計算3的乘方。printf("pow(3,%d)%.0f\n",i, =1=3=9=#include#include<stdio.h>#include<math.h>intmain(void){inti,doublescanf("%d",&n);for(i=0;i<=n;i++)}} sum=#include#include<stdio.h>#include<math.h>intmain(void){int } 計算物體自由下落的距離intmain(void){doubleheight=0.5*10*3* } 求 編寫程序,輸入一個正整數(shù)n,求 的前n項之和,輸出 sum=sum=intmain(void){inti,doublesum;for(ri=1;ri<=ri++){scanf("%d",}}} 階梯電價留2位小數(shù)。 cost=cost=intmain(void){intrepeat,ri;doublecost,for(ri=1;ri<=ri++){scanf("%lf",&e); }}420025計算分段函數(shù)的值(error02_4)xf(x)1位小數(shù)。當x10時,y=f(x)=x,當x10時,y=f(x)=1/x。 f(10.0)=f(234.0)=#include#include<stdio.h>intmain(void){intrepeat,ri;doublex,for(ri=1;ri<=repeat; printf("f(%.1f)=%.1f\n",x,}}20027計算旅途時間。輸入2個整數(shù)time1time2,表示火車的出發(fā)時間和到達時間,計算并輸出旅途時間。Thetrainjourneytimeis6hours59#include#include<stdio.h>intmain(void){inttime1,time2,hour,minutes,t1,t2,result;scanf("%d%d",&time1,&time2);t1=(time1/100)*60+time1%100; /*minutes*/t2=(time2/100)*60+time2%100;result=t2-hour=result/60;minuteshour=result/60;minutes=result%60;printf("Thetrainjourneytimeis%dhours%dminutes.\n",hour,}#include<stdio.h>intmain(void)#include<stdio.h>intmain(void){inttime1,time2,hour,intth1,th2,tm1,tm2; //printf(“\n”)也能通過?scanf("%d%d",&time1,&time2);}}printf("Thetrainjourneytimeis%dhours%dminutes.\n",hour,minutes);return0;}20028#include#include<stdio.h>intmain(void){intnumber,digit1,digit2,digit3,digit4, /*取個位+910取余,然后變成百位(個位與百位交換)/*取百位+910取余,變成個位,百位和個位上的數(shù)字互換*//*取千位+910printf("Theencryptednumberis%d\n",newnum);return0;}#include<stdio.h>intmain(void){#include<stdio.h>intmain(void){intdigit1,digit2,digit3,digit4,newnum,number; /*個位*/ /*十位*/ 百位*/ /*千位*/printf("Theencryptednumberis%d\n",newnum);return0;} 求1+1/3+1/5 的前n項n,11/31/5n出時保留6位小數(shù)。 (計算 sum=sum=#include#includeintint{inti,intrepeat,ri;doublesum;for(ri=1;ri<=ri++){scanf("%d",for(i=1;i<=n;i++){}}} 輸出華氏-攝氏溫度轉(zhuǎn)換表(改錯題取值范圍是[lower,upper],每次增加2F。計算公式:c5*(f32)9,其中:c,f輸出請使用語句printf("%3.0f%6.1f\n",fahr,celsius); 32 40 fahr fahrintmain(void){intrepeat,ri;doublecelsius,for(ri=1;ri<=&upper);printf("fahr{celsius=5*(fahr-32)/9; }} nm!*n-2m和n(m<=n),計算n!/(m!*(n-m)!)。是double。2 (m=2,5 (m=5,result=result=intmain(void){intm,doubles;doublefact(intn);for(ri=1;ri<=ri++){scanf("%d%d",&m,printf("result=%.0f\n",s);}}{returnresult;} 求 m和n(m<=n),求sum=312255===intmain(void){inti,m,n;doublesum;for(ri=1;ri<=repeat;ri++){scanf("%d%d",&m,&n); }} 求1-2/3+3/5-4/7+5/9-出時保留3位小數(shù)。 sum=sum=sum=intmain(void){intflag,i,intrepeat,ri;doubleitem,sum;for(ri=1;ri<=ri++){scanf("%d",&n);for(i=1;i<=n;{item=flag*i*1.0/denominator;} }} #include#include<stdio.h>int{{intintrepeat,ri;doublesum;for(ri=1;ri<=ri++){scanf("%d",&n); }} n,e0!+1!+2!+……+n!,要求定義和調(diào)用函數(shù)fact(n)計算n!,函數(shù)類型是double。 124sum=sum=sum=intmain(void){intintrepeat,ri;doublesum;for(ri=1;ri<=ri++){scanf("%d",i);sum=sum+fact(i);} }}{intj;}5 求一元二次方程的根printf("x=%0.2f\n",-c/b);printf("x1=%0.2f\n",(-printf("x2=%0.2f\n",(-b-printf("x1=%0.2f+%0.2fi\n",-b/(2*a),sqrt(-printf("x2=%0.2f-%0.2fi\n",-b/(2*a),sqrt(-500000102413ab0,c0,方程不成立x=-2.00x1=-x2=-x1=-1.00+1.41ix2=-1.00-#include#include<stdio.h>#include<math.h>intmain(void){intrepeat,ri;doublea,b,c,d;for(ri=1;ri<=ri++){scanf("%lf%lf%lf",&a,&b,&c);d=b*b-4*a*c;if(c==0)printf("參數(shù)都為零,方程無意義}elseprintf("x=%0.2f\n",-}printf("x1=%0.2f\n",(-}}}}}參考#include#include<stdio.h>#include<math.h>intmain(void){intrepeat,ri;doubleintrepeat,ri;doublea,b,c,d;for(ri=1;ri<=ri++){scanf("%lf%lf%lf",&a,&b,&c);d=b*b-4*a*c; printf("參數(shù)都為零,方程無意義!\n"); printf("x=%0.2f\n",-c/b);printf("x1=%0.2f\n",(-}}}} 求分段函數(shù)的值repeat(0<repeat<10),repeat輸入一個整數(shù)x,計算并輸出下列分段函數(shù)sign(x)的值。- x<y=sign(x)= x= x> - (x=-sign(10)=1 (x=10時y=1)sign(0)=0 (x=0時y=0)sign(-98)=-1 (x=-98時y=-1)intmain(void){intrepeat,intintx,for(ri=1;ri<=ri++){scanf("%d",&x);if(x==0)y=0;elseif(x>0)y=1;elsey=-1;}} 顯示五級記分制成績所對應(yīng)的百分制成績區(qū)間(使 repeat(0<repeat<10repeat和E(0-59),如果輸入不正確的成績,顯示"Invalidinput"。printf("90-printf("80-printf("70-printf("60-printf("Invalid 字符j)90-80-70-60-0-Invalidinputintmain(void){charintrepeat,for(ri=1;ri<=repeat;ri++){for(ri=1;ri<=repeat;ri++){ch=getchar();}}return} 顯示水果的價格(使用55輸入其他編號,顯示價格為0。 price=[0]intmain(void){intchoice,i;for(i=1;i<=5;i++){printf("[1]printf("[0]Exit\n");if(choice==0)case1:casecasecase4:}price=3.00;}}}30007repeat(0<repeat<10repeat輸入三角形的3a,b,c,如果能構(gòu)成一個三角形,輸出面積area和周長perimeter(2Thesesidesdonotcorrespondtoavalidtriangle"。在一個三角形中,任意兩邊之和大于第三邊。三角形面積計算公式:area(s(s-a)(s-b)(s-c))^0.5,其中s=(a+b+c)/2printf("area=%.2f,perimeter=%.2f\n",area,printf("Thesesidesdonotcorrespondtoavalid25514area=7.15,perimeter=Thesesidesdonotcorrespondtoavalid#include#include<stdio.h>#include<math.h>intmain(void){inta,b,c;for(ri=1;ri<=ri++){scanf("%d%d%d",&a,&b,if((a+b>c)&&(a+c>b)&&area=sqrt(s*(s-a)*(s-b)*(s-printf("area=%.2f,perimeter=%.2f\n",area,}elseprintf("Thesesidesdonotcorrespondtoavalid}} 計算個人所得稅repeat(0<repeat<10repeat計算公式:tax=rate*(salary-850)當salary≤850時,rate=當850<salary≤1350時,rate=當salary≤2850,rate當salary5850,rate當<salary時,rate= =====intmain(){intri,doublerate,salary,scanf("%d",&repeat);{scanf("%lf",&salary);if(salary<=850) }elseif{rate=0.05;}elseif{rate=0.10;}elseif{rate=0.15;}{rate=0.20;}printf("tax=%0.2f\n",}}} 統(tǒng)計學(xué)生成績成績等級分為五級,分別為A(90-100)、B(80-89)、C(70-79)、D(60-69)和 77549273NumberofA(90-100):1NumberofB(80-89):0NumberofC(70-79):2NumberofD(60-69):1NumberofE(0-59):1intmain(void){intmark,n,intfor(i=1;i<=n;i++){ if(mark>=90&&mark<=100)elseif(mark>=80&&mark<=89)nb++;elseif(mark>=70&&mark<=79)nc++;elseif(mark>=60&&mark<=69)nd++;elsene++;}printf("NumberofB(80-89):%d\n",nb);printf("NumberofC(70-79):%d\n",nc);printf("NumberofD(60-69):%d\n",nd);printf("NumberofE(0-59):%d\n",ne);return0;} 分段計算水費(使用嵌套的if-else語句y(x(數(shù))。要求用嵌套的if-else語句。 y=f(x)= 2.5x- - (x=- f(-0.50)=f(9.50)=f(21.30)=intmain(void){doublex,y;scanf("%d",&repeat);for(ri=1;ri<=repeat;ri++){ elsey=2.5*x-10.5;printf("f(%.2f)=%.2f\n",x,y);}} 出租車計費repeat(0<repeat<10repeat1.62 6 11.8230 cost=cost=cost=intmain(void){intrepeat,intminutes,seconds;doublecost,mile;scanf("%d",&repeat);for(ri=1;ri<=repeat;ri++){scanf("%lf%d%d",&mile,&minutes, elseif(mile<=10) printf("cost=%.0f\n",}3006221400intmain(void){intfor(year=2000;year<=2099;year++){if((year%4==0&&}6 求最小公倍數(shù)和最大公約數(shù)(調(diào)試示例repeat(0<repeat<10),repeat輸入兩個正整數(shù)m和n,輸出它們的最小公倍數(shù)和最大公約數(shù)。 3 24 24 21istheleastcommonmultipleof3and7,1isthegreatestcommondivisorof3and7.24istheleastcommonmultipleof24and4,4isthegreatestcommondivisorof24and4.72istheleastcommonmultipleof24and18,6main(void){intgcd,lcm,m,n,c;intrepeat,ri;scanf("%d",&repeat);for(ri=1;ri<=repeat;ri++){for(lcm=1;} lcmprintf("%distheleastcommonmultipleof%dand%d,%disthegreatestcommondivisorof%dand%d.\n",lcm,m,n,gcd,m,n);}return}intmain(void){intgcd,lcm,m,intintrepeat,scanf("%d",for(ri=1;ri<=repeat;{scanf("%d",scanf("%d",if(m<=n)c=m; elsec=n;for(gcd=c;;gcd--)}lcm=printf("%distheleastcommonmultipleof%dand%d,%disgreatestcommondivisorof%dand%d.\n",lcm,m,n,gcd,m,}return} eps(保留6位小數(shù))。請使用while語句實現(xiàn)循環(huán)。2E- (eps=2E- sum=sum=#include#include<stdio.h>#include<math.h>intmain(void){intdenominator,flag;intrepeat,ri;doubleeps,item,for(ri=1;ri<=repeat;}}return} 求整數(shù)的位數(shù)repeat(0<repeat<10repeat - (in=-- (in=- count= count= count= count= intmain(void){intcount,in;for(ri=1;ri<=ri++){scanf("%d",if(in<0)in=-in;}while}return} repeat(0<repeat<10repeat輸入一個正整數(shù)n,輸出2/1+3/2+5/3+8/5n項之和,保留2位小數(shù)。(該序列從第2項起,每一項的分子是前一項分子與分母的和,分母是前一 sum sum sum= intmain(void){inti,intrepeat,for(ri=1;ri<=ri++){scanf("%d",sum+=tempdenominator*}}} 換硬幣repeat(0<repeat<10repeat將一筆零錢(大8分1精確到分)換521分的硬幣。要求:硬幣面值按5分、2分、1分順序,各類硬幣數(shù)量依次從大到小的順序,輸出使用語句:printf("fen5:%d,fen2:%d,fen1:%d,total:%d\n",fen5,fen2,fen1,fen5+fen2+fen1); count=2 count= intmain(void){intcount,fen1,fen2,fen5,money;intrepeat,ri;for(ri=1;ri<=ri++){scanf("%d",&money);printf("fen5:%d,fen2:%d,fen1:%d,total:%d\n",fen5,fen2,fen1,fen5+fen2+fen1);}return} 找出各位數(shù)字的立方和等于它本身的數(shù)repeat(0<repeat<10repeatmn(1<=m,n<=1000),mn輸出使用語句:printf("%d\n", 100 (m=100,1 (m=1, intmain(void){inti,digit,m,n,a,b,c,sum;intrepeat,ri;for(ri=1;ri<=repeat;ri++){scanf("%d%d",&m,&n);}}return}intmain(void){inti,digit,m,n,number,sum;intrepeat,ri;for(ri=1;ri<=repeat;ri++){scanf("%d%d",&m,&n);}}}} 找完數(shù)(改錯題repeat(0<repeat<10repeatmn(1<=m,n<=1000),mn其中1、2、3為因子,6為因子和。printf("%d=1",printf("+%d",factor);1 (m=1,400 (m=400,1=6=1+2+ 28=1+2+4+7+496=1+2+4+8+16+31+62+124+intmain(void){intfactor,m,n,number,sum;intrepeat,ri;for(ri=1;ri<=repeat;ri++){scanf("%d%d",&m,&n);for{forif(sum==number){printf("%d=forif(number%factor==0)printf("+%d",factor);}}}} 驗證歌德巴赫猜想(選作mn(6<=m,n<=100),mn數(shù)之和,打印時一行打印5組。輸出使用語句:printf("%d=%d+%d",number,i,number-89100(m=90,90=7+8392=3+8994=5+8996=7+89#include"math.h"intmain(void){intcount,i,j,k,m,n,scanf("%d%d",&m,&n);if(m%2!=0)m=m+1;if(m>=6){ }}intprime(intm){inti,ifPrime=0;if(m==1)return0;}return}int{intcount,i,m,n,if(m%2!=0)m=m+1;if(m>=6){ prime(i)&&prime(number-i){printf("%d=%d+%d",number,i,number-i);count++;}}}}} 從高位開始逐位輸出一個整數(shù)的各位數(shù)字(選作輸入一個整數(shù)in,從高位開始逐位分割并輸出它的各位數(shù)字。輸出使用語句:printf("%-2d",digit); - (in=- 2345608intmain(void){intdigit,in,power,temp,k;intrepeat,ri;for(ri=1;ri<=repeat;if(in<0)in=-in;k={k=k*} /*temp{temp--;k=digit=power/power=power%k; /*power*/}}return} 求平均成績并統(tǒng)計不及格人數(shù)678873548287437056100-Gradeaverageis72.80,thenumberoffailis1Gradeaverageis25.38,thenumberoffailis#include#includeintint{intfail,num;intrepeat,ri;doublegrade,for(ri=1;ri<=repeat;ri++){scanf("%lf",&grade);if(num>0)printf("Gradeaverageis%.2f,thenumberoffailis%d\n",total/num,fail);}return}7 使用函數(shù)判斷數(shù)的符號repeat(0<repeat<10repeat1x,x0,sign(x)1x0,sign(x)0;否則,sign(x)=-1,最后輸出sign(x)的值。類型是int。 - (x=- sign(10)=1 (x=10時sign(x)的值為1)sign(-5)=- sign(0)= ①intsign(intx);int{intx,intrepeat,for(ri=1;ri<=ri++){}}intsign(int{intt=0; elseif(x==0) return} 使用函數(shù)求奇數(shù)0,函數(shù)形參n的類型是int,函數(shù)類型是int。 12961721019-Thesumoftheoddnumbersis30.Thesumoftheoddnumbersisinteven(intn);int{intn,sum;intn,sum;for(ri=1;ri<=ri++){sum=0;while(n>0){if(even(n)==0)}printf("Thesumoftheoddnumbersis%d.\n",}}inteven(int{intif(n%2==0) returnt;}50005repeat(0<repeat<10repeat2mn(1<=m,n<=500),mn數(shù)以及這些素數(shù)的和。素數(shù)就是只能被1和自身整除的正整數(shù),1不是素數(shù),2要求定義并調(diào)用函數(shù)prime(m)m是否為素數(shù),當m素數(shù)時返1,否則0,函數(shù)形參m的類型是int,函數(shù)類型是int。 1 (m=1,Count=4,sum= #include#include"stdio.h"#include"math.h"intmain(void){intcount,i,m,n,intintrepeat,intprime(intfor(ri=1;ri<=ri++){scanf("%d%d",&m,&n);sum=0;if /*1,for(i=m;i<=n;i++){if(}}printf("Count=%d,sum=%d\n",count,}}intprime(int{int if(m==1)returnresult=for(t=2;t<=m/2;t++)if(m%t==0)returnresult;} 使用函數(shù)統(tǒng)計一個整數(shù)中數(shù)字的個數(shù)repeat(0<repeat<10repeat數(shù)字digit的個數(shù)。digitnumberdigitint,函數(shù)類型是int。例如,countdigit(10090,0)的返回值是3。22(number=21252,-9(number=-1111,Number21252ofdigit2: (21252中有3intmain(void){intintmain(void){intcount,digit,in;intrepeat,ri;for(ri=1;ri<=repeat;ri++){scanf("%d%d",&in,&digit);printf("Number%dofdigit%d:%d\n",in,digit,}}{intdoif(t==digit)c++;returnc;}/*//while0050007使用函數(shù)找水仙花數(shù)repeat(0<repeat<10repeat2mn(1<=m,n<=1000),mnis(numbernumber自身,若相等則返回1,否則返回0,函數(shù)形參number的類型是int,函數(shù)類型是int。輸出使用語句:printf("%d\n", 100400(m=100,1 (m=1,result:(100400 result:(1100 intmain(void){inti,m,n;intis(intfor(ri=1;ri<=ri++){scanf("%d%d",&m,&n);for{}}}intis(int{intsum,res,t,j;while{}if(sum!=number) 1!2!+…m!(n!,nint,double。 1!+2!+...+5!=#include#include<stdio.h>doublefact(intn);intmain(void){inti,m;printf("1!+2!+...+%d!=%f\n",m,sum);}{int double (n==0)return1.0;return} 使用函數(shù)求余弦函數(shù)的近似值repeat(0<repeat<10repeat于e。cos(x)=為double,函數(shù)類型是double。20(e=0.001,-(e=0.0001,x=-sum=sum=-#include#includedoublefuncos(doublee,doublex);intmain(void){intrepeat,ri;doublee,sum,x;for(ri=1;ri<=ri++){scanf("%le%le",&e,&x);sum=funcos(e,x);printf("sum=%f\n",sum);}}doublefuncos(doublee,double{intflag,n; /*tn*/}return}求funcos(e,x)方法doublefuncos(doublee,double{intflag=1,n=0;doubledouble /*item*/}returnreturn} 使用函數(shù)找最大a、ba、bint,int。 5 (a=5,-1-10(a=-1,b=-1 (a=1,max(5,8)=max(-1,-10)=-max(1,1)=intmain(void){inta,b,maximum;intrepeat,ri;intmax(inta,intfor(ri=1;ri<=repeat;ri++){scanf("%d%d",& }}intmax(inta,int{int /*aif(c<b) /*b*/returnc; Fibonaccimn(1<=m,n<=10000),mnFibonacci序列(第一項起):11 5 1321nint,long。例如,fib(7)13。輸出使用語句:printf("%ld",f); 1 (m=1,20 (m=20,1000 (m=1000,11235 intmain(void){inti,m,n;longf;for(ri=1;ri<=repeat;ri++){&m,&n);f=1;i=1;while(f<=n){if(f>=m)printf("%d",f);}}}longfib(int{intlonga=1,b=1,if(n==1||n==2) for(i=3;i<=n;i++){}}50063repeat(0<repeat<10repeat輸入兩個正整數(shù)mn(1<=m,n<=1000),輸出mn要求定義并調(diào)用函數(shù)factorsum(number),它的功能是返number的因子和,函數(shù)形參number的類型是整型,函數(shù)類型是整型。例如,factorsum(12)的返回值是16(1+2+3+4+6)。輸出使用語句:printf("%d 20 (m=201 (m=1,28 16 intmain(void){inti,m,n;for(ri=1;ri<=ri++){scanf("%d%d",&m,&n);printf("%d",i);}}}{{ else{ t<=number-1也對}}return}8 求x+x*x/2!+x*x*x/3!+x*x*x*x/4!+……的輸入1個實數(shù)x,計算并輸出下最后一項的絕對值小于0.00001(保留2位小數(shù))。要求定義和調(diào)用函數(shù)fact(n)計算n的階乘,可以調(diào)用pow函數(shù)sx+x*x/2!+x*x*x/3!+x*x*x*x/4!+……301sss#include<stdio.h>#include<stdio.h>intmain(void){intintrepeat,ri;doubleitem,s,x;for(rifor(ri=1;ri<=ri++){scanf("%lf",&x);}printf("s=%.2f\n",}}{intdoubleproduct;product=1;returnproduct;} 使用函數(shù)計算兩點間的距離輸入一個正整數(shù)repeat(0<repeat<10),做repeat次下列運算:(x1,y1)(x2,y2),求這兩點之間的距離(2位小數(shù))dist(x1,y1,x2,y2)x1、y1、x2y2的類型都是double,函數(shù)類型是double。 10 (x1=10,200 (x2=200,輸#include<stdio.h>#include<stdio.h>doubledist(doublex1,doubley1,doublex2,doubley2);intmain(void){intrepeat,doubledoubledistance,x1,y1,x2,for(ri=1;ri<=ri++){scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);distance=dist(x1,y1,x2,y2);printf("Distance=%.2f\n",distance);}}doubledist(doublex1,doubley1,doublex2,double{doublea,b,c;returnc;} 數(shù)字金字塔數(shù)字金字塔,函數(shù)形參n的類型是int,函數(shù)類型是void。printf("printf("%d", (n=5時的數(shù)字金字塔2334445555 (n=2時的數(shù)字金字塔2intmain(void){intintintrepeat,ri;intnRow,nCol;for(ri=1;ri<=ri++){scanf("%d",for(nRow=1;nRow<= {for(nCol=1;nCol<=n-nRow;nCol++) for(nCol=1;nCol<=nRow*2-1;nCol++)printf("%d",nRow);}}} 使用函數(shù)求程序填空,不要改變與輸入輸出有關(guān)的語句。an,a+aa+aaa+aa…a(na)之和。要求定義并調(diào)用函fn(an),它的功能是返回aa…a(na)函數(shù)形an的類型輸 2 (a=2,8 (a=8,輸 intmain(void){inta,i,longsn;longfn(inta,intfor(ri=1;ri<=repeat;scanf("%ld%d",scanf("%ld%d",&a,&n);printf("%ld\n",}}/*思路:如2222=((2+2*10)*10)+2) *10) +2,是一個累加和*/longfn(inta,intn){}return} 使用函數(shù)求PI(調(diào)試示例源程序repeat(0<repeat<10),repeat輸入精度eps,根據(jù)下式求PI的值,直到最后一項小于eps。fact(n)n!,nint,函數(shù)類型是函數(shù)類型是double。 1E- (eps=1E-1E- (eps=1E-PI=PI=doubledenominator(intn);intmain(void){intintintrepeat,doubleeps,sum,item;for(ri=1;r

溫馨提示

  • 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論