




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、第三章 程序和流程控制 1.输入两个整数,输出较大者。 #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个整数a,b,c,由键盘输入,输出其中最大的数。#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语句。#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 的前十项。#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.输入一个自然数,判断它是奇数还是偶数。 #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项之和。 #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.有一个函数 写一程序,输入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.给一个不多于5位的的正整数,要求:求出它是几位数,分别打印出每一位数字,最后按照逆序打印各位数字,例如原数为321,应输出为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编写一猜数游戏程序,随机产生某个整数,从键盘反复输入整数进行猜数,当未猜中时,提示输入过大或过小.猜中时,指出猜的次数.最多允许猜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.计算1999中能被3整除,且至少有一位数字是5的所有整数。 #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.输入两个整数,求它们的最大公约数和最小公倍数。#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;/求公约数算法while(c%d)r=c%d;c=d;d=r;comYueShu=d;comBeis
13、hu=a*b/comYueShu;printf("%d,%d",comYueShu,comBeishu);12.输入一个整数,求它包含又多少个2的因子。(例如,8含有3个2的因子,10含有一个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.计算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.猴子吃桃问题:第一天吃掉总数的一半多一个,第二天又将剩下的桃子吃掉一半多一个,以后每天吃掉前一天剩下的一半多一个,到第十天准备吃的时候见只剩下一个桃子,
15、求第一天开始吃的时候桃子的总数。#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,计算出圆锥体的体积并输出#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章课后编程习题参考答案2.已有一个已排好序的数组,现在输入一个数,要求按照原来排序的规则将它插入数组中。#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、阵是指这样的方阵,它的每一行,每一列和对角线之和均相等。例如,三阶魔方阵为: 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个字符,要求分别统计出其中英文大写字母,小写字母,数字,空格以及其它字符的个数。#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.编写一个程序,将两个字符串连起来,不要用strcat函数。#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.输入每个学生的平均成绩和姓名,将成绩按照递减顺序排序,姓名做相应的调整。输出排序后的成绩和姓名。#include<stdio.h>#
28、include<string.h>define N 10/* 假定有十个学生*/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维数组中行为最大列上为最小的元素(称为鞍点)及其位置。如果不存在任何鞍点,则也应输出相应信息。#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.计算矩阵A 的转置矩阵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、个八进制数的字符串,将他转换成等价的十进制字符串,用printf的s格式输出转换结果以检验转换的正确性。例如:输入字符串"1732",转换成十进制数的字符串为"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、 输入一行字母串,统计其中各个字母和空格的分别出现的次数。#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编写程序,实现:利用指向字符数组的指针变量,统计两个字符数组中相同的字符个数.#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输入三个整数,按从小到大的顺序输出, 用三种不同方式实现.方法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个字符。使用指针,将此字符串从第m个字符开始的全部字符复制成为另一个字符串。# 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 输入一个字符串,内有数字和非数字字符,如:a123x456 17960?302tab5876将其中连续的数字作为一个整数,依次存放到一数组a中。例如123放在a0中,456放在a1中统计共有多少个整数,并输出这些数。# 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 利用数组和指针,将一个4*4的矩阵转置,并输出矩阵中的最大值及其位置# 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利用指向指针的指针方法对6个字符串排序并输出.#
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. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 网红炸鸡连锁店区域代理合作协议-品牌授权与区域保护
- 网络视频游戏平台用户数据安全保密及游戏平衡性协议
- 股票期权激励计划与员工职业发展规划协议
- 癌症药物治疗技术发展与应用
- 大班音乐活动:大狮子教案设计
- 遗产继承证据确认合同(2篇)
- 临终心理护理实施要点
- 2024-2025学年高中地理课下能力提升九资源的跨区域调配-以南水北调为例含解析鲁教版必修3
- 学校春夏季常见传染病防控指南
- 个人贷款管理暂行办法
- GB/T 33084-2016大型合金结构钢锻件技术条件
- GB/T 23703.1-2009知识管理第1部分:框架
- 12掺合料试验记录(矿渣粉)带数据
- 春天就是我童声合唱简谱
- 普安金桥百汇项目经理变更申请书
- (新版)国家统计执法证资格考试备考题库(含答案)
- 《有趣的推理》课件公开课
- 工作单位接收函
- 研究生英语综合教程上-课文 翻译
- 中国联通cBSS系统使用培训-第一部分
- 施工进度网络图、施工进度横道图模板大全
评论
0/150
提交评论