C語言程序設(shè)計慕課下編譯答案(完美)_第1頁
C語言程序設(shè)計慕課下編譯答案(完美)_第2頁
C語言程序設(shè)計慕課下編譯答案(完美)_第3頁
C語言程序設(shè)計慕課下編譯答案(完美)_第4頁
C語言程序設(shè)計慕課下編譯答案(完美)_第5頁
已閱讀5頁,還剩13頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、1求最大公約數(shù)和最小公倍數(shù)(15分)輾轉(zhuǎn)相除法求m/n的公約數(shù)最小公倍數(shù)#include <stdio.h> int fun1( int m, int n) / int r;while (n)r=m % n;m=n;n=r;return m;int fun2(int m,int n) /return marr = key;n/fun1(m,n);int main()int a,b;scanf("%d,%d",&a,&b); 輸入printf("最大公Z數(shù): dn 最小公倍數(shù):dn",fun1(a,b),fun2(a,b);re

2、turn 0;2排序并插入(15分)#include <stdio.h>void InsertionSort(int* arr, int n) /插入排序int *last = 0, *next = 0, key = 0, i = 0;for (i = 1; i < n; +i) next = arr + i, key = *next;if (key < *arr) / 將待插的數(shù)據(jù)比第一個數(shù)據(jù)小,直接插到最前面。for (last = next; next != arr; next = last) *next = *-last;else /否則從后往前遍歷,找到第一個

3、小于關(guān)鍵字的位置插入。for (last = next; key < *-last; next = last)*next = *last;*next = key; void OutPutArray(const int* arr, int n) / int i;for (i = 0; i < n; +i)printf("%d", arri);if(i<n-1)printf(",");printf("n"); void InsertNumber(int* arr, int n)int key = 0, i = 0;Ins

4、ertionSort(arr, n - 1); / OutPutArray(arr, n - 1); /打印一個數(shù)組對之前的n-1個數(shù)排序這里可以打印出來看一下對之前的數(shù)據(jù)排序是否正scanf("%d", &key); /輸入一個數(shù)確參照之前的插入排序原理,將這個數(shù)插入到數(shù)組。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

5、()int arr10 = 0 ;scanf("%d,%d,%d,%d,%d,%d,%d,%d,%d"/輸入 9 個數(shù),arr + 0, arr + 1, arr + 2, arr + 3, arr + 4, arr + 5, arr + 6, arr + 7,調(diào)用函數(shù),將輸入的第 10個數(shù)插進去。arr + 8);InsertNumber(arr, 10); /OutPutArray(arr, 10); /輸出輾轉(zhuǎn)相除法求m/n的公約數(shù)輸入return (0);3 .最大公約數(shù)#include <stdio.h>int fun1( int m, int n)

6、/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 .奇數(shù)球和#include<stdio.h>int main() int n;int sum(int n);scanf("%d",&n);printf("%dn",sum(n);int sum(int n) int b;if(n>1)b=sum(

7、n-1)+2*n-1;if(n=1)b=1;return b;5 .巧算自然數(shù)#include<stdio.h>int step = 1;void fc(int n)/輸出n這個數(shù)并按角谷猜想對n做處理以便進入下一步。返回值是總步數(shù)。if(n = 1)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, char* argv) in

8、t n;scanf("%d",&n);fc(n);printf("nstep=%dn",step);return 0;6 .賣鴨子#include <stdio.h>int main()static int i,x8;x7=2;for(i=7;i>=0;i-)xi-1=(xi+1)*2;printf("sum=%dn",x0);for(i=0;i<7;i+)printf("sell=%d,",xi-xi+1); printf("n");return 0;7 .各位

9、數(shù)字之和int add(int s)if (s<10) return s; else return s%10+add(s/10);int main(int argc, char *argv)int a,b;scanf("%d",&a);b=add(a);printf("%d",b);return 0;對稱字符串#include <stdio.h>#include <string.h> main() char a100;int i,j; gets(a);i=0;j=strlen(a)-1;while(i<j)if

10、(ai=aj)i+;j-;elsebreak;if(i>=j) printf("YES'n"); else printf("NO'n");指針排序#include <stdio.h>void sort(int *p,int n);void sort(int *p,int n)int i,j;int temp=0;for(i=0;i<n;i+)for(j=0;j<n;j+)if(pj>pj+1)temp=pj;pj=pj+1;pj+1=temp;int main()int i;int a10;for(i=

11、0;i<10;i+)scanf("%d”,&ai);int *p=a;sort(a,10);for(i=0;i<10-1;i+)printf("%d,",*(p+);printf("%dn",*p);return 0;查找數(shù)#include <stdio.h>void find(int *p,int element,int n); void find(int *p,int element,int n) int i,flag=0;for(i=0;i<n;i+)flag+;if(element = pi)pri

12、ntf("%dn",pi);break;if(flag = n)printf("No'n");int main()int i;int a10;for(i=0;i<10;i+)scanf("%d”,&ai);int *p=a;int num;scanf("%d",&num);find(p,num,10);return 0;輸出指定成績#include <stdio.h>int main()int grade34,i,j,(*p)4=grade,n;float ave=0;for(i=0

13、;i<3;i+)for(j=0;j<4;j+)scanf("%d”,&gradeij);scanf("%d",&n);for(i=0;i<3;i+)printf("%d ”,*(*(p+n-1)+i);printf("%dn",*(*(p+n-1)+3);for(i=0;i<4;i+)ave+=*(*(p+ni)+i);ave/=4;if(ave-(int)ave)printf("%.2fn",ave);elseprintf("%dn",(int)ave)

14、;return 0;成績排序#include <stdio.h>void average(int p口6,int n);void average(int p口6,int n)int i;for(i=0;i<4;i+)Pn4+=pni;Pn5=Pn4/4;void Print(int p6,int n)int max1=p05;int max2=p15;int max3=p25;int i;if(max1>max2 && max1>max3)if(max2>max3)printf("%d,%d,%d,%dn",p00,p01

15、,p2,p03);printf("%d,%d,%d,%dn",p10,p11,p12,p13);printf("%d,%d,%d,%dn",p20,p21,p22,p23);elseprintf("%d,%d,%d,%dn",p00,p01,p2,p03);printf("%d,%d,%d,%dn",p20,p21,p22,p23); printf("%d,%d,%d,%dn",p10,p11,p12,p13);else if(max2>max1 && max2>ma

16、x3)if(max1>max3)printf("%d,%d,%d,%dn",p10,p11,p12,p13);printf("%d,%d,%d,%dn",p00,p01,p2,p03); printf("%d,%d,%d,%dn",p20,p21,p22,p23);elseprintf("%d,%d,%d,%dn",p10,p11,p12,p13);printf("%d,%d,%d,%dn",p20,p21,p22,p23); printf("%d,%d,%d,%dn"

17、,p00,p01,p2,p03);else if(max3>max1 && max3>max2)if(max1>max2)printf("%d,%d,%d,%dn",p20,p21,p22,p23);printf("%d,%d,%d,%dn",p00,p01,p2,p03);printf("%d,%d,%d,%dn",p10,p11,p12,p13);elseprintf("%d,%d,%d,%dn",p20,p21,p22,p23);printf("%d,%d,%d,%

18、dn",p10,p11,p12,p13);printf("%d,%d,%d,%dn",p00,p01,p2,p03);int main()int i,j;int a36=0;for(i=0;i<3;i+)for(j=0;j<4;j+)scanf("%d",&aij);average(a,i);Print(a,3);return 0;統(tǒng)計字符串出現(xiàn)次數(shù)#include <stdio.h>#include <string.h>int main()char str1100,str2100;int flag=

19、0,counter=0,l1,l2,i,j;gets(str1);gets(str2);I1=strlen(str1);l2=strlen(str2);for(i=0;i<l1;i+)if(str1i=str20)for(j=1;j<l2;j+)if(str1i+j!=str2j) break;else flag=1;if(flag)counter+;if(counter)printf("%dn",counter);elseprintf("Non");return 0;成績統(tǒng)計求平均分#include<stdio.h>main()

20、printf("wanglei,86.75'n");printf("lihong,86.50n");printf("zhangli,79.50'n");printf("liuming,83'n");printf("AVERAGE:84.50,86.75,82.50,82'n");加密#include<stdio.h>#include<string.h>int l;void gm(char*a);main()char s100=0;gets(

21、s);l=strlen(s);gm(s);void gm(char*a)int i;char d100=0;for(i=0;i<l;i+)switch(ai)case'a':di='d'break; case'b':di='w'break; case'c':di尸k'break; case'd':di=''break; case'e':di='i'break; case'i':di='a'break; ca

22、se'k':di尸b'break;case'':di='c'break;case'w':di='e'break;default:di=ai;puts(d);學(xué)生成績#include"stdio.h"struct 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;i<3;i+) scanf("%c”

23、,&si);if(si='n') break;if(s0='m')printf("2");elseif(s0='1'|s0=211s0='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<stdio.h>#include<stdli

24、b.h>typedef struct Char char ch;char intch;struct Char *next; | CHAR;int main(void) CHAR *node = NULL;CHAR_ *ch_ = NULL;CHAR *hear = NULL;char zimu = 'a' char panduanzimu;char record;for (; zimu != 'e' zimu+)ch_ = (CHAR_ *)malloc(sizeof(CHAR_);if (ch_ = NULL) exit(0); ch_->ch =

25、 zimu;ch_->next = NULL;if (node = NULL) node = ch_; elsenode->next = ch_;node = node->next; if (hear = NULL) hear = node; for (zimu -= 2; zimu != ('a' - 1); zimu-)ch_ = (CHAR_ *)malloc(sizeof(CHAR_);if (ch = NULL) exit(0); ch_->ch = zimu;ch->next = NULL;node->next = ch_;nod

26、e = node->next;for (node = hear, zimu = '0' node != NULL; node = node->next, zimu+)node->intch = zimu;scanf("%c", &panduanzimu);node = hear;if (panduanzimu >= '0'&&panduanzimu <= '6')while (node != NULL) if (panduanzimu = node->intch) p

27、rintf("%c", node->ch); break; |node = node->next;else if (panduanzimu >= 'a'&&panduanzimu <= 'd') while (node != NULL)if (panduanzimu = node->ch)record = node->intch;node = node->next; Iprintf("%c", record);else printf("N");n

28、ode = hear;while (node != NULL)hear = node->next;free(node);node = hear;return 0;鏈表合并#include<stdio.h>#include<string.h>int main()static int i,j,m,n;static char q,x100,y100,z100;gets(x);gets(y);strcat(x,y);m=strlen(x);for(j=0;j<m;j+)for(i=0;i<m-j;i+)q=xj;if(xj>xj+i)xj=xj+i; x

29、j+i=q;)for(i=0;i<m;i+)for(j=0;j<=i;j+)if(zj=xi)break;if(j=i)zn=xi;n=strlen(z);)for(i=m-1;i>0;i-)if(isalpha(zi)m=i;break;)for(i=0;i<=m;i+)if(zi=' ')continue;elseif(i=m)printf("%cn",zi);elseprintf("%c ",zi);return 0;解析字符串#include <stdio.h>#include <stdl

30、ib.h>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!='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<stdio.h>#include <string.h>int main () char str80, c;int i, j, n; gets(str); n = strlen(str);for ( i=0, j=n-1; i<j; i+, j-) c= stri;stri = stjst

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論