《C语言程序设计慕课下编译答案完美》由会员分享,可在线阅读,更多相关《C语言程序设计慕课下编译答案完美(19页珍藏版)》请在金锄头文库上搜索。
1、-1求最大公约数和最小公倍数(15 分)*include int fun1( int m, int n) /辗转相除法求 m/n 的公约数int r;while (n)r=m % n;m=n;n=r;return m;int fun2(int m,int n) /最小公倍数return m*n/fun1(m,n);int main()int a,b;scanf(%d,%d,&a,&b); /输入printf(最大公约数:%dn 最小公倍数:%dn,fun1(a,b),fun2(a,b);return 0;2排序并插入(15 分)*include void InsertionSort(int*
2、arr, int n) / 插入排序int *last = 0, *ne*t = 0, key = 0, i = 0;for (i = 1; i n; +i)ne*t = arr + i, key = *ne*t;if (key *arr) / 将待插的数据比第一个数据小,直接插到最前面。for (last = ne*t; ne*t != arr; ne*t = last)*ne*t = *-last;*arr = key;else/ 否则从后往前遍历,找到第一个小于关键字的位置插入。for (last = ne*t; key *-last; ne*t = last)*ne*t = *last
3、;.z.-*ne*t = key;void OutPutArray(const int* arr, int n) / 打印一个数组int i;for (i = 0; i n; +i)printf(%d, arri);if(in-1)printf(,);printf(n);void InsertNumber(int* arr, int n)int key = 0, i = 0;InsertionSort(arr, n - 1);/ 对之前的 n-1 个数排序/ OutPutArray(arr, n - 1); / 这里可以打印出来看一下对之前的数据排序是否正确scanf(%d, &key);/
4、输入一个数if (key arr0)/ 参照之前的插入排序原理,将这个数插入到数组。for (i = n-1; 0 i; -i)arri = arri - 1;arr0 = key;elsefor (i = n-2; key arri; -i)arri + 1 = arri;arr+i = key;int main()int arr10 = 0 ;scanf(%d,%d,%d,%d,%d,%d,%d,%d,%d/ 输入 9 个数, arr + 0, arr + 1, arr + 2, arr + 3, arr + 4, arr + 5, arr + 6, arr + 7, arr + 8);I
5、nsertNumber(arr, 10);/ 调用函数,将输入的第 10 个数插进去。OutPutArray(arr, 10);/ 输出.z.-return (0);3.最大公约数*include int fun1( int m, int n) /辗转相除法求 m/n 的公约数int r;while (n)r=m % n;m=n;n=r;return m;int main()int a,b;scanf(%d,%d,&a,&b); /输入printf(%dn,fun1(a,b);return 0;4.奇数球和*includeint main() int n;int sum(int n);scan
6、f(%d,&n);printf(%dn,sum(n);int sum(int n) int b;if(n1)b=sum(n-1)+2*n-1;if(n=1)b=1;return b;5.巧算自然数*includeint step = 1;void fc(int n)/输出 n 这个数并按角谷猜想对n 做处理以便进入下一步。返回值是总步数。if(n = 1).z.-printf(%d,n);return;else if(n%2 = 0)printf(%d,n);fc(n/2);step+;elseprintf(%d,n);fc(n*3+1);step+;int main(int argc, ch
7、ar* argv)int n;scanf(%d,&n);fc(n);printf(nstep=%dn,step);return 0;6.卖鸭子*include int main()static int i,*8;*7=2;for(i=7;i=0;i-)*i-1=(*i+1)*2;printf(sum=%dn,*0);for(i=0;i7;i+)printf(sell=%d,*i-*i+1);printf(n);return 0;7.各位数字之和int add(int s)if (s10)return s;elsereturn s%10+add(s/10);.z.int main(int arg
8、c, char *argv)int a,b;scanf(%d,&a);b=add(a);printf(%d,b);return 0;对称字符串*include *include main() char a100;int i,j;gets(a);i=0;j=strlen(a)-1;while(i=j) printf(YESn);elseprintf(NOn);指针排序*include void sort(int *p,int n);void sort(int *p,int n)int i,j;int temp=0;for(i=0;in;i+)for(j=0;jpj+1)temp=pj;pj=pj
9、+1;pj+1=temp;-.z.-int main()int i;int a10;for(i=0;i10;i+)scanf(%d,&ai);int *p=a;sort(a,10);for(i=0;i10-1;i+)printf(%d,*(p+);printf(%dn,*p);return 0;查找数*include void find(int *p,int element,int n);void find(int *p,int element,int n)int i,flag=0;for(i=0;in;i+)flag+;if(element = pi)printf(%dn,pi);break
10、;if(flag = n)printf(Non);int main()int i;int a10;for(i=0;i10;i+).z.-scanf(%d,&ai);int *p=a;int num;scanf(%d,&num);find(p,num,10);return 0;输出指定成绩*include int main()int grade34,i,j,(*p)4=grade,n;float ave=0;for(i=0;i3;i+)for(j=0;j4;j+)scanf(%d,&gradeij);scanf(%d,&n);for(i=0;i3;i+)printf(%d ,*(*(p+n-1)
11、+i);printf(%dn,*(*(p+n-1)+3);for(i=0;i4;i+)ave+=*(*(p+n-1)+i);ave/=4;if(ave-(int)ave)printf(%.2fn,ave);elseprintf(%dn,(int)ave);.z.-return 0;成绩排序*include void average(int p6,int n);void average(int p6,int n)int i;for(i=0;ima*2 & ma*1ma*3)if(ma*2ma*3)printf(%d,%d,%d,%dn,p00,p01,p02,p03);printf(%d,%d,
12、%d,%dn,p10,p11,p12,p13);printf(%d,%d,%d,%dn,p20,p21,p22,p23);elseprintf(%d,%d,%d,%dn,p00,p01,p02,p03);printf(%d,%d,%d,%dn,p20,p21,p22,p23);printf(%d,%d,%d,%dn,p10,p11,p12,p13);else if(ma*2ma*1 & ma*2ma*3)if(ma*1ma*3)printf(%d,%d,%d,%dn,p10,p11,p12,p13);printf(%d,%d,%d,%dn,p00,p01,p02,p03);printf(%d,
13、%d,%d,%dn,p20,p21,p22,p23);.z.-elseprintf(%d,%d,%d,%dn,p10,p11,p12,p13);printf(%d,%d,%d,%dn,p20,p21,p22,p23);printf(%d,%d,%d,%dn,p00,p01,p02,p03);else if(ma*3ma*1 & ma*3ma*2)if(ma*1ma*2)printf(%d,%d,%d,%dn,p20,p21,p22,p23);printf(%d,%d,%d,%dn,p00,p01,p02,p03);printf(%d,%d,%d,%dn,p10,p11,p12,p13);els
14、eprintf(%d,%d,%d,%dn,p20,p21,p22,p23);printf(%d,%d,%d,%dn,p10,p11,p12,p13);printf(%d,%d,%d,%dn,p00,p01,p02,p03);int main()int i,j;int a36=0;for(i=0;i3;i+)for(j=0;j4;j+)scanf(%d,&aij);average(a,i);Print(a,3);return 0;统计字符串出现次数*include *include .z.-int main()char str1100,str2100;int flag=0,counter=0,l
15、1,l2,i,j;gets(str1);gets(str2);l1=strlen(str1);l2=strlen(str2);for(i=0;il1;i+)if(str1i=str20)for(j=1;jl2;j+)if(str1i+j!=str2j)break;else flag=1;if(flag)counter+;if(counter)printf(%dn,counter);elseprintf(Non);return 0;.z.-成绩统计求平均分*includemain()printf(wanglei,86.75n);printf(lihong,86.50n);printf(zhang
16、li,79.50n);printf(liuming,83n);printf(AVERAGE:84.50,86.75,82.50,82n);加密*include*includeint l;void gm(char*a);main()char s100=0;gets(s);l=strlen(s);gm(s);void gm(char*a)int i;char d100=0;for(i=0;il;i+)switch(ai)casea:di=d;break;caseb:di=w;break;casec:di=k;break;cased:di=;break;casee:di=i;break;casei:
17、di=a;break;casek:di=b;break;case;:di=c;break;casew:di=e;break;default:di=ai;puts(d);.z.-学生成绩*includestdio.hstruct adeint no;int am;int bm;int cm;struct ade N3=1,70,80,90,2,75,85,95,3,88,84,65;main()char s300;int i=0,k,m;for(i=0;i3;i+)scanf(%c,&si);if(si=n) break;if(s0=m)printf(2);elseif(s0=1|s0=2|s0
18、=3)k=N(int)(s0-1).am+N(int)(s0-1).bm+N(int)(s0-1).cm;m=k/3;printf(%d,m);else printf(0);字母储存*include*includetypedef struct Char_ char ch;char intch;struct Char_ *ne*t;CHAR_;int main(void)CHAR_ *node = NULL;CHAR_ *ch_ = NULL;CHAR_ *hear = NULL;char zimu = a;char panduanzimu;char record;for (; zimu !=
19、e; zimu+)ch_ = (CHAR_ *)malloc(sizeof(CHAR_);if (ch_ = NULL) e*it(0); ch_-ch = zimu;ch_-ne*t = NULL;if (node = NULL) node = ch_; else.z.-node-ne*t = ch_;node = node-ne*t; if (hear = NULL) hear = node; for (zimu -= 2; zimu != (a - 1); zimu-)ch_ = (CHAR_ *)malloc(sizeof(CHAR_);if (ch_ = NULL) e*it(0);
20、 ch_-ch = zimu;ch_-ne*t = NULL;node-ne*t = ch_;node = node-ne*t;for (node = hear, zimu = 0; node != NULL; node = node-ne*t, zimu+)node-intch = zimu;scanf(%c, &panduanzimu);node = hear;if (panduanzimu = 0&panduanzimu intch) printf(%c, node-ch); break; node = node-ne*t;else if (panduanzimu = a&panduan
21、zimu ch)record = node-intch;node = node-ne*t;printf(%c, record);else printf(N);.z.-node = hear;while (node != NULL)hear = node-ne*t;free(node);node = hear;return 0;链表合并*include*includeint main()static int i,j,m,n;static char q,*100,y100,z100;gets(*);gets(y);strcat(*,y);m=strlen(*);for(j=0;jm;j+)for(
22、i=0;i*j+i)*j=*j+i;*j+i=q;.z.-for(i=0;im;i+)for(j=0;j0;i-)if(isalpha(zi)m=i;break;for(i=0;i=m;i+)if(zi= )continue;elseif(i=m)printf(%cn,zi);.z.-elseprintf(%c ,zi);return 0;解析字符串*include *include int main()int i,j,k=0;char a100,b100,c100;gets(a);for(i=0;ai!=n;i+)printf(%c,ai);k+;printf(n);for(i=k+1;ai
23、!=0&ai!=n;i+)printf(%c,ai);k+;if(ai=n&ai+1!=0)for(i=k+2;ai!=0;+i)printf(%c,ai);字符串反向*include.z.-*include int main ( ) char str80, c;int i, j, n;gets(str);n = strlen(str);for ( i=0, j=n-1; ij; i+, j- )c= stri;stri = strj;strj = c;puts(str);return 0;基本的四则运算*include int main()long long a,b;char op;scan
24、f(%lld%c%lld,&a,&op,&b);switch(op)case +:printf(%lldn,a+b);break;case -:printf(%lldn,a-b);break;case *:printf(%lldn,a*b);break;.z.-case /:printf(%lldn,a/b);break;case %:printf(%lldn,a%b);break;return 0;复杂的四则运算*include *include int main()static char *100;int i;scanf(%s,&*);for(i=0;i=0&*i=9)continue;if(*i=*)*i-1=(*i-1-0)*(*i+1-0)+0;*i+1=0;i+;else if(*i=/)*i-1=(*i-1-0)/(*i+1-0)+0;*i+1=0;i+;for(i=1;i=0&*i=9)if(*i-1=+)*0+=(*i-0);.z.-else if(*i-1=-)*0-=(*i-0);elsecontinue;printf(%cn,*0);return 0;.z.