c語言初學必背代碼(共8頁)_第1頁
c語言初學必背代碼(共8頁)_第2頁
c語言初學必背代碼(共8頁)_第3頁
c語言初學必背代碼(共8頁)_第4頁
c語言初學必背代碼(共8頁)_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、/1.成績判斷#include int main()/成績int score;printf(請輸入你的成績:n);scanf(%d, &score);/判斷if(score =0 & score 60)printf(不及格n);else if(60 = score & score 80)printf(中等n);else if(80 = score & score 100)printf(優(yōu)秀n);elseprintf(輸入錯誤!n);/2.計算1到100的和#include int main()int sum = 0;/存結(jié)果變量int i;for(i=1;i = 100;i+)sum = su

2、m + i;printf(sum=%dn, sum);/3.最大公約數(shù)#include /求m,n的最大公約數(shù)int main()int m, n;int i, k;printf(請輸入兩個數(shù):);scanf(%d %d, &m, &n);/三元運算符找較小的那個k = m =1; i-)/這是公約數(shù)if(m % i = 0) & (n % i =0)printf(最大公約數(shù)是%dn, i);break;/跳出for循環(huán)/4.最小公倍數(shù)#include /求m,n的最小公倍數(shù)int main()int m, n;int max, min;/m,n中較大,較小的那個int k;/max, 2*m

3、ax, 3*max, .printf(請輸入兩個數(shù):);scanf(%d %d, &m, &n);/也可以交換m,n,保證m小n大max = m n ? m : n;min = m n ? m : n;k = max;/從max開始while(k % min != 0)k += max;/每次倍增printf(最小公倍數(shù)是%dn, k);/5.金字塔#include /金字塔int main()int i;/外層int j;/內(nèi)層for(i=1;i=10;i+)/當前是在第i行/先補空格10-i個for(j=1;j=10-i;j+)printf( );/再打2i-1個*for(j=1;j=2*

4、i-1;j+)printf(*);printf(n);/6.九九乘法表#include /打印九九乘法表int main()int i,j;for(i=1;i=9;i+)/外層一定是9行for(j=1; j=i; j+)/內(nèi)層第幾行走幾遍printf(%d*%d=%d , i, j, i*j);printf(n);/7.百錢買百雞#include /*百錢買百雞,類似1,2,5湊100銀幣問題*/int main2()int i,j;/公雞,母雞個數(shù)for(i=0; i=20; i+)/公雞for(j=0; j=33; j+)/母雞if( (15*i + 9*j + (100-i-j) = 3

5、00)printf(公雞%d,母雞%d,小雞%dn, i, j, 100-i-j);/1,2,5湊100銀幣問題int main3()int count = 0;/情況數(shù)int i,j;/5分個數(shù),2分個數(shù)for(i=0; i=20; i+)/5分個數(shù)for(j=0; j=50; j+)/2分個數(shù)if( ( 5*i + 2*j ) = 100 ) count+;printf(%d: 5分%d個,2分%d個,1分%d個n, count, i, j, 100-5*i-2*j);/8.一維數(shù)組的最大值、最小值、平均值#include #define N 10/宏定義常量int main()int i

6、;/下標索引int max, min;double sum = 0;/累加和int aN = 58, 58, 96, 100, 25, 55, 66, 88, 99, 77;max = a0;/假設(shè)第一個最大min = a0;/假設(shè)第一個最小for(i=1; i max)/比最大值還大max = ai;/你才是最大if(ai min)/比最小值還小min = ai;/你才是最小sum += ai;printf(max=%d, min=%dn, max, min);printf(average = %.2lfn, sum/N);/9.二維數(shù)組的最大值、最小值、平均值#include int ma

7、in()int i; /第幾行int j; /第幾列int a34 = 1,2,3,4, 5,-6,7,8, 9,19,39,0;int max = a00;/假設(shè)你最大int min = a00;/假設(shè)你最小double average;/平均值double sum = 0; /總和for(i=0; i3; i+)/必定3行for(j=0; j max)max = aij;if(aij min)min = aij;printf(n);average = sum / (3*4);printf(max=%d, min=%d, avg=%.2lfn, max, min, average);/10.

8、二維數(shù)組轉(zhuǎn)置#include /二維數(shù)組轉(zhuǎn)置:行變列,列變行int main()int i; /第幾行int j; /第幾列int a34 = 1,2,3,4, 5,-6,7,8, 9,19,39,0;int b43;for(i=0; i3; i+)for(j=0; j4; j+)printf(%5d, aij);printf(n);/矩陣轉(zhuǎn)置for(i=0; i3; i+)for(j=0; j4; j+)bji = aij;for(i=0; i4; i+)for(j=0; j3; j+)printf(%5d, bij);printf(n);/11.冒泡排序#include #define N

9、 10/宏定義常量int main()int i;/下標索引int j;int tmp;/臨時交換用int aN = 58, 58, 96, 100, 25, 55, 66, 88, 99, 77;/外層循環(huán)一定是N-1for(i=0; iN-1; i+)/兩兩交換,大的往后走for(j=0; j aj+1)tmp = aj;aj = aj+1;aj+1 = tmp;for(i=0; iN; i+)printf(%d , ai);printf(n);/12.結(jié)構(gòu)冒泡排序#include /結(jié)構(gòu)定義,用戶自定義類型typedef struct studentchar sno20;/學號char

10、name20;/姓名int age;/年齡char gender;/性別char tel20;/電話;int main()int i;int j;double sum = 0;struct student tmp;/兩兩交換臨時用;/結(jié)構(gòu)數(shù)組struct student team5;for(i=0; i5; i+)printf(請輸入第%d個隊員的信息:n, i+1);scanf(%s %s %d %c %s, teami.sno, , &teami.age, &teami.gender, teami.tel);/按年齡冒泡排序for(i=0; i5; i+)for(j=0

11、; j teamj+1.age)tmp = teamj;teamj = teamj+1;teamj+1 = tmp;/取值printf(%-12s %-10s %-5s %-5s %-15sn, 學號, 姓名, 年齡, 性別, 電話);for(i=0; i5; i+)printf(%-12s %-10s %-5d %-5c %-15sn, teami.sno, , teami.age, teami.gender, teami.tel);/13.結(jié)構(gòu)數(shù)組找年齡最大值#include /結(jié)構(gòu)定義,用戶自定義類型typedef struct studentchar sno20;/

12、學號char name20;/姓名int age;/年齡char gender;/性別char tel20;/電話;int main()int i;struct student tmp;/找最大臨時用/結(jié)構(gòu)數(shù)組struct student team5;for(i=0; i5; i+)printf(請輸入第%d個隊員的信息:n, i+1);scanf(%s %s %d %c %s, teami.sno, , &teami.age, &teami.gender, teami.tel);/取值printf(%-12s %-10s %-5s %-5s %-15sn, 學號, 姓名,

13、 年齡, 性別, 電話);for(i=0; i5; i+)printf(%-12s %-10s %-5d %-5c %-15sn, teami.sno, , teami.age, teami.gender, teami.tel);/找學號最大的那一個tmp = team0;for(i=1; i 0 )tmp = teami;printf(學號最大的隊員如下:n);printf(%-12s %-10s %-5d %-5c %-15sn, tmp.sno, , tmp.age, tmp.gender, tmp.tel);/14.文件讀寫#include #inc

14、lude /結(jié)構(gòu)定義,用戶自定義類型typedef struct studentchar sno20;/學號char name20;/姓名int age;/年齡char gender;/性別char tel20;/電話;/文件讀寫int main()struct student * s, * p1;/個數(shù)未知FILE * fp;int i, n = 0;char buf1024;/fgets緩沖區(qū)/打開文件fp = fopen(e:test.txt, r);while(fgets(buf, 1024, fp) != NULL)n+;fclose(fp);/指向一個可以存儲n個student結(jié)構(gòu)

15、的內(nèi)存空間s = (struct student *)malloc(sizeof(struct student) * n);p1 = s;/不要動頭位置s的值/打開文件fp = fopen(e:test.txt, r);for(i=0; isno, p1-name, &p1-age, &p1-gender, p1-tel);p1+;fclose(fp);p1 = s;for(i=0; isno, p1-name, p1-age, p1-gender, p1-tel);free(s);/15.輸入三角形三邊長計算周長和面積#include #include int main() double a

16、rea,perimeter,s,a,b,c; printf(請輸入三邊長 a b c:); scanf(%lf%lf%lf,&a,&b,&c); if(a+bc) & (a+cb) & (b+ca) s=(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c); perimeter=a+b+c; printf(area=%.2f,perimeter=%.2fn,area,perimeter); else printf(三邊長無法構(gòu)成三角形。n); return 0; /16.輸入年份和月份輸出對應那一月的天數(shù),要處理閏年#includeint main()int yea

17、r,month;int leap =0;scanf(%d %d,&year,&month);if(year %4=0 & year %100 !=0) | year %400 =0) /閏年2月29天leap=1;switch(month)case 1:case 3:case 5:case 7:case 8:case 10:case 12:printf(31);break;case 4:case 6:case 9:case 11:printf(30);break;case 2:printf(%d,28+leap);break;default:printf(error);break;return

18、 0;/17.1,2,5分硬幣問題#include int main()int sum = 0;for(int i=0; i=20; i+)/5分for(int j=0; j=50; j+)/2分if(5*i + 2*j = 100)sum+;elsebreak;printf(%dn, sum); /18.100到200所有的素數(shù)#include int main()int i,j,n;/外層for(i=100; i=200; i+)n = i;/內(nèi)層for(j=2;j=n-1;j+)if(n%j = 0)break;/不是素數(shù)if(j=n)/沒找到printf(%d是素數(shù)n, n);elseprintf(%d不是素數(shù)n, n);/19.輸入一個數(shù)n,計算12+22+32+n2#include int main()int sum = 0;int i,

溫馨提示

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

評論

0/150

提交評論