计算机软件基础小抄.doc_第1页
计算机软件基础小抄.doc_第2页
计算机软件基础小抄.doc_第3页
计算机软件基础小抄.doc_第4页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1编写一个C程序,输入a,b,c三个值,输出其中最大者*/main()float a,b,c,max; printf(please input three numbers:n); scanf(%f%f%f,&a,&b,&c); if(a=b) max=a; else max=b; if(maxc) max=c; printf(the max is: %fn,max);2两个瓶子分别装醋和酱油,要求互换,实际就是交换两变量*/main()int a,b,t; printf(please input two integer numbers:n); scanf(%d%d,&a,&b); printf(before swap:a=%d,b=%dn,a,b); t=a; a=b; b=t; printf(after swap:a=%d,b=%dn,a,b);3依次将10个数输入,要求打印其中最大的数*/main()float num,max,n=1; printf(please input a number:n); scanf(%f,&num); max=num; while(nmax) max=num; n=n+1; printf(the max=%fn,max);4有3个数a,b,c,要求按大小顺序把它们打印出来*/main()float a,b,c,t; printf(please input three numbers:n); scanf(%f%f%f,&a,&b,&c); printf(before sort:a=%g,b=%g,c=%gn,a,b,c); if(ab) t=a;a=b;b=t; if(ac) t=a;a=c;c=t; if(bc) t=b;b=c;c=t; printf(after sort:a=%g,b=%g,c=%gn,a,b,c);5有3个整数a,b,c,由键盘输入,输出其中最大的数*/main()int a,b,c,max; printf(please input three integer numbers:n); scanf(%d%d%d,&a,&b,&c); if(a=b) max=b; else max=a; if(maxc) max=c; printf(max=%dn,max);求1+2+3+.+100*/main()int sum=0,i=1; while(i=100) sum=sum+i; i=i+1; printf(1+2+3+.+100=%dn,sum);6、/*判断一个数n能否同时被3和5整除*/main()int n; printf(please input a integer number:n); scanf(%d,&n); if(n%3=0)&(n%5=0) printf(%d can be divided by both 3 and 5n,n); else printf(%d cant be divided by both 3 and 5n,n);7、将100到200之间的素数打印出来*/#include math.hmain()int num=100,i,flag; printf(the prime number from 100 to 200:n); while(num=200) i=2;flag=0; while(i=sqrt(num) if(num%i=0) flag=1; i=i+1; if(flag=0) printf(%4d,num); num=num+1; printf(n);8、/*求两个数m和n的最大公约数*/main()int m,n,temp,answer; printf(please input two integer numbers:n); scanf(%d%d,&m,&n); if(mn) temp=m;m=n;n=temp; while(n!=0) temp=m%n; m=n; n=temp; answer=m; printf(the greatest common divisor is %dn,answer);9、/*打印出1900到2000年中是闰年的年份,闰年的条件是能被4整除但不能被100整除 或 能被100整除且能被400整除*/main()int year=1900; printf(the leap year from 1900 to 2000:n); while(year=2000) if(year%4=0&year%100!=0) | (year%100=0&year%400=0) printf(%6d,year); year=year+1; printf(n);10、/*输入一个华氏温度,要求输出摄氏温度,公式为;c=5/9*(F-32)*/main()float c,F; printf(please input a Fahrenheit temperature:); scanf(%f,&F); c=5.0/9.0*(F-32); printf(when Fahrenheit temperature=%.2f,then Celsius temperature=%.2fn,F,c);11/*有一函数:当x=1且x=10时,y=3x-11,写一程序,输入x,输出y值*/main()int x,y; printf(please input x:n); scanf(%d,&x); if(x1) y=x; else if(xb) t=a;a=b;b=t; if(ac) t=a;a=c;c=t; if(ad) t=a;a=d;d=t; if(bc) t=b;b=c;c=t; if(bd) t=b;b=d;d=t; if(cd) t=c;c=d;d=t; printf(small to large is:%d,%d,%d,%dn,a,b,c,d);14、/*输入两个正整数m和n,求其最大公约数和最小公倍数*/*根据数学上对最大公约数和最小公倍数的定义来考虑算法*/main()int m,n,a,b,temp,answer1,answer2; printf(please input two integer numbers:n); scanf(%d%d,&m,&n); if(m=a & ch=A & ch=0 & ch=9) digit+; else other+;printf(letter=%d,space=%d,digit=%d,other=%dn,letter,space,digit,other);6-317/*求Sn=a+aa+aaa+.+aa.a之值,其中a是一个数字,例如:2+22+222+2222+22222(此时a=2,n=5),n和a由键盘输入*/main()int a,n,i; long t,sum=0; printf(please input a and n:n); scanf(%d%d,&a,&n); t=a; for(i=1;i=n;i+) sum=sum+t; t=t*10+a; printf(a+aa+aaa+.=%ldn,sum);18、/*求阶乘和:1!+2!+3!+.+20!*/main()int n; float sum=0,t=1; for(n=1;n=20;n+) t=t*n; sum=sum+t; printf(1!+2!+3!+.+20!=%en,sum);19、/*现有三组数:1100之和;150的平方和;110的倒数和。求该三组数之和*/main()float k,sum,sum1=0,sum2=0,sum3=0; for(k=1;k=100;k+) sum1=sum1+k; for(k=1;k=50;k+) sum2=sum2+k*k; for(k=1;k=10;k+) sum3=sum3+1/k; sum=sum1+sum2+sum3; printf(sum= %fn,sum);20、/*打印出所有的水仙花数,所谓水仙花数是指一个3位数,其各位数字立方和等于该数本身。例如:153*/main()int i,j,k,n; printf(the narcissus number is:n); for(n=100;n1000;n+) i=n/100; j=n/10-i*10; k=n%10; if(i*i*i+j*j*j+k*k*k=n) printf(%dn,n); 21、一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如:6的因子为1、2、3,而6=1+2+3,因此6是”完数“。编程序找出1000之内的所有完数,并按下面格式输出其因子:6 its factors are 1,2,3*/main()static int k10; int i,j,n,s; for(j=2;j=1000;j+) n=-1; s=j; for(i=1;ij;i+) if(j%i)=0) n+;s=s-i;kn=i; if(s=0) printf(%d its factors are:,j); for(i=0;i=n;i+) printf(%d ,ki); printf(n); 22、/*有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13,.。求出这个数列的前20项之和*/#define T 20main() float i,j,temp,n,sum; i=2;j=1;sum=0; for(n=1;n=T;n+) sum=sum+i/j; temp=i;i=i+j;j=temp; printf(2/1+3/2+5/3+8/5+13/8+.=%fn,sum);23、/*打印出菱形图案*/main()int i,j,k; for(i=1;i=4;i+) for(j=1;j=20-i;j+) printf( ); for(k=1;k=1;i-) for(j=1;j=20-i;j+)printf( ); for(k=1;k=2*i-1;k+)printf(*); printf(n); 24、/*两个乒乓球队进行比赛,各出3个。甲队为A、B、C3人,乙队为X、Y、Z3人。已抽签决定比赛名单。有人向队员打听比赛的名单,A说他不和X比,C说他不和X、Z比,请编程序找出3对赛手的名单*/main() char i,j,k; /*i is As enemy,j is Bs enemy,k is Cs enemy*/ for(i=X;i=Z;i+) for(j=X;j=Z;j+)if(i!=j) for(k=X;k=Z;k+)if(k!=i&k!=j) if( i!=X & k!=X & k!=Z) printf(the match list is: A-%c,B-%c,C-%cn,i,j,k);25、用筛选法求100之内的素数*/#define N 100#include math.hmain()int aN,i,j; clrscr(); for(i=0;iN;i+) ai=i+1; for(i=1;iN;i+) for(j=2;j=sqrt(ai);j+) if(ai%j=0) ai=0; break; printf(the prime number from 3 to %d:n,N); for(i=2;iN;i+) if(ai!=0) printf(%3d,ai); printf(n); getch();26、/*用选择法对10个整数排序*/#define N 10main()int aN,i,j,min,temp; clrscr(); printf(please input %d integer numbers:n,N); for(i=0;iN;i+) scanf(%d,&ai); printf(the array before sorted:n); for(i=0;iN;i+) printf(%d ,ai); printf(n); for(i=0;iN-1;i+) min=i; for(j=i;jN;j+) if(ajamin) min=j; temp=ai; ai=amin;amin=temp; printf(the array after sorted:n); for(i=0;iN;i+) printf(%d ,ai); printf(n); getch();27、/*求一个33矩阵对角线元素之和*/#define m 3main()int amm,i,j,sum=0; clrscr(); printf(please input a %d*%d integer array:n,m,m); for(i=0;im;i+) for(j=0;jm;j+) scanf(%d,&aij); printf(the %d*%d integer array is:n,m,m); for(i=0;im;i+) for(j=0;jm;j+) printf(%d ,aij); printf(n); for(i=0;im;i+) sum=sum+aii+aim-1-i; if(m%2=1) sum=sum-am/2m/2; printf(the sum of diagonal is: %dn,sum); getch();28、/*已有一个已排好序(由小到大)的数组,今输入一个数,要求按原来排序的规律将它插入原数组中*/#define N 10main()int aN+1,num,temp,i,position; clrscr(); printf(please input %d integer numbers(small to big):n,N); for(i=0;iN;i+) scanf(%d,&ai); printf(please input the integer you want to insert:n); scanf(%d,&num); if(aN-1=num) position=N; for(i=0;inum) position=i;break; for(i=N-1;i=position;i-) ai+1=ai; aposition=num; printf(after insert,the array is:n); for(i=0;iN+1;i+) printf(%d ,ai); printf(n); getch();29、/*将一个数组中的值按逆序重新存放。例如,原来顺序为8,6,5,4,1,要求改为1,4,5,6,8*/#define N 10main()int aN,i,temp; clrscr(); printf(please input %d integer numbers:n,N); for(i=0;iN;i+) scanf(%d,&ai); printf(the array before reverse:n); for(i=0;iN;i+) printf(%d ,ai); printf(n); for(i=0;iN/2;i+) temp=ai; ai=aN-i-1; aN-i-1=temp; printf(the array after reverse:n); for(i=0;iN;i+) printf(%d ,ai); printf(n); 30、/*打印10行杨辉三角形*/#define N 10main()int aNN,i,j; clrscr(); for(i=0;iN;i+) aii=1; ai0=1; for(i=2;iN;i+) for(j=1;ji;j+) aij=ai-1j-1+ai-1j; printf(the %d line yanghui triangle is:n,N); for(i=0;iN;i+) for(j=0;j=i;j+) printf(%6d,aij); printf(n); 31、 /*打印“魔方阵”。所谓魔方阵是指这样的方阵,它的每一行、每一列和对角线之和均相等。要求打印出由1到n*n的自然数构成的魔方阵*/main() int a1616,i,j,k,p,m,n; clrscr(); p=1; while(p=1) printf(please input n:( 0n0)&(n=15)&(n%2!=0) p=0; else printf(error,retry! ); for(i=1;i=n;i+) for(j=1;j=n;j+) aij=0; j=n/2+1; a1j=1; for(k=2;k=n*n;k+) i=i-1; j=j+1; if(in) i=i+2;j=j-1; else if(in) j=1; if(aij=0) aij=k;else i=i+2; j=j-1; aij=k; for(i=1;i=n;i+) for(j=1;j=n;j+) printf(%3d,aij); printf(n); getch(); 32、/*找出一个二维数组中的鞍点,即该位置上的元素在该行上最大,在该列上最小,也可能没有鞍点*/#define m 3#define n 4main()int amn,i,j,k,max,maxi,maxj,flag1,flag2; clrscr(); printf(please input a %d*%d array:n,m,n); for(i=0;im;i+) for(j=0;jn;j+) scanf(%d,&aij); printf(the array you have inputed:n); for(i=0;im;i+) for(j=0;jn;j+) printf(%d ,aij); printf(n); flag2=0; for(i=0;im;i+) max=ai0; for(j=0;jn;j+) if(maxaij) max=aij; maxj=j; flag1=1; for(k=0;km&(flag1=1);k+) if(akmaxjmax) flag1=0; if(flag1=1) printf(a%d%d=%d is saddle point!n,i,maxj,max); flag2=1; if(flag2=0) printf(this array has no saddle point!n); 33、/*有15个数已按由大到小的顺序存放在一个数组中,现再输入一个数,要求用折半查找法找出该数是数组中第几个元素的值。如果该数不在数组中,则打印出“无此数”*/#define N 15main()int aN,num,i,left,right,mid,find=0; clrscr(); left=0; right=N-1; printf(please input %d integer numbers( big to small):n,N); for(i=0;iN;i+) scanf(%d,&ai); printf(please input the integer you want to find:n); scanf(%d,&num); while( find=0 & left=right) mid=(left+right)/2; if(numamid) right=mid-1; else find=1; if(find=1) printf(%d has been found,it is a%dn,num,mid); else printf(%d has not been foundn,num); getch();34、/*/*用函数实现从3个数中找出最大数*/main()int a,b,c; int max(int x,int y,int z); clrscr(); printf(please input 3 integer numbers:n); scanf(%d%d%d,&a,&b,&c); printf(the max number is: %dn,max(a,b,c); getch();int max(int x,int y,int z)int t; if(xy) t=y; else t=x; if(tz) t=z; return(t);35、有一篇文章,共有3行文字,每行有80个字符。要求分别统计出其中英文大写字母、小写字母、数字、空格以及其他字符的个数*/#include string.h#define N 3main()char strN81; int i,j,big=0,small=0,digit=0,space=0,other=0; clrscr(); printf(please input %d line string:n,N); for(i=0;iN;i+) gets(stri); for(i=0;iN;i+) for(j=0;j=a&strij=A&strij=0&strij=9) digit+; else if(strij= ) space+; else other+; printf( big letter=%dn small letter=%dn digit=%dn,big,small,digit); printf( space=%dn other=%dn,space,other); getch();36、/*编一程序,将两个字符串连接起来,不要用strcat函数*/#include string.hmain()char str181,str281; int i,j; clrscr(); printf(please input the first string:n); gets(str1); printf(please input the second string:n); gets(str2); for(i=0;str1i!=0;i+) ; for(j=0;str2j!=0;j+,i+) str1i=str2j; str1i=0; printf(-after catenate,the string is-:n); puts(str1); getch();37、/*比较两个字符串大小,并输出它们首个不相同字符的ASCII码差值,不用strcmp函数*/#include string.hmain()char s181,s281; int i,x; clrscr(); printf(please input the first string:n); gets(s1); printf(please input the second string:n); gets(s2); for(i=0;s1i=s2i&s1i!=0;i+) ; x=s1i-s2i; if(s1i=0&s2i=0) x=0; printf(the compare result is: %dn,x); getch();38、/*编写一个程序,将字符数组s2中的全部字符拷贝到字符数组s1中,不用strcpy函数。拷贝时,0也要拷贝过去,0后面的字符不拷贝*/#include string.hmain()char from81,to81; int i; clrscr(); printf(please input a string:n); gets(from); for(i=0;i81&fromi!=0;i+) toi=fromi; toi=0; printf(-after copy-:n); puts(to); getch();/39、写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果,两个整数由键盘输入*/main()int m,n,answer1,answer2; int divisor(int x,int y); int multiple(int x,int y); clrscr(); printf(please input two integer numbers:n); scanf(%d%d,&m,&n); answer1=divisor(m,n); answer2=multiple(m,n); printf(the greatest common divisor is %dn,answer1); printf(the lease common multiple is %dn,answer2); getch();int divisor(int x,int y)int temp; for(temp=x;temp-) if(x%temp=0&y%temp=0) break; return(temp);int multiple(int x,int y)int temp; for(temp=x;temp+) if(temp%x=0&temp%y=0) break; return(temp);40、/*写一函数,使给定的一个二维数组(3*3)转置,即行列互换*/#define N 3main()int arrayNN,i,j; void rotate(int aNN); clrscr(); printf(input a %d*%d integer array:n,N,N); for(i=0;iN;i+) for(j=0;jN;j+) scanf(%d,&arrayij); printf(the array before rotate:n); for(i=0;iN;i+) for(j=0;jN;j+) printf(%d ,arrayij); printf(n); rotate(array); printf(the array after rotate:n); for(i=0;iN;i+) for(j=0;jN;j+) printf(%d ,arrayij); printf(n); getch();void rotate(int aNN)int i,j,t; for(i=0;iN;i+) for(j=0;ji;j+) t=aij; aij=aji; aji=t; 41、/*写一函数,使输入的一个字符串按反序存放,在主函数中输入和输出字符串*/#include string.hmain()char string81; void reverse(char str); clrscr(); printf(please input the string:n); gets(string); reverse(string); printf(after reverse:n); puts(string); getch();void reverse(char str)int i,t,len; len=strlen(str); for(i=0;i0;i-) str2*(i-1)=stri-1; str2*(i-1)-1= ; str2*len-1=0;45、/*编写一函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符的个数,在主函数中输入字符串以及输出上述的结果*/#include string.hint letter=0,digit=0,space=0,other=0;void count(char str)int i; for(i=0;stri!=0;i+)if(stri=A&stri=a&stri=0&stri=9) digit+; else if(stri= ) space+; else other+;main()char str81; clrscr(); printf(please input the string:n); gets(str); count(str);printf(letter=%d,digit=%d,space=%d,other=%dn,letter,digit,space,other); getch();46、/*写一函数,输入一行字符,将此字符串中最长的单词输出*/#include string.hint character(cha

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论