華科周純杰c語(yǔ)言答案_第1頁(yè)
華科周純杰c語(yǔ)言答案_第2頁(yè)
華科周純杰c語(yǔ)言答案_第3頁(yè)
華科周純杰c語(yǔ)言答案_第4頁(yè)
華科周純杰c語(yǔ)言答案_第5頁(yè)
已閱讀5頁(yè),還剩44頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、第三章 程序和流程控制 1.輸入兩個(gè)整數(shù),輸出較大者。 #include<stdio.h>void main()int a,b; int max=0;printf("Please input two numbers:");scanf("%d%d",&a,&b);if(a<=b) max=b;else max=a;printf("The larger number is %dn",max);2.有3個(gè)整數(shù)a,b,c,由鍵盤(pán)輸入,輸出其中最大的數(shù)。#include<stdio.h>void m

2、ain()int a,b,c,max;printf("Please input three numbers:");scanf("%d%d%d",&a,&b,&c);if(a>b) if(a>c) max=a; else max=c;elseif(b>c) max=b; else max=c;printf("The largest number is %dn",temp);3.從1累加到100,用while語(yǔ)句。#include<stdio.h>#define N 100void m

3、ain()int i=1,sum=0;while(i<=N) sum+=i; i+;printf("sum:%dn",sum);4.已知a110,a2 3,an3an-1 an-2 ,求 a 的前十項(xiàng)。#include<stdio.h>#define N 10void main()int a20=10,-3;int i=0,m=0;for(i=2;i<N;i+) ai=3*ai-1+ai-2; m=N;printf("The %d numbers are:n",m);for(i=0;i<N;i+) printf("

4、a%d=%dn",i+1,ai); 5.輸入一個(gè)自然數(shù),判斷它是奇數(shù)還是偶數(shù)。 #include<stdio.h>void main()int a, temp;printf("Please input a numbers:n");scanf("%d",&a);temp=a%2;if(temp=0) printf("The number %d is an even!n",a);else printf("The number %d is an odd number!n",a);6.已知a1

5、 8,an an-1 bn ,b1 1,bn bn-1 3,求 a 前10項(xiàng)之和。 #include<stdio.h>#define N 10void main()int aN=8,bN=1,i,sum=0;for(i=1;i<N;i+) bi=bi-1+3; for(i=1;i<N;i+) ai=ai-1+bi; for(i=0;i<N;i+) sum+=ai; printf("The sum is:%dn",sum);7.有一個(gè)函數(shù) 寫(xiě)一程序,輸入x,輸出Y的值。 #include<stdio.h>void main()floa

6、t x,y;printf("Please input a float number x=:");scanf("%f",&x);printf("x=%f",x);if(x<1) y=x;else if(x>=10) y=3*x-11; else y=2*x-1; printf("The value of y is:%.3fn",y);8.給一個(gè)不多于5位的的正整數(shù),要求:求出它是幾位數(shù),分別打印出每一位數(shù)字,最后按照逆序打印各位數(shù)字,例如原數(shù)為321,應(yīng)輸出為123。 #include<st

7、dio.h>#define N 99999#define M 5void main()int i,j,k,m,bM;long int a,temp;printf("Please input a number:n");scanf("%ld",&a);if(a<0|a>N) printf("Error in input data!n");else temp=a; for(i=0;temp!=0;i+) temp=temp/10; m=i; printf("The number a=%ld have %d

8、 single numbersn",a,m); temp=a; for(j=0;j<m;j+) bj=temp%10; temp=temp/10; printf("The origal numbers are:n"); for(k=m-1;k!=-1;k-) printf("%d",bk); printf("n"); printf("every letter is:"); for(j=m-1;j>=0;j-) printf("%dt",bj); printf("n

9、"); printf("The inverted numbers are:n"); for(j=0;j<m;j+) printf("%d",bj); printf("n");9編寫(xiě)一猜數(shù)游戲程序,隨機(jī)產(chǎn)生某個(gè)整數(shù),從鍵盤(pán)反復(fù)輸入整數(shù)進(jìn)行猜數(shù),當(dāng)未猜中時(shí),提示輸入過(guò)大或過(guò)小.猜中時(shí),指出猜的次數(shù).最多允許猜20次. #include<stdio.h>#include<time.h>#include<stdlib.h>#define N 10void main() int a,b,i;

10、randomize(); a=random(N); printf("Please input number you guess:n"); for(i=0;i<20;i+) scanf("%d",&b); if(b=a) printf("Congratulation!nYour guessed number is right!n"); printf("The time(s) you have guessed is %d",i+1); break; else if(b>a) printf("

11、;Your number is too big!n"); else printf("Your number is too small!n"); printf("n"); 10.計(jì)算1999中能被3整除,且至少有一位數(shù)字是5的所有整數(shù)。 #include<stdio.h>#define N 999void main() int i,b,c,d; printf("The numbers are:n"); for(i=1;i<=N;i+) if(i%3) b=i/100; c=(i-100*b)/10; d=i%

12、10; if(b=5|c=5|d=5) printf("%dt",i); 11.輸入兩個(gè)整數(shù),求它們的最大公約數(shù)和最小公倍數(shù)。#include "stdio.h"void main()int a,b,c,d;int comYueShu;int comBeishu;printf("please input two number!");scanf("%d %d",&a,&b);c=a;d=b;int r;/求公約數(shù)算法while(c%d)r=c%d;c=d;d=r;comYueShu=d;comBeis

13、hu=a*b/comYueShu;printf("%d,%d",comYueShu,comBeishu);12.輸入一個(gè)整數(shù),求它包含又多少個(gè)2的因子。(例如,8含有3個(gè)2的因子,10含有一個(gè)2的因子,15不含有2的因子。)#include "stdio.h"void main()int a=0;int integerGene=0;printf("input a number!n");scanf("%d",&a);while(a%2=0)integerGene+;a/=2; printf("%dn

14、",integerGene);13.計(jì)算1!,2!,3!10!#include<stdio.h>#define N 10void main() int i; long int a=1; printf("The outcome are:n"); for(i=1;i<=10;i+) a*=i; printf("The result of %d! is %ldn",i,a); 14.猴子吃桃問(wèn)題:第一天吃掉總數(shù)的一半多一個(gè),第二天又將剩下的桃子吃掉一半多一個(gè),以后每天吃掉前一天剩下的一半多一個(gè),到第十天準(zhǔn)備吃的時(shí)候見(jiàn)只剩下一個(gè)桃子,

15、求第一天開(kāi)始吃的時(shí)候桃子的總數(shù)。#include<stdio.h>#define N 10void main() int i,x10; x9=1; for(i=9;i>=1;i-) xi-1=2*(xi+1); printf("The total numbers in the first day are:%dn",x0);15.輸入圓錐體的底半徑r和高h(yuǎn),計(jì)算出圓錐體的體積并輸出#include<stdio.h>#define PI 3.1415926void main() float r,h,v; printf("Please in

16、put the radius and the height:n"); scanf("%f%f",&r,&h); v=PI*r*r*h/3; printf("The volume of is %.6fn",v);16題#include "stdio.h"void main() int a,n; long tmp,SUM=0; do printf("nPlease input the number a="); scanf("%d",&a); while(!(a>

17、;=0 && a<=9); printf("nPlease input the number,it's value is not too big n="); scanf("%d",&n); tmp = a; while(n-) SUM += tmp; tmp = tmp * 10 + a; printf("na+aa+aaa+.=%ld(a= %d)n",SUM,a);17題#include "stdio.h"void main()unsigned char week720=&

18、quot;Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","saturday" unsigned char weekday;do printf("nPlease input the weeday(0 =<the value of weekday <= 6):"); scanf("%d",&weekday); while(!(weekday

19、 >= 0 && weekday <= 6); printf("n%d correspoind to %sn",weekday,weekweekday);18題#include "stdio.h"#include "math.h"void main() float x,y;float Result;int i = 1;printf("nPlease input the value x =");scanf("%f",&x); Result = 1.0; y =

20、x; do Result += y; y = (y*x)/(i + 1); i+;while(y > 1e-6);printf("nThe natural constant e, it's %f pow = %f",x,Result);19題#include "stdio.h"#include "math.h"void main() float a; float x1,x2; printf("nPlease input the number a="); scanf("%f",&am

21、p;a); x1 = x2 = a/2; do x1=x2; x2=(x1+(a/x1)/2; while(fabs(x2 - x1) > 1e-5); printf("nInput a namber %f,It's sqrt = %f",a,x2); 第5章課后編程習(xí)題參考答案2.已有一個(gè)已排好序的數(shù)組,現(xiàn)在輸入一個(gè)數(shù),要求按照原來(lái)排序的規(guī)則將它插入數(shù)組中。#include<stdio.h>#define N 7void main() int aN=1,5,16,45,87,120; int n,i,j,temp1,temp2,end; prin

22、tf("please enter a number:n"); scanf("%d",&n); end=aN-2; if(n>end) aN-1=n; else for(i=0;i<N-1;i+) if(n<ai) for(j=N-1;j>i+1;j-) aj=aj-1; ai=n; break; printf("put out the new array:n"); for(i=0;i<N;i+) printf("%4d",ai);3.打印"魔方陣",所謂魔方

23、陣是指這樣的方陣,它的每一行,每一列和對(duì)角線之和均相等。例如,三階魔方陣為: 8 1 6 3 5 7 4 9 2#include<stdio.h>void main()int a1616;int n,i,j,k;do printf("enter a number n(115):n"); scanf("%d",&n); while(!(n>0)&&(n<=15)&&(n%2=1); for(i=1;i<=n;i+)for(j=1;j<=n;j+)aij=0;j=n/2+1;a1j=

24、1;for(k=2;k<=n*n;k+)i=i-1;j=j+1;if(i<1)&&(j>n)i=i+2;j=j-1;elseif(i<1) i=n;if(j>n) j=1;if(aij=0)aij=k;elsei=i+2;j=j-1;aij=k;for(i=1;i<=n;i+)for(j=1;j<=n;j+)printf("%4d",aij); printf("n"); 4,有一篇文章,共有3行文字,每行有80個(gè)字符,要求分別統(tǒng)計(jì)出其中英文大寫(xiě)字母,小寫(xiě)字母,數(shù)字,空格以及其它字符的個(gè)數(shù)。#inc

25、lude<stdio.h>#include <string.h>void main()char s380;int i,j,up,low,num,spa,oth;up=low=num=spa=oth=0;for(i=0;i<3;i+)printf("enter the %d line:n",i+1);gets(si);for(j=0;j<80&&sij!='0'j+)if(sij>='A'&&sij<='Z')up+;else if(sij>=

26、'a'&&sij<='z')low+;else if(sij>='0'&&sij<='9')num+;else if(sij=' ')spa+;elseoth+;printf("up=%dn",up);printf("low=%dn",low);printf("num=%dn",num);printf("spa=%dn",spa);printf("oth=%dn",o

27、th);5.編寫(xiě)一個(gè)程序,將兩個(gè)字符串連起來(lái),不要用strcat函數(shù)。#include<stdio.h>#include <string.h>void main()char s1100,s2100;gets(s1);gets(s2);int i,j;i=j=0;while(s1i!='0')i+;while(s2j!='0')s1i+=s2j+;s1i='0'puts(s1);6.輸入每個(gè)學(xué)生的平均成績(jī)和姓名,將成績(jī)按照遞減順序排序,姓名做相應(yīng)的調(diào)整。輸出排序后的成績(jī)和姓名。#include<stdio.h>#

28、include<string.h>define N 10/* 假定有十個(gè)學(xué)生*/void main()char name5010,name110;int markN,i,j,mark1;for(i=0;i<N;i+)printf("the %d th student's name and mark:n",i+1);scanf("%s%d",namei,&marki); for ( i=0;i<N-1;i+) for (j=i+1;j<N;j+) if(markj>marki) mark1 = markj

29、; markj = marki; marki = mark1; strcpy(name1,namej); strcpy(namej,namei); strcpy(namei,name1); for(i=0;i<N;i+) printf("%s,%dn",namei,marki); 7.輸出2維數(shù)組中行為最大列上為最小的元素(稱為鞍點(diǎn))及其位置。如果不存在任何鞍點(diǎn),則也應(yīng)輸出相應(yīng)信息。#include<stdio.h>void main()int i,j,k,m,n,max,maxj;int a1010;int flag=0;printf("ent

30、er the hang number n:n");scanf("%d",&n); printf("enter the lie number m:n"); scanf("%d",&m); for(i=0;i<n;i+)for(j=0;j<m;j+)scanf("%d",&aij);printf("n");for(i=0;i<n;i+) max=ai0; for(j=0;j<m;j+) if(aij>max) max=aij; maxj

31、=j; for(k=0;k<n;k+) if( max>akmaxj) break; if(k>=n)printf("nthe %d hang,the %d lie ,the number %d is an dian!n",i, maxj,max);flag=1;if(!flag)printf("no an dian!");8.計(jì)算矩陣A 的轉(zhuǎn)置矩陣A ,例如:A= , A = #include<stdio.h>void main()int a43,b34;int i,j;printf("enter array a

32、:n");for(i=0;i<4;i+)for(j=0;j<3;j+)scanf("%d",&aij);for(i=0;i<4;i+)for(j=0;j<3;j+)printf("%4d",aij);printf("n"); for(i=0;i<4;i+)for(j=0;j<3;j+)bji=aij;for(i=0;i<3;i+)for(j=0;j<4;j+)printf("%4d",bij);printf("n");9. 輸入一

33、個(gè)八進(jìn)制數(shù)的字符串,將他轉(zhuǎn)換成等價(jià)的十進(jìn)制字符串,用printf的s格式輸出轉(zhuǎn)換結(jié)果以檢驗(yàn)轉(zhuǎn)換的正確性。例如:輸入字符串"1732",轉(zhuǎn)換成十進(jìn)制數(shù)的字符串為"986"。#include "stdio.h"#include "string.h"#include "math.h"#define MAX 20void main()int i,len,tmp;long int sum = 0;char inputMAX;char outputMAX=""scanf("%s

34、",input);len = strlen(input);for(i = 0;i < len;i +) sum=sum*8+(inputi - 48);for(i = 0;i < MAX;i +)outputi = (char)(sum % 10 +48);sum /= 10;if(sum=0)break;len = strlen(output);for(i = 0;i < len/2;i +)tmp = outputi;outputi = outputlen-1;outputlen-1 = tmp;printf("%s",output);10.

35、 輸入一行字母串,統(tǒng)計(jì)其中各個(gè)字母和空格的分別出現(xiàn)的次數(shù)。#include<stdio.h>void main()char s80;int i,up,low,spa;up=low=spa=0;gets(s);for(i=0;si!='0'i+)if(si>='A')&&(si<='Z')up+;else if (si>='a')&&(si<='z')low+;else if(si=' ')spa+;else ;printf(&quo

36、t;the up=%d,the low=%d,the spa=%d",up,low,spa);第6章編程題參考答案 1編寫(xiě)程序,實(shí)現(xiàn):利用指向字符數(shù)組的指針變量,統(tǒng)計(jì)兩個(gè)字符數(shù)組中相同的字符個(gè)數(shù).#include<stdio.h>#include<string.h>void main()char s180,s280;char *p1,*p2;int n=0,len1,len2,i,j;p1=s1,p2=s2;gets(s1);gets(s2);len1=strlen(s1);len2=strlen(s2);for(i=0;i<len1&&

37、i<len2;i+)if(*(p1+i)=*(p2+i) n+;printf("%d",n);2輸入三個(gè)整數(shù),按從小到大的順序輸出, 用三種不同方式實(shí)現(xiàn).方法1:# include <stdio.h># include <conio.h>void main() int a3,i,j,temp;/*清屏指令*/ clrscr(); printf("nnPlease input 3 integers :nn"); for (i=0;i<3;i+) scanf("%d",&ai); for(i=0

38、;i<2;i+) for (j=i+1;j<3;j+) if (aj<ai) temp=ai; ai=aj; aj=temp; printf("nnAfter sorted, the 3 inegers:nn"); for (i=0;i<3;i+) printf("%dt",ai); 方法2:# include <stdio.h># include <conio.h>void main() int a3,i,j,temp; clrscr(); printf("nnPlease input 3 in

39、tegers :nn"); for (i=0;i<3;i+) scanf("%d",a+i); for(i=0;i<2;i+) for (j=i+1;j<3;j+) if (*(a+j)<*(a+i) temp=*(a+i); *(a+i)=*(a+j); *(a+j)=temp; printf("nnAfter sorted, the 3 inegers:nn"); for (i=0;i<3;i+) printf("%dt",*(a+i); 方法3:# include <stdio.h&

40、gt;# include <conio.h>void main() void swap(int *p1, int *p2); int a,b,c; int *pa=&a,*pb=&b,*pc=&c; clrscr(); printf("nnPlease input 3 integers :nn"); scanf("%d",&a); scanf("%d",&b); scanf("%d",&c); if (b>c) swap(pb,pc); if (a&

41、gt;b) swap(pa,pb); if (b>c) swap(pb,pc); printf("nnAfter sorted, the 3 inegers:nn"); printf("%dt%dt%dt",a,b,c); void swap(int *p1, int *p2) int temp; temp=*p1; *p1=*p2; *p2=temp; 3有一字符串,包含n個(gè)字符。使用指針,將此字符串從第m個(gè)字符開(kāi)始的全部字符復(fù)制成為另一個(gè)字符串。# include <stdio.h># include <string.h>

42、;# include <conio.h># define N1 20# define N2 20void main() char str1N1,str2N2; int m,n,i,j; clrscr(); printf("nnPlease input a string:nn"); gets(str1); n=strlen(str1); do printf("nnPlease input place m:nn"); scanf("%d",&m); while(n<m); for (i=m-1,j=0; i<

43、;n; i+,j+) *(str2+j)=*(str1+i); *(str2+j)='0' printf("nnThe new string :nn"); printf("%s",str2); 4 輸入一個(gè)字符串,內(nèi)有數(shù)字和非數(shù)字字符,如:a123x456 17960?302tab5876將其中連續(xù)的數(shù)字作為一個(gè)整數(shù),依次存放到一數(shù)組a中。例如123放在a0中,456放在a1中統(tǒng)計(jì)共有多少個(gè)整數(shù),并輸出這些數(shù)。# include <stdio.h># include <conio.h># include <c

44、type.h># include <string.h>void main() char str60,ch; int i,j,bit,dit,n; long int a20; clrscr(); printf("nnPlease input a string:nn"); scanf("%s",str); for (i=0,j=0,a0=0,bit=1,dit=0;i<strlen(str) ;i+) if(isdigit(ch=*(str+i) dit=1; aj*=bit; aj+=(ch-48); bit=10; n=j; el

45、se if (dit=1) j+; aj=0 ; dit=0; bit=1; printf ("nnai :nn") ; for (i=0;i<=n;i+) printf("nna%d=%ldn",i,*(a+i); 5 利用數(shù)組和指針,將一個(gè)4*4的矩陣轉(zhuǎn)置,并輸出矩陣中的最大值及其位置# include <stdio.h># include <conio.h># define N 4void initpm(float * pm, float mtN);void main() void inputm(float mtN);

46、 void printm( float mtN); void schmax(float mtN); void tranmtx(float mtN); float mtxNN ; clrscr(); inputm(mtx); printf("nn"); printm(mtx); schmax(mtx); tranmtx(mtx); printm(mtx); printf("n"); void inputm(float mtN) int i,j; float *pmN; initpm(pm,mt); printf("nnPlease input a

47、 4*4 matrix:nn"); for(i=0;i<4;i+) for (j=0;j<4;j+,pmi+) scanf("%f",pmi); void printm( float mtN) int i,j; float *pmN; initpm(pm,mt); for(i=0;i<N;i+) for(j=0;j<N;j+,pmi+) printf("%3.2ft",*pmi); printf("n"); void schmax(float mtN) int i,j,line ,colum; flo

48、at *pmN,max; initpm(pm,mt); max=mt00; line=0; colum=0; for (i=0;i<N;i+) for(j=0;j<N;j+) if (max<*(pmi+j) max=*(pmi+j); line=i; colum=j; printf("nnThe max digital in the matrix :%3.2fnn",max); printf("The line of the max digital : %d nn",line+1); printf("The colum of

49、 the max digital : %d nn",colum+1); void tranmtx(float mtN) int i,j; float *pmN; initpm(pm,mt); for (i=0;i<N;i+) for(j=i+1;j<N;j+) float temp; temp=*(pmi+j); *(pmi+j)=*(pmj+i); *(pmj+i)=temp; void initpm(float * pm, float mtN) int i; for (i=0;i<N;i+) pmi=mti; 6利用指向指針的指針?lè)椒▽?duì)6個(gè)字符串排序并輸出.#

50、include <stdio.h># include <conio.h># include <string.h># define N 6void main() char strN20; char *pstrN; int i,j; for (i=0;i<N;i+) pstri=stri; char *pp; pp=pstr; clrscr(); printf("nnPlease input %d strings:nn",N); for(i=0;i<N;i+) gets(pstri); for(i=0;i<N-1;i+) for(j=i+1;j<N;j+) if (strcmp(*(pp+i),*(pp+j)>0) char *temp=*(pp+i);*(pp+i)=*(pp+j);*(pp+j)=temp; printf("nnAfter sorted

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(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)論