c語言經(jīng)典例題(Clanguageclassicexamples)_第1頁
c語言經(jīng)典例題(Clanguageclassicexamples)_第2頁
c語言經(jīng)典例題(Clanguageclassicexamples)_第3頁
c語言經(jīng)典例題(Clanguageclassicexamples)_第4頁
免費(fèi)預(yù)覽已結(jié)束,剩余29頁可下載查看

下載本文檔

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

文檔簡(jiǎn)介

c語言經(jīng)典例題(Clanguageclassicexamples)writeafunctionandcalltosolveasmallballfromHmetersheightfreefall,eachtimeafterfallingdown,reboundbacktotheoriginalheightofhalf,thenfall,askitintheNlanding,howmanymeters?VoidSM(floats)!Inti;Floath=100;For(i=l;i<=10;i++)IH=h/2;S=s+h*2;)Printf("\nh=%f\ns=%f\n","H","s");Return(s);Floatsum;SM(sum);)writeafunctionandcall,findthesequenceof2/1,3/2,5/3,8/5,13/8.21/13 Findthesumoftheprecedingnitems.Voidsum(floats){Int,a=2,b=l,n=2,t;S=a/b;While(nく=20)(T=a;A=a+b;S=s+(float)a/b;N++;)Printf("\ns=%f\n”,s);Return(s);)(main)IFloatsu;Sum(Su);)writeafunction,askS=a+aa+aaa+aaaa+ +aaa...ItemninAAAA(na),whichcalculatesthevalueofSbycalling.#include<stdio.h>#include<math.h>Int}a,N,t,I,s=0;Sum=O;Scanf(〃%d",〃%d",〃&a","&n");For(i=l;i<=n;i++){T=a*pow(10,(i-1));S=s+t;Sum+=s;)Printf('\nsum4d\n",sum);Return(sum);)(main)Total(Su);writeafunction,fortherootoftheequationax2+bx+c=0,fromthemainfunctioninputa,B,Cvalues.#include<stdio.h>#include<math.h>VoidABC(int,a,int,B,int,c){float,x,I,xl,x2;I=pow(B,2)-4*a*c;If(a==0){x=-(c/b);Printf("x=%f",x);}ElseIf(i〉=0)Xl=(-b+sqrt(I))/(2*a);X2=(-b-sqrt(I))/(2*a);Printf("xl=%.2f","x2=%.2f\n","XI","x2");)ElsePrintf("wrong\n");))(main)(Int,a,B,c;Scanf("%d,%d,%d",&a,&b,&c);ABC(a,B,C);)ヽwriteprograms,calculatethefollowingformulaandoutputtheresult.Requirements:(1)writeafunctiontocalculaten!writethemainfunction,bykeyboardinputnandm,call(1)inthefunctiontocompletethecalculation.enterNandmtogivehints,andcheckthereasonablenessofNandm,unreasonableinputshouldoutputwronginformation,andnolongercalculate.#include<stdio.h>Longpower(intn)ILongf;If(n>l)F=power(n-1)*n;ElseF=1;Return(f);Int,N,m;Longc;Scanf("%d",〃%d",〃&n","&m");If((n-m)>0)(empower(n)//(power(n-m)*power(m));Printf("c=%ld\n”,C);)ElsePrintf("pleaseput,into,new,number,\n");}writeafunction,entera4digitnumber,theoutputofthese4digitalcharacters,buteverytwonumbersemptyaspace.Iftheinputoutputshouldbe1990,"1"9"9"〇”.#include<stdio.h>Charnum(intn)Int,N1,N2,N3,n4;N4=n%10;N3=n/10%10;N2=n/100%10;Nl=n/1000;Printf(%d,%d,%d,%d\n,N1,N2,N3,N4);)(main)Intn;Charnuml;Scanf("%d",&n);Numl=num(n);Printf numl);writeafunctionandcalltodeterminea5digitnumber,isnotpalindromenumber.Thatis,12321isthepalindromenumber,thebitsarethesameasthetens,thetenbitsarethesameasthethousand,andthenprinttheresult.N(int,a){Int,Al,A2,A3,A4,a5;A5=a%10;A4=a/10%10;A3=a/100%10;A2=a/1000%10;Al=a/10000;If(a5==alanda4==a2)Printf(The,num,%d,'huiwen'\n,is,a);ElsePrintf("Itisnot'huiwen'\n");(main)Int,AA,num;Scanf &num);Aa=n(Num);writeafunctionandcall,forafloatingpointnumberhinthevalueof2decimalplaces,andthirdforfourtofiveinto(specifythefloatingpointnumberispositive).Forexample,theHvalueis8.32433,andthefunctionreturns8.32;theHvalueis8.32533,thenthefunctionreturns8.(becausetheprintffunctionitself,whenprinted%.2fcanbefourtofive,theproblemcannotbedirectlyentered,printed,andtheneedtointerceptthenumberofoperations,andthenoutput)#include<stdio.h>Voidn(floata)Int,B,C,e;Floatd;B=(a*100〇)/10;C-b/10;E=b%10;If(e>5)(C-c+1;D=c?〇.01;}ElseD=c?0.01;Printf(the,num,is,%f\n,D);Floata;Scanf &a);N(a);)writeafunctiontochangeacharacterfromitsthirdcharacters(example:entera,becomeC,enterB,d),enteralineofcharactersfromthekeyboard,andconvertbyfunctioncall.#include<stdio.h>VoidCH(charc)(While((c=getchar())-='\n')(If(c〉='a'&&cく='x'c>='A'&&cく='X'II)C=c+2;Elseif(c==,y)||c=='z'||c=='Y'||c=='Z')C=c-24;Printf("祝",C);)Return?;)(main)(Chara;A=getchar();CH(a);)twotabletennisteamcompetition,eachoutof3people.AteamofA,B,C,BteammembersX,Y,Z,havedrawnlotstodecideonthelistofmatches.Someoneaskstheteamforalistofthegames,andAsayshedoesn'tplaywithX.Csayshedoesn'tplaywithXorZ.Pleaseprogramtofindalistofthe3matchplayers.Char,jia[3]={'A','B','C'};Char,yi[3]={'X','Y','Z'};Int,I,j;Printf("\nthelistis:\n");For(i=0;i<3;i++)For(j=0;j<3;j++)If(i==0j==2IIi==2j==0i==2||&&&&&&j==2)JElsePrintf("%c:%c\n”,jia[i],yi[j]);)n(n>3)personalcircle,orderarranging.Fromfirsttostartoff(from1to3report),whoreported3peopleoutofthecircle,leavingthelastquestionistheoriginalnumber.Thevalueofnisenteredfromkeyboard.Inta[50];Int,I,N,num,counter;Printf("\nPleaseinputn:\n");Scanf &n);For(i=l;i〈=n;i++)1=1,num=0,counters;While(counter>l)[If(i==n+l)1=1;If(a[i]==l)Num++;If(num==3)A[i]=0,num=0,counter一;For(i=l;i<=n;i++)If(a[i]==l)Printf(\nThe,last,one,is,%d\n,No.,I);)Enterastringandcopytheuppercaseletterstoanotherstring.(main){char,strl[50],str2[50];Int,i=0,j=0;Printf("\npleaseinputastring:\n");Scanf("%s”,&strl);While(strl[i],=‘、〇'){if(strl[i]>='A'andstrl[i]く='Z'){str2[j]=strl[i];J++;1++;)Str2[j]='\0';Printf(\nthe,first,string,is:%s\nthe,second,string,is:%s\n,STR1,STR2);)pocketred,green,white,black,4colorsoftheballafew.Eachtimeyoutake2ballsoutofyourpocket,askforthepossibilityofgetting2differentcolors,andoutputeachpermutation.(main){int,Bl,B2,num=0;Printf("num\tballl\tball2:\n");For(bl=l;blく=4;bl++)For(b2=l;b2く=4;b2++)If(B2,=bl){num++;Printf("%d\t",num);Switch(Bl){case1:printf("red\t");break;Case2:printf("green't");break;Case3:printf("white't");break;Case4:printf("black't");break;)Switch(B2){case1:printf("red't");break;Case2:printf("green't");break;Case3:printf("white't");break;Case4:printf("black't");break;Printf("\n")Printf("\ntotal:%d\n”,num);Getch();)aboutprimenumbers.enteranumbertodeterminewhetheritisaprimenumberornot.#include<math.h>#includesstdio.h><sushu(intm)Iinti,k;printf("pleaseinputnumberbiggerthanzero:");scanf("%d",a&m);k=(int)sqrt(m).for(i=2;the<=k;i++)if(m%i==0)break;if(i>k)printf("%dissushu\nm).elseprintf('%disn'tsushu\n',m).)main()(intnum;sushu(num);}(2)、求n以內(nèi)所有的素?cái)?shù).#includesmath,h><

#includesstdio.h><sushu(intn)inti,k,m=1.printf("ヽprintf("ヽnpleaseinputnumberbiggerthanone:");scanf("%d",&n).for(m=1,m<=n;m++)k=(int)sqrt(m).for(i=2;the<=k;i++)if(m%i==0)break;if(i>k)printf("%d\t"m);elsem++;main()!intnum;sushu(num);)15、現(xiàn)有一個(gè)整數(shù)數(shù)組,其中的元素按照從小到大的順序已經(jīng)排好序,現(xiàn)再輸入ー個(gè)數(shù),要求按原來的規(guī)律將它插入數(shù)組中.(注意:插入數(shù)的時(shí)候,需要將要插入位置及以后的內(nèi)容向后移動(dòng)ー個(gè)位置,空出當(dāng)前位置放置新的數(shù)值)main()(int[11]1,4,6,9,13,16,19,28,40100-{};inttempi,temp2,number,end,i,j;printf(originalarrayis:\n);for(i=0;i<10;i++)printf("%5d",[]);printf("ヽn");printf(insertanewnumber:,z);scanf("%d”,&number);end=[9].if(number>end)in[10]=number;elsefor(i=0;{<10;i++){if(a[i]>number){tempi=a[i].[i]=number;for(j=i+1,j<11;j++)temp2=a[j].a[j]=tempi;tempi=temp2;}break;)))for(i=0;i<11;i++)printf("%6d",[]);)16、輸入行數(shù),打印n行的ー個(gè)由數(shù)字組成的三角形,三角型如下11211232112345654321123456765432112345678765432112345678987654321#includesstdio.h>くmain(){intn,i,j;scanf("%d”,&n).for(i=1,theく=n;i++)for(j=1;j<=n-1;j++)printf(つ.for(j=1;j<=i;j++)printf("%d”,j).for(j=1;j>0;i—)printf("%d”,j).printf("ヽn");))17、編寫ー個(gè)函數(shù),輸入n為偶數(shù)時(shí),調(diào)用函數(shù)求1/2+1/4+...+1/n,當(dāng)輸入n為奇數(shù)時(shí),調(diào)用函數(shù)1/1+1/3+...+1/nfl(intx)[floaty=0;inti;if(x!=1)if(x%2==0)for(i=1;i<x;i++)d=s+1.0/(2*);)if((x%2).=0&&x!=1)!for(i=1;i<x;i++)d=s+1.0/(2*1).))elsed=1;printf("thetotalis$.2f\n",s);main()intn;printf("ヽnpleaseinputsthenumberis").scanf("%d",&n).fl(n).)18、編寫ー個(gè)函數(shù),分別對(duì)整型數(shù)組的下標(biāo)為偶數(shù)的元素和下標(biāo)為奇數(shù)的元素求和main(){inti,js=0,f=0.intarr[10]={1,2,3,4,5,6,7,8,9,10);for(i=0;i<10;i++)if(i%2==0)os=os+arr[i].elsejsjs+=arr[i].printf("\njs=%dos=%d",js,os).19、編寫ー個(gè)程序,實(shí)現(xiàn)兩個(gè)字符串的連接includesstdio.h><main(){charal[50],a[50].inti,j;printf

溫馨提示

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

評(píng)論

0/150

提交評(píng)論