免费建网站 建站之星,高端网站建设设计,如何优化网站性能,泰安房价网二手房出售信息接着上次来说#xff0c;C语言经典面试题目及答案详解#xff08;一#xff09;当中大部分是一些概念和理解的东西 #xff0c;今天说一说实践操作#xff0c;有关c的经典程序。1、输出9*9口诀。共9行9列#xff0c;i控制行#xff0c;j控制列。#include stdio.hC语言经典面试题目及答案详解一当中大部分是一些概念和理解的东西 今天说一说实践操作有关c的经典程序。1、输出9*9口诀。共9行9列i控制行j控制列。#include stdio.h
int main(){int i,j,result;for (i1;i10;i){ for(j1;j10;j){resulti*j;printf(%d*%d%-3d,i,j,result);/*-3d表示左对齐占3位*/}printf(n);/*每一行后换行*/}}2、古典问题有一对兔子从出生后第3个月起每个月都生一对兔子小兔子长到第三个月后每个月又生一对兔子假如兔子都不死问每个月的兔子总数为多少 兔子的规律为数列1,1,2,3,5,8,13,21....#include stdio.hint main(){long f1,f2;int i;f1f21;for(i1;i20;i){ printf(%12ld %12ld,f1,f2);if(i%20) printf(n);/*控制输出每行四个*/f1f1f2; /*前两个月加起来赋值给第三个月*/f2f1f2; /*前两个月加起来赋值给第三个月*/}}3、判断101-200之间有多少个素数并输出所有素数及素数的个数。 程序分析判断素数的方法用一个数分别去除2到sqrt(这个数)如果能被整除则表明此数不是素数反之是素数。#include math.h#include stdio.hint main(){int m,i,k,h0,leap1;printf(n);for(m101;m200;m){ ksqrt(m1);for(i2;ik;i){if(m%i0){leap0;break;}}if(leap) /*内循环结束后leap依然为1则m是素数*/{printf(%-4d,m);h;if(h%100)printf(n);}leap1;}printf(nThe total is %d,h);}4、一个数如果恰好等于它的因子之和这个数就称为完数。例如6123.编程找出1000以内的所有完数。#include stdio.hint main(){static int k[10];int i,j,n,s;for(j2;j1000;j){n-1;sj;for(i1;ij;i){if((j%i)0){ n;ss-i;k[n]i;}}if(s0){printf(%d is a wanshu: ,j);for(i0;in;i)printf(%d,,k[i]);printf(%dn,k[n]);}}}5、下面程序的功能是将一个4×4的数组进行逆时针旋转90度后输出要求原始数组的数据随机输入新数组以4行4列的方式输出请在空白处完善程序。#include stdio.h#include string.hvoid main(){ int a[4][4],b[4][4],i,j; /*a存放原始数组数据b存放旋转后数组数据*/printf(input 16 numbers: );/*输入一组数据存放到数组a中然后旋转存放到b数组中*/for(i0;i4;i)for(j0;j4;j){ scanf(%d,a[i][j]);b[3-j][i]a[i][j];}printf(array b:n);for(i0;i4;i){ for(j0;j4;j)printf(%6d,b[i][j]);printf(n);}}6、编程打印直角杨辉三角形#include stdio.h
void main(){int i,j,a[6][6];for(i0;i5;i){a[i][i]1;a[i][0]1;}for(i2;i5;i)for(j1;ji-1;j)a[i][j]a[i-1][j]a[i-1][j-1];for(i0;i5;i){for(j0;ji;j)printf(%4d,a[i][j]);printf(n);}}7、通过键盘输入3名学生4门课程的成绩分别求每个学生的平均成绩和每门课程的平均成绩。要求所有成绩均放入一个4行5列的数组中输入时同一人数据间用空格,不同人用回车其中最后一列和最后一行分别放每个学生的平均成绩、每门课程的平均成绩及班级总平均分。#include stdio.h#include stdlib.hvoid main(){ float a[4][5],sum1,sum2;int i,j;for(i0;i3;i)for(j0;j4;j)scanf(%f,a[i][j]);for(i0;i3;i){ sum10;for(j0;j4;j)sum1a[i][j];a[i][4]sum1/4;}for(j0;j5;j){ sum20;for(i0;i3;i)sum2a[i][j];a[3][j]sum2/3;}for(i0;i4;i){ for(j0;j5;j)printf(%6.2f,a[i][j]);printf(n);}}8、完善程序实现将输入的字符串反序输出 如输入windows 输出swodniw。#include string.h#include stdio.hvoid main(){ char c[200],c1;int i,j,k;printf(Enter a string: );scanf(%s,c);kstrlen(c);for (i0,jk-1;ik/2;i,j--){ c1c[i];c[i]c[j];c[j]c1; }printf(%sn,c);}指针法#include string.h#include stdio.hvoid invert(char *s){int i,j,k;char t;kstrlen(s);for(i0,jk-1;ik/2;i,j--){ t*(si); *(si)*(sj); *(sj)t; }}void main(){ FILE *fp;char str[200],*p,i,j;if((fpfopen(p9_2.out,w))NULL){ printf(cannot open the filen);exit(0);}printf(input str:n);gets(str);printf(n%s,str);fprintf(fp,%s,str);invert(str);printf(n%s,str);fprintf(fp,n%s,str);fclose(fp);}9、下面程序的功能是从字符数组s中删除存放在c中的字符。#include stdio.hvoid main(){
char s[80],c;int j,k;printf(nEnter a string: );gets(s);printf(nEnter a character: );cgetchar( );for(jk0;s[j]! 0;j)if(s[j]!c)s[k]s[j];s[k] 0;printf(n%s,s);}10、编写一个void sort(int *x,int n)实现将x数组中的n个数据从大到小 排序。n及数组元素在主函数中输入。将结果显示在屏幕上并输出到文件p9_1.out中#includestdio.hvoid sort(int *x,int n){int i,j,k,t;for(i0;in-1;i){ki;for(ji1;jn;j)if(x[j]x[k]) kj;if(k!i){tx[i];x[i]x[k];x[k]t;}}}void main(){FILE *fp;int *p,i,a[10];fpfopen(p9_1.out,w);pa;printf(Input 10 numbers:);for(i0;i10;i)scanf(%d,p);pa;sort(p,10);for(;pa10;p){ printf(%d ,*p);fprintf(fp,%d ,*p); }system(pause);fclose(fp);}11、已知数组a中的元素已按由小到大顺序排列以下程序的功能是将输入的一个数插入数组a中插入后数组a中的元素仍然由小到大顺序排列#includestdio.hvoid main(){ int a[10]{0,12,17,20,25,28,30}; /*a[0]为工作单元从a[1]开始存放数据*/int x , i, j6; /*j为元素个数*/printf(Enter a number: );scanf(%d,x);a[0]x;ij; /*从最后一个单元开始*/while(a[i]x){ a[i1]a[i]; i--; } /*将比x大的数往后移动一个位置*/a[i]x;j; /*插入x后元素总个数增加*/for(i1;ij;i) printf(%8d,a[i]);printf(n);}12、编写函数replace(char *s,char c1,char c2)实现将s所指向的字符串中所有字符c1用c2替换字符串、字符c1和c2均在主函数中输入将原始字符串和替换后的字符串显示在屏幕上并输出到文件p10_2.out中#includestdio.hvoid replace(char *s,char c1,char c2){while(*s!0){ if (*sc1)*sc2;s;}}void main(){ FILE *fp;char str[100],a,b;if((fpfopen(p10_2.out,w))NULL){ printf(cannot open the filen);exit(0); }printf(Enter a string:n);gets(str);printf(Enter ab:n);scanf(%c,%c,a,b);printf(%sn,str);fprintf(fp,%sn,str);replace(str,a,b);printf(The new string is----%sn,str);fprintf(fp,The new string is----%sn,str);fclose(fp);}13、在一个字串s1中查找一子串s2若存在则返回子串在主串中的起始位置不存在则返回-1。#includestdio.hvoid replace(char *s,char c1,char c2)void main(){char s1[6]thisis;char s2[5]is;printf(%dn,search(s1,s2));system(pause);}int search(char s1[],char s2[]){int i0,j,lenstrlen(s2);while(s1[i]){for(j0;jlen;j)if(s1[ij]!s2[j]) break;if(jlen)return i;else i;}return -1;}14、用指针变量输出结构体数组元素。#includestdio.hstruct student{int num;char *name;char sex;int age;}
stu[5]{{1001,lihua,F,18},{1002,liuxing,M,19},{1003,huangke,F,19},{1004,fengshou,F,19},{1005,Wangming,M,18}};void main(){int i;struct student *ps;printf(Num tNametttSextAgetn);/*用指针变量输出结构体数组元素。*/for(psstu;psstu5;ps)printf(%dt%-10stt%ct%dtn,ps-num,ps-name,ps-sex,ps-age);/*用数组下标法输出结构体数组元素学号和年龄。*/for(i0;i5;i)printf(%dt%dtn,stu[i].num,stu[i].age);}15、建立一个有三个结点的简单链表#includestdio.h#define NULL 0struct student{int num;char *name;int age ;struct student *next;};void main(){struct student a,b,c,*head,*p;a.num1001; a.namelihua; a.age18; /* 对结点成员进行赋值 */b.num1002; b.nameliuxing; b.age19;c.num1003; c.namehuangke; c.age18;heada; /* 建立链表a为头结点 */a.nextb;b.nextc;c.nextNULL;phead; /* 输出链表 */do{printf(%5d,%s,%3dn,p-num,p-name,p-age);pp-next;}while(p!NULL);}16、输入一个字符串判断其是否为回文。回文字符串是指从左到右读和从右到左读完全相同的字符串。#include stdio.h#include string.h
void main(){ char s[100];int i,j,n;printf(输入字符串n);gets(s);nstrlen(s);for(i0,jn-1;ij;i,j--)if(s[i]!s[j]) break;if(ij) printf(是回文串n);else printf(不是回文串n);}17、冒泡排序从小到大排序后结果输出到屏幕及文件myf2.out#includestdio.hvoid fun(int a[],int n){int i,j,t;for(i0;in-1;i)for(j0;ji;j)if(a[j]a[j1]) {ta[j];a[j]a[j1];a[j1]t;}}void main(){int a[10]{12,45,7,8,96,4,10,48,2,46},n10,i;FILE *f;if((ffopen(myf2.out,w))NULL)printf(open file myf2.out failed!n);fun(a,10);for(i0;i10;i){printf(%4d,a[i]);fprintf(f,%4d,a[i]);}fclose(f);}18、编写函数countpi利用公式 计算π的近似值当某一项的值小于10-5时认为达到精度要求请完善函数。将结果显示在屏幕上并输出到文件p7_3.out中。#includestdio.hdouble countpi(double eps) /*eps为允许误差*/{int m1;double temp1.0,s0;while(tempeps){ stemp;temptemp*m/(2*m1);m;}return(2*s);}void main(){FILE *fp;double eps1e-5,pi;if((fpfopen(p7_3.out,w))NULL){ printf(cannot open the filen);exit(0);}pi countpi(eps);printf(pi%lfn,pi);fprintf(fp,pi%lfn,pi);fclose(fp);}