程序設(shè)計(jì)入門-C語言-翁愷_第1頁
程序設(shè)計(jì)入門-C語言-翁愷_第2頁
程序設(shè)計(jì)入門-C語言-翁愷_第3頁
程序設(shè)計(jì)入門-C語言-翁愷_第4頁
程序設(shè)計(jì)入門-C語言-翁愷_第5頁
已閱讀5頁,還剩34頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上程序設(shè)計(jì)入門-C語言 浙大 翁愷循環(huán)類型編程源代碼1、#include <stdio.h>int main()int a,b;int min;scanf("%d %d", &a, &b);if ( a<b ) min = a; else min = b;int ret = 0;int i;for ( i = 1; i < min; i+ ) if ( a%i = 0 ) if ( b%i = 0 ) ret = i;printf("%d和%d的最大公約數(shù)是%d.n", a, b, ret)

2、;return 0;2、#include <stdio.h>int main()int a,b;int t;scanf("%d %d", &a, &b);int origa = a;int origb = b;while ( b != 0 ) t = a%b;a = b;b = t;printf("%d和%d的最大公約數(shù)是%d.n", origa, origb, a);return 0;3、#include <stdio.h>int main()int x;scanf("%d", &x)

3、;int mask = 1;int t = x;while ( t>9 ) t /= 10;mask *=10; printf("x=%d, mask=%dn", x, mask);do int d = x / mask;printf("%d", d);if ( mask > 9 ) printf(" ");x %= mask;mask /= 10; while ( mask > 0 );printf("n");return 0;4、#include <stdio.h>int main

4、()int n;int i;double ret=0.0;scanf("%d", &n);for ( i=1; i<=n; i+ ) ret += 1.0/i;printf("%fn", ret);return 0;5、#include <stdio.h>int main()int n;int i;double ret=0.0;int sign = 1;scanf("%d", &n);for ( i=1; i<=n; i+ ) ret += 1.0*sign/i;sign = -sign;pri

5、ntf("%fn", ret);return 0;6、#include <stdio.h>int main()int x;int one, two, five;scanf("%d", &x);for ( one = 1; one < x*10; one+ ) for ( two = 1; two < x*10/2; two+ ) for ( five = 1; five < x*10/5; five+ ) if ( one + two*2 + five*5 = x*10 ) printf("可以用%d個(gè)1角

6、加%d個(gè)2角加%d個(gè)5角得到%d元n", one, two, five, x);break;return 0;7、#include <stdio.h>int main()int x;int one, two, five;int exit = 0;scanf("%d", &x);for ( one = 1; one < x*10; one+ ) for ( two = 1; two < x*10/2; two+ ) for ( five = 1; five < x*10/5; five+ ) if ( one + two*2 +

7、 five*5 = x*10 ) printf("可以用%d個(gè)1角加%d個(gè)2角加%d個(gè)5角得到%d元n", one, two, five, x);exit = 1;break;if ( exit = 1 ) break;if ( exit = 1 ) break;return 0;8、#include <stdio.h>int main()int x;int one, two, five;scanf("%d", &x);for ( one = 1; one < x*10; one+ ) for ( two = 1; two &l

8、t; x*10/2; two+ ) for ( five = 1; five < x*10/5; five+ ) if ( one + two*2 + five*5 = x*10 ) printf("可以用%d個(gè)1角加%d個(gè)2角加%d個(gè)5角得到%d元n", one, two, five, x);goto out;out:return 0;9、#include <stdio.h>int main()int x;scanf("%d", &x);int i;int isPrime = 1;/x是素?cái)?shù)for ( i=2; i<x;

9、 i+ ) if ( x % i = 0 ) isPrime = 0;break;if ( isPrime = 1 ) printf("是素?cái)?shù)n"); else printf("不是素?cái)?shù)n");return 0;10、#include <stdio.h>int main()int x;for ( x=1; x<=100; x+ ) int i;int isPrime = 1;/x是素?cái)?shù)for ( i=2; i<x; i+ ) if ( x % i = 0 ) isPrime = 0;break;if ( isPrime = 1 )

10、 printf("%d ", x); printf("n");return 0;11、#include <stdio.h>int main()int x;int cnt = 0;x = 1;while ( cnt <50 ) int i;int isPrime = 1;/x是素?cái)?shù)for ( i=2; i<x; i+ ) if ( x % i = 0 ) isPrime = 0;break;if ( isPrime = 1 ) cnt +;printf("%dt", x);if ( cnt %5 = 0 ) pr

11、intf("n"); x+;return 0;12、#include <stdio.h>int main()int sum = 0;int count = 0;int number;scanf("%d", &number);while ( number != -1 ) sum += number;count +;scanf("%d", &number);double dsum = sum;printf("The average is %f.n", dsum / count);return

12、 0;13、#include <stdio.h>int main()int n = 3;while ( n>= 0 ) printf("%d ", n);n-;printf("發(fā)射n");return 0;14、#include <stdio.h>int main()int number = rand()%100+1;int count = 0;int a = 0;printf("我已經(jīng)想好了一個(gè)1到100之間的數(shù)。");do printf("請(qǐng)猜這個(gè)1到100之間數(shù):");scanf

13、("%d", &a);if ( a > number ) printf("你猜的數(shù)大了。"); else if ( a < number ) printf("你猜的數(shù)小了。");count +; while (a != number);printf("太好了,你用了%d次就猜到了答案。n", count);return 0;15、/log2x#include <stdio.h>int main()int x;int ret = 0;scanf("%d", &

14、;x);int t = x;while ( x > 1 ) x /= 2;ret +;printf("log2 of %d is %d.", t, ret);return 0;16、#include <stdio.h>#include <stdlib.h>#include <time.h>int main()srand(time(0);int a = rand();printf("%dn", a%100);return 0;17、#include <stdio.h>int main()int x;sc

15、anf("%d", &x);int digit;int ret = 0;while ( x> 0 ) digit = x%10;printf("%d", digit);ret = ret*10 + digit;/ printf("x=%d,digit=%d,ret=%dn", x, digit, ret);x /= 10;/ printf("%d", ret);return 0;每周編程題目源代碼18、#include <stdio.h>int main() int i=0; scanf(

16、"%d",&i); printf("%dn",(i%10)*100+(i%100/10)*10+i/100); return 0;19、#include <stdio.h>int main() int h,m,input,b; scanf("%d",&input); h = input/100; m = input%100;if (h>=8) printf("%d",b = (h-8)*100+m); else printf("%d",b = (h+24-8)*

17、100+m); return 0; 20、#include<stdio.h>int main() int x; scanf("%d",&x); int m,n; m=x/10; n=x%10;switch(n) case 1: printf("Faint signals, barely perceptible");break; case 2: printf("Very weak signals");break; case 3: printf("Weak signals");break; case

18、 4: printf("Fair signals");break; case 5: printf("Fairly good signals");break; case 6: printf("Good signals");break; case 7: printf("Moderately strong signals");break; case 8: printf("Strong signals");break; case 9: printf("Extremely strong sign

19、als");break; default :printf("worng");break;printf(", ");switch(m) case 1: printf("unreadable");break; case 2: printf("barely readable, occasional words distinguishable");break; case 3: printf("readable with considerable difficulty");break; case

20、 4: printf("readable with practically no difficulty");break; case 5: printf("perfectly readable");break; default :printf("worng");break;printf(".");return 0;21、#include <stdio.h>int main() int x; int a=0,b=0; while (scanf("%d",&x) if (x=-1)

21、 break; if (x%2=0) a+; else b+; printf("%d %d",b,a); return 0;22、#include <stdio.h>int main() int x; scanf("%d",&x); int ans=0,k=1,cnt=1; while (x) int temp; if (x%2=k) temp=1; else temp=0; /printf("%d ",temp); ans=ans+temp*cnt; k=1-k; x/=10; cnt*=2; printf(&q

22、uot;%dn",ans); return 0;23、#include <stdio.h>#include <math.h>int a;int isprime(int x) if (x=2|x=3|x=5|x=7) return 1; for (int i=2;i*i<=x;i+) if (x%i=0) return 0; return 1;int main() int n,m; scanf("%d%d",&n,&m); int k=0; for (int i=2;k<=m;i+) if (isprime(i)=

23、1) ak+=i; int sum=0; for (int i=n-1;i<m;i+) sum+=ai; printf("%dn",sum ); return 0;24、#include <stdio.h>#include <string.h>char ans1210="ling","yi","er","san","si","wu","liu","qi","ba",&

24、quot;jiu"int main() int x; scanf("%d",&x); if (x=0) printf("%s",ansx); int flag=0; if (x<0) printf("fu "); x=-x; int a200; int k=0; while (x) ak+=x%10; x/=10; for (int i=k-1;i>=0;i-) if (flag=0) printf("%s",ansai); flag=1; else printf(" %s&

25、quot;,ansai); printf("n"); return 0;25、#include <stdio.h>int main() int a,b; scanf("%d/%d",&a,&b); printf("%d",a/b); a=a%b; if (a) printf("."); int k=0; while (a!=0 && k<200) if(a<b) a=a*10; printf("%d",a/b); k+; a=a%b; pr

26、intf("n"); return 0;判斷類型源代碼32、#include <stdio.h>int main()const int MINOR = 35;int age = 0;printf("請(qǐng)輸入你的年齡: ");scanf("%d", &age);printf("你的年齡是%d歲。n", age);if ( age < MINOR ) printf("年輕是美好的,");printf("年齡決定了你的精神世界,好好珍惜吧。n");retur

27、n 0;33、#include <stdio.h>int main()/初始化int price = 0;int bill = 0;/讀入金額和票面printf("請(qǐng)輸入金額:");scanf("%d", &price);printf("請(qǐng)輸入票面:");scanf("%d", &bill);/計(jì)算找零printf("應(yīng)該找您:%dn", bill - price);return 0;34、#include <stdio.h>int main()/初始化i

28、nt price = 0;int bill = 0;/讀入金額和票面printf("請(qǐng)輸入金額:");scanf("%d", &price);printf("請(qǐng)輸入票面:");scanf("%d", &bill);/計(jì)算找零if ( bill >= price ) printf("應(yīng)該找您:%dn", bill - price);return 0;35、#include <stdio.h>int main()/初始化int price = 0;int bill

29、= 0;/讀入金額和票面printf("請(qǐng)輸入金額:");scanf("%d", &price);printf("請(qǐng)輸入票面:");scanf("%d", &bill);/計(jì)算找零if ( bill >= price ) printf("應(yīng)該找您:%dn", bill - price); else printf("你的錢不夠n");return 0;36、#include <stdio.h>int main()int hour1, minut

30、e1;int hour2, minute2;scanf("%d %d", &hour1, &minute1);scanf("%d %d", &hour2, &minute2);int ih = hour2 - hour1;int im = minute2 - minute1;if ( im <0 ) im = 60 + im;ih -;printf("時(shí)間差是%d小時(shí)%d分。n", ih, im);return 0;37、#include <stdio.h>int main()int

31、a,b;printf("請(qǐng)輸入兩個(gè)整數(shù):");scanf("%d %d", &a, &b);int max = b;if ( a > b ) max = a;printf("大的那個(gè)是%dn", max);return 0;38、/max3.c#include <stdio.h>int main()int a,b,c;scanf("%d %d %d", &a, &b, &c);int max = 0;if ( a>b ) if ( a>c ) m

32、ax = a; else max = c; else if ( b>c ) max = b; else max = c;printf("The max is %dn", max);return 0;39、#include <stdio.h>int main()const double RATE = 8.25; const int STANDARD = 40; double pay = 0.0;int hours;printf("請(qǐng)輸入工作的小時(shí)數(shù): ");scanf("%d", &hours);printf(

33、"n");if (hours > STANDARD) pay = STANDARD * RATE + (hours-STANDARD) * (RATE * 1.5);else pay = hours * RATE;printf("應(yīng)付工資: %fn", pay);return 0;40、#include <stdio.h>int main()const int PASS=60;int score;printf("請(qǐng)輸入成績(jī): ");scanf("%d", &score);printf(&q

34、uot;你輸入的成績(jī)是%d.n", score);if ( score < PASS )printf("很遺憾,這個(gè)成績(jī)沒有及格。");else printf("祝賀你,這個(gè)成績(jī)及格了。");printf("再見n");return 0;判斷分支類型源代碼41、/max3.c#include <stdio.h>int main()int a,b,c;scanf("%d %d %d", &a, &b, &c);int max = 0;if ( a>b ) if

35、 ( a>c ) max = a; else max = c; else if ( b>c ) max = b; else max = c;printf("The max is %dn", max);return 0;42、#include <stdio.h>int main()int x;scanf("%d", &x);int f=0;if ( x < 0 ) f = -1; else if ( x = 0 ) f = 0; else if ( x >5 ) f = 2 * x; else f = 3*x;p

36、rintf("%dn", f);return 0;43、#include <stdio.h>int main()printf("輸入成績(jī)(0-100)");int grade;scanf("%d", &grade);grade /=10;switch ( grade ) case 10:case 9:printf("An");break;case 8:printf("Bn");break;case 7:printf("Cn");break;case 6:p

37、rintf("Dn");break;default:printf("Fn");break;return 0;44、#include <stdio.h>int main()printf("請(qǐng)輸入月份:");int month;scanf("%d", &month);switch ( month )case 1: printf("Januaryn"); break;case 2: printf("Februaryn"); break;case 3: print

38、f("Marchn"); break;case 4: printf("Apriln"); break;case 5: printf("Mayn"); break;case 6: printf("Junen"); break;case 7: printf("Julyn"); break;case 8: printf("Augustn"); break;case 9: printf("Septembern"); break;case 10:printf("Octobern"); break;case 11:printf("Novembern"); break;case 12:printf("Decembern"); break;return 0;初始計(jì)算型源代碼45、#include <stdio.h>int main()int a;int b;printf("請(qǐng)輸入兩個(gè)整數(shù):");scanf("%

溫馨提示

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

評(píng)論

0/150

提交評(píng)論