如果已知英制長度的英尺foot和英寸inch的值,那么對應的米是(foot+inch/12)×0.3048。現在,如果用戶輸入的是厘米數,那么對應英制長度的英尺和英寸是多少呢?別忘了1英尺等於12英寸。
輸入格式:
輸入在一行中給出1個正整數,單位是厘米。
輸出格式:
在一行中輸出這個厘米數對應英制長度的英尺和英寸的整數值,中間用空格分開。
輸入樣例:
170
輸出樣例:
5 6
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 int main() 6 { 7 int x; 8 scanf("%d",&x); 9 int inch = 0.394*x; 10 printf("%d %d", inch/12, inch%12); 11 return 0; 12 }
有時候人們用四位數字表示一個時間,比如1106表示11點零6分。現在,你的程序要根據起始時間和流逝的時間計算出終止時間。
讀入兩個數字,第一個數字以這樣的四位數字表示當前時間,第二個數字表示分鍾數,計算當前時間經過那么多分鍾后是幾點,結果也表示為四位數字。當小時為個位數時,沒有前導的零,即5點30分表示為530。注意,第二個數字表示的分鍾數可能超過60,也可能是負數。
輸入格式:
輸入在一行中給出2個整數,分別是四位數字表示的起始時間、以及流逝的分鍾數,其間以空格分隔。注意:在起始時間中,當小時為個位數時,沒有前導的零,即5點30分表示為530;流逝的分鍾數可能超過60,也可能是負數。
輸出格式:
輸出四位數字表示的終止時間,當小時為個位數時,沒有前導的零。題目保證起始時間和終止時間在同一天內。
輸入樣例:
1120 110
輸出樣例:
1310
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 int main() 6 { 7 int time, minute; 8 scanf("%d%d",& time,&minute); 9 int t = time/100*60 + time%100 + minute; 10 printf("%d", t/60*100 + t%60); 11 return 0; 12 }
程序每次讀入一個正3位數,然后輸出按位逆序的數字。注意:當輸入的數字含有結尾的0時,輸出不應帶有前導的0。比如輸入700,輸出應該是7。
輸入格式:
每個測試是一個3位的正整數。
輸出格式:
輸出按位逆序的數。
輸入樣例:
123
輸出樣例:
321
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 int main() 6 { 7 int x; 8 scanf("%d",&x); 9 int sum = 0; 10 while(x) 11 { 12 sum = sum*10 + x%10; 13 x /= 10; 14 } 15 printf("%d", sum); 16 return 0; 17 }
BCD數是用一個字節來表達兩位十進制的數,每四個比特表示一位。所以如果一個BCD數的十六進制是0x12,它表達的就是十進制的12。但是小明沒學過BCD,把所有的BCD數都當作二進制數轉換成十進制輸出了。於是BCD的0x12被輸出成了十進制的18了!
現在,你的程序要讀入這個錯誤的十進制數,然后輸出正確的十進制數。提示:你可以把18轉換回0x12,然后再轉換回12。
輸入格式:
輸入在一行中給出一個[0, 153]范圍內的正整數,保證能轉換回有效的BCD數,也就是說這個整數轉換成十六進制時不會出現A-F的數字。
輸出格式:
輸出對應的十進制數。
輸入樣例:
18
輸出樣例:
12
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 int main() 6 { 7 int x; 8 scanf("%d",&x); 9 printf("%d", x/16*10+x%16); 10 return 0; 11 }
本題要求編寫程序,按照規定格式輸出表格。
輸入格式:
本題目沒有輸入。
輸出格式:
要求嚴格按照給出的格式輸出下列表格:
------------------------------------ Province Area(km2) Pop.(10K) ------------------------------------ Anhui 139600.00 6461.00 Beijing 16410.54 1180.70 Chongqing 82400.00 3144.23 Shanghai 6340.50 1360.26 Zhejiang 101800.00 4894.00 ------------------------------------
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 int main() 6 { 7 printf("------------------------------------\n"); 8 printf("Province Area(km2) Pop.(10K)\n"); 9 printf("------------------------------------\n"); 10 printf("Anhui 139600.00 6461.00\n"); 11 printf("Beijing 16410.54 1180.70\n"); 12 printf("Chongqing 82400.00 3144.23\n"); 13 printf("Shanghai 6340.50 1360.26\n"); 14 printf("Zhejiang 101800.00 4894.00\n"); 15 printf("------------------------------------\n"); 16 return 0; 17 }
本題要求編寫程序,順序讀入浮點數1、整數、字符、浮點數2,再按照字符、整數、浮點數1、浮點數2的順序輸出。
輸入格式:
輸入在一行中順序給出浮點數1、整數、字符、浮點數2,其間以1個空格分隔。
輸出格式:
在一行中按照字符、整數、浮點數1、浮點數2的順序輸出,其中浮點數保留小數點后2位。
輸入樣例:
2.12 88 c 4.7
輸出樣例:
c 88 2.12 4.70
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 int main() 6 { 7 float f1,f2; 8 int i; 9 char c; 10 scanf("%f%d%*c%c%f",&f1,&i,&c,&f2); 11 printf("%c %i %.2f %.2f",c,i,f1,f2); 12 13 return 0; 14 }
編寫一個程序,要求用戶輸入24小時制的時間,然后顯示12小時制的時間。
輸入格式:
輸入在一行中給出帶有中間的:符號(半角的冒號)的24小時制的時間,如12:34表示12點34分。當小時或分鍾數小於10時,均沒有前導的零,如5:6表示5點零6分。
提示:在scanf的格式字符串中加入:,讓scanf來處理這個冒號。
輸出格式:
在一行中輸出這個時間對應的12小時制的時間,數字部分格式與輸入的相同,然后跟上空格,再跟上表示上午的字符串AM或表示下午的字符串PM。如5:6 PM表示下午5點零6分。注意,在英文的習慣中,中午12點被認為是下午,所以24小時制的12:00就是12小時制的12:0 PM;而0點被認為是第二天的時間,所以是0:0 AM。
輸入樣例:
21:11
輸出樣例:
9:11 PM
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 int main() 6 { 7 int h,m; 8 scanf("%d:%d",&h,&m); 9 if(h<12) 10 printf("%d:%d AM",h,m); 11 else if(h==12) 12 printf("%d:%d PM",h,m); 13 else 14 printf("%d:%d PM",h-12,m); 15 16 return 0; 17 }
模擬交通警察的雷達測速儀。輸入汽車速度,如果速度超出60 mph,則顯示“Speeding”,否則顯示“OK”。
輸入格式:
輸入在一行中給出1個不超過500的非負整數,即雷達測到的車速。
輸出格式:
在一行中輸出測速儀顯示結果,格式為:Speed: V - S,其中V是車速,S或者是Speeding、或者是OK。
輸入樣例1:
40
輸出樣例1:
Speed: 40 - OK
輸入樣例2:
75
輸出樣例2:
Speed: 75 - Speeding
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 int main() 6 { 7 int speed; 8 scanf("%d",&speed); 9 if(speed<=60) 10 printf("Speed: %d - OK",speed); 11 else 12 printf("Speed: %d - Speeding",speed); 13 14 return 0; 15 }
三個球A、B、C,大小形狀相同且其中有一個球與其他球重量不同。要求找出這個不一樣的球。
輸入格式:
輸入在一行中給出3個正整數,順序對應球A、B、C的重量。
輸出格式:
在一行中輸出唯一的那個不一樣的球。
輸入樣例:
1 1 2
輸出樣例:
C
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 int main() 6 { 7 int A,B,C; 8 scanf("%d%d%d",&A,&B,&C); 9 if(A==B) 10 printf("C"); 11 else{ 12 if(A==C) 13 printf("B"); 14 else 15 printf("A"); 16 } 17 18 return 0; 19 }
某公司員工的工資計算方法如下:一周內工作時間不超過40小時,按正常工作時間計酬;超出40小時的工作時間部分,按正常工作時間報酬的1.5倍計酬。員工按進公司時間分為新職工和老職工,進公司不少於5年的員工為老職工,5年以下的為新職工。新職工的正常工資為30元/小時,老職工的正常工資為50元/小時。請按該計酬方式計算員工的工資。
輸入格式:
輸入在一行中給出2個正整數,分別為某員工入職年數和周工作時間,其間以空格分隔。
輸出格式:
在一行輸出該員工的周薪,精確到小數點后2位。
輸入樣例1:
5 40
輸出樣例1:
2000.00
輸入樣例2:
3 50
輸出樣例2:
1650.00
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 int main() 6 { 7 int y,h; 8 scanf("%d%d",&y,&h); 9 10 int hs; 11 if(y>=5) 12 hs = 50; 13 else 14 hs = 30; 15 16 double ws; 17 if(h<40) 18 ws = hs*h; 19 else 20 ws = hs*(40+(h-40)*1.5); 21 22 printf("%.2f",ws); 23 24 return 0; 25 }
7-11 分段計算居民水費
為鼓勵居民節約用水,自來水公司采取按用水量階梯式計價的辦法,居民應交水費y(元)與月用水量x(噸)相關:當x不超過15噸時,y = 4x/3;超過后,y = 2.5x - 17.5。請編寫程序實現水費的計算。
輸入格式:
輸入在一行中給出非負實數x。
輸出格式:
在一行輸出應交的水費,精確到小數點后2位。
輸入樣例1:
12
輸出樣例1:
16.00
輸入樣例2:
16
輸出樣例2:
22.50
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 int main() 6 { 7 int x; 8 scanf("%d",&x); 9 10 double y; 11 if(x<15) 12 y = 4*x/3.0; 13 else 14 y = 2.5*x - 17.5; 15 16 printf("%.2f",y); 17 18 return 0; 19 }
本題要求編寫一個簡單計算器程序,可根據輸入的運算符,對2個整數進行加、減、乘、除或求余運算。題目保證輸入和輸出均不超過整型范圍。
輸入格式:
輸入在一行中依次輸入操作數1、運算符、操作數2,其間以1個空格分隔。操作數的數據類型為整型,且保證除法和求余的分母非零。
輸出格式:
當運算符為+、-、*、/、%時,在一行輸出相應的運算結果。若輸入是非法符號(即除了加、減、乘、除和求余五種運算符以外的其他符號)則輸出ERROR。
輸入樣例1:
-7 / 2
輸出樣例1:
-3
輸入樣例2:
3 & 6
輸出樣例2:
ERROR
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 int main() 6 { 7 int a,b; 8 char c; 9 scanf("%d%*c%c%d",&a,&c,&b); 10 11 switch(c) 12 { 13 case '+': 14 printf("%d",a+b); 15 break; 16 case '-': 17 printf("%d",a-b); 18 break; 19 case '*': 20 printf("%d",a*b); 21 break; 22 case '/': 23 printf("%d",a/b); 24 break; 25 case '%': 26 printf("%d",a%b); 27 break; 28 default: 29 printf("ERROR"); 30 } 31 32 return 0; 33 }
股票價格漲跌趨勢,常用蠟燭圖技術中的K線圖來表示,分為按日的日K線、按周的周K線、按月的月K線等。以日K線為例,每天股票價格從開盤到收盤走完一天,對應一根蠟燭小圖,要表示四個價格:開盤價格Open(早上剛剛開始開盤買賣成交的第1筆價格)、收盤價格Close(下午收盤時最后一筆成交的價格)、中間的最高價High和最低價Low。
如果Close<Open,表示為“BW-Solid”(即“實心藍白蠟燭”);如果Close>Open,表示為“R-Hollow”(即“空心紅蠟燭”);如果Open等於Close,則為“R-Cross”(即“十字紅蠟燭”)。如果Low比Open和Close低,稱為“Lower Shadow”(即“有下影線”),如果High比Open和Close高,稱為“Upper Shadow”(即“有上影線”)。請編程序,根據給定的四個價格組合,判斷當日的蠟燭是一根什么樣的蠟燭。
輸入格式:
輸入在一行中給出4個正實數,分別對應Open、High、Low、Close,其間以空格分隔。
輸出格式:
在一行中輸出日K蠟燭的類型。如果有上、下影線,則在類型后加上with 影線類型。如果兩種影線都有,則輸出with Lower Shadow and Upper Shadow。
輸入樣例1:
5.110 5.250 5.100 5.105
輸出樣例1:
BW-Solid with Lower Shadow and Upper Shadow
輸入樣例2:
5.110 5.110 5.110 5.110
輸出樣例2:
R-Cross
輸入樣例3:
5.110 5.125 5.112 5.126
輸出樣例3:
R-Hollow
1 #include <stdio.h> 2 int main() 3 { 4 double open,high,low,close; 5 scanf("%lf%lf%lf%lf",&open,&high,&low,&close); 6 if(close<open) printf("BW-Solid"); 7 else if(close>open) printf("R-Hollow"); 8 else printf("R-Cross"); 9 10 if(low<open&&low<close&&high>open&&high>close) 11 printf(" with Lower Shadow and Upper Shadow"); 12 else if(low<open&&low<close) 13 printf(" with Lower Shadow"); 14 else if(high>open&&high>close) 15 printf(" with Upper Shadow"); 16 return 0; 17 }
7-14 求整數段和
給定兩個整數A和B,輸出從A到B的所有整數以及這些數的和。
輸入格式:
輸入在一行中給出2個整數A和B,其中−,其間以空格分隔。
輸出格式:
首先順序輸出從A到B的所有整數,每5個數字占一行,每個數字占5個字符寬度,向右對齊。最后在一行中按Sum = X的格式輸出全部數字的和X。
輸入樣例:
-3 8
輸出樣例:
-3 -2 -1 0 1
2 3 4 5 6
7 8
Sum = 30
1 #include <stdio.h> 2 int main() 3 { 4 int a,b,sum=0,count=0; 5 scanf("%d%d",&a,&b); 6 for(int i=a; i<=b; ++i){ 7 sum += i; 8 printf("%5d",i); 9 count++; 10 if(count%5==0) 11 printf("\n"); 12 } 13 if(count!=5) 14 printf("\n"); 15 printf("Sum = %d",sum); 16 return 0; 17 }
根據下面關系式,求圓周率的值,直到最后一項的值小於給定閾值。
π /2 = 1+ 1/3 + 2! / (3×5) + 3! / (3×5×7) + ⋯ + n! / (3×5×7×⋯×(2n+1)) + ⋯
輸入格式:
輸入在一行中給出小於1的閾值。
輸出格式:
在一行中輸出滿足閾值條件的近似圓周率,輸出到小數點后6位。
輸入樣例:
0.01
輸出樣例:
3.132157
1 #include <stdio.h> 2 int main() 3 { 4 long long i=1,j=3; 5 double x, num = 1, den = 3, value = num/den, sum = 1+1.0/3; 6 scanf("%lf",&x); 7 while(value>x){ 8 num *= ++i; 9 den *= (j+=2); 10 value = (double)num/den; 11 sum += value; 12 } 13 printf("%.6f",sum*2); 14 return 0; 15 }
給定不超過6的正整數A,考慮從A開始的連續4個數字。請輸出所有由它們組成的無重復數字的3位數。
輸入格式:
輸入在一行中給出A。
輸出格式:
輸出滿足條件的的3位數,要求從小到大,每行6個整數。整數間以空格分隔,但行末不能有多余空格。
輸入樣例:
2
輸出樣例:
234 235 243 245 253 254
324 325 342 345 352 354
423 425 432 435 452 453
523 524 532 534 542 543
1 #include <stdio.h> 2 int main() 3 { 4 int a=2,count=0; 5 scanf("%d",&a); 6 for(int i=a; i<=a+3; i++) 7 { 8 for(int j=a; j<=a+3; j++){ 9 for(int k=a; k<=a+3; k++) 10 if(i!=j&&i!=k&&j!=k){ 11 if(count%6)printf(" "); 12 printf("%d%d%d",i,j,k); 13 count++; 14 if(count%6==0) 15 printf("\n"); 16 } 17 } 18 } 19 return 0; 20 }
一條蠕蟲長1寸,在一口深為N寸的井的底部。已知蠕蟲每1分鍾可以向上爬U寸,但必須休息1分鍾才能接着往上爬。在休息的過程中,蠕蟲又下滑了D寸。就這樣,上爬和下滑重復進行。請問,蠕蟲需要多長時間才能爬出井?
這里要求不足1分鍾按1分鍾計,並且假定只要在某次上爬過程中蠕蟲的頭部到達了井的頂部,那么蠕蟲就完成任務了。初始時,蠕蟲是趴在井底的(即高度為0)。
輸入格式:
輸入在一行中順序給出3個正整數N、U、D,其中D<U,N不超過100。
輸出格式:
在一行中輸出蠕蟲爬出井的時間,以分鍾為單位。
輸入樣例:
12 3 1
輸出樣例:
11
1 /* 2 12 3 1 3 */ 4 #include <stdio.h> 5 int main() 6 { 7 int n,u,d,t=0; 8 scanf("%d%d%d",&n,&u,&d); 9 while(n) 10 { 11 n -= u; 12 t++; 13 if(n<=0) break; 14 n += d; 15 t++; 16 } 17 printf("%d",t); 18 return 0; 19 }
二分法求函數根的原理為:如果連續函數(在區間[的兩個端點取值異號,即(,則它在這個區間內至少存在1個根r,即(。
二分法的步驟為:
- 檢查區間長度,如果小於給定閾值,則停止,輸出區間中點(;否則
- 如果(,則計算中點的值(;
- 如果(正好為0,則(就是要求的根;否則
- 如果(與(同號,則說明根在區間[,令),重復循環;
- 如果(與(同號,則說明根在區間[,令),重復循環。
本題目要求編寫程序,計算給定3階多項式(在給定區間[內的根。
輸入格式:
輸入在第1行中順序給出多項式的4個系數a3、a2、a1、a0,在第2行中順序給出區間端點a和b。題目保證多項式在給定區間內存在唯一單根。
輸出格式:
在一行中輸出該多項式在該區間內的根,精確到小數點后2位。
輸入樣例:
3 -1 -3 1
-0.5 0.5
輸出樣例:
0.33
1 #include<stdio.h> 2 #include<math.h> 3 #define EPS 1e-6 4 float Root(float x,float a3,float a2,float a1,float a0) 5 { 6 return a3*x*x*x + a2*x*x + a1*x + a0; 7 } 8 int main() 9 { 10 float a0,a1,a2,a3; 11 float min,max,mid; 12 scanf("%f%f%f%f",&a3,&a2,&a1,&a0); 13 scanf("%f%f",&min,&max); 14 do{ 15 mid=(max+min)/2.0; 16 if(Root(mid,a3,a2,a1,a0)*Root(min,a3,a2,a1,a0)>0) min = mid; 17 if(Root(mid,a3,a2,a1,a0)*Root(max,a3,a2,a1,a0)>0) max = mid; 18 }while(fabs( Root(mid,a3,a2,a1,a0)) > EPS); 19 printf("%.2f\n",mid); 20 return 0; 21 }
一個采購員去銀行兌換一張y元f分的支票,結果出納員錯給了f元y分。采購員用去了n分之后才發覺有錯,於是清點了余額尚有2元2分,問該支票面額是多少?
輸入格式:
輸入在一行中給出小於100的正整數n。
輸出格式:
在一行中按格式y.f輸出該支票的原始面額。如果無解,則輸出No Solution。
輸入樣例1:
23
輸出樣例1:
25.51
輸入樣例2:
22
輸出樣例2:
No Solution
1 #include <stdio.h> 2 int main() 3 { 4 int n=23,flag=0; 5 scanf("%d",&n); 6 7 for(int y=0; y<=100; ++y){ 8 for(int f=0; f<=100; ++f){ 9 if(f*100+y - n == 2*y*100+2*f){ 10 flag=1; 11 printf("%d.%d\n",y,f); 12 } 13 } 14 } 15 16 if(!flag) 17 printf("No Solution"); 18 return 0; 19 }
下面是一個完整的下三角九九口訣表:
1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 1*4=4 2*4=8 3*4=12 4*4=16 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
本題要求對任意給定的一位正整數N,輸出從1*1到N*N的部分口訣表。
輸入格式:
輸入在一行中給出一個正整數N(1≤N≤9)。
輸出格式:
輸出下三角N*N部分口訣表,其中等號右邊數字占4位、左對齊。
輸入樣例:
4
輸出樣例:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1 #include <stdio.h> 2 int main() 3 { 4 int n; 5 scanf("%d",&n); 6 7 for(int i=1; i<=n; ++i){ 8 for(int j=1; j<=i; ++j){ 9 printf("%d*%d=%-4d",j,i,i*j); 10 } 11 printf("\n"); 12 } 13 return 0; 14 }
本題要求對任意給定的正整數N,求方程X^2+Y^2=N的全部正整數解。
輸入格式:
輸入在一行中給出正整數N(≤10000)。
輸出格式:
輸出方程X^2+Y^2=N的全部正整數解,其中X≤Y。每組解占1行,兩數字間以1空格分隔,按X的遞增順序輸出。如果沒有解,則輸出No Solution。
輸入樣例1:
884
輸出樣例1:
10 28
20 22
輸入樣例2:
11
輸出樣例2:
No Solution
1 #include<stdio.h> 2 int main() 3 { 4 int N = 884, flag = 0; 5 scanf("%d",&N); 6 for(int x=1; x<=N-x; ++x){ 7 for(int y=1; y<=N-x*x; ++y){ 8 if(x<y && x*x+y*y==N){ 9 printf("%d %d\n",x,y); 10 flag = 1; 11 } 12 } 13 } 14 if(!flag) 15 printf("No Solution"); 16 return 0; 17 }
烏龜與兔子進行賽跑,跑場是一個矩型跑道,跑道邊可以隨地進行休息。烏龜每分鍾可以前進3米,兔子每分鍾前進9米;兔子嫌烏龜跑得慢,覺得肯定能跑贏烏龜,於是,每跑10分鍾回頭看一下烏龜,若發現自己超過烏龜,就在路邊休息,每次休息30分鍾,否則繼續跑10分鍾;而烏龜非常努力,一直跑,不休息。假定烏龜與兔子在同一起點同一時刻開始起跑,請問T分鍾后烏龜和兔子誰跑得快?
輸入格式:
輸入在一行中給出比賽時間T(分鍾)。
輸出格式:
在一行中輸出比賽的結果:烏龜贏輸出@_@,兔子贏輸出^_^,平局則輸出-_-;后跟1空格,再輸出勝利者跑完的距離。
輸入樣例:
242
輸出樣例:
@_@ 726
1 #include<stdio.h> 2 int main() 3 { 4 int time; 5 scanf("%d", &time); 6 7 int i, rest = 0, run = 0, disrab = 0, distur = 0; 8 for(i = 0; i<time; i++) 9 { 10 if(run == 0){ 11 run = 10;/*兔子跑十分鍾*/ 12 if(disrab>distur){ 13 rest = 30;/*超過了烏龜休息半小時*/ 14 } 15 } 16 17 distur += 3;/*烏龜一分鍾*/ 18 if(rest == 0 ){ 19 disrab += 9;/*兔子一分鍾(十分鍾之內)*/ 20 run--; 21 } 22 23 if(rest>0){/*兔子休息(半小時之內)*/ 24 rest--; 25 } 26 } 27 28 if(disrab<distur){ 29 printf("@_@ %d",distur); 30 } 31 else if(disrab > distur){ 32 printf("^_^ %d",disrab); 33 } 34 else{ 35 printf("-_- %d",disrab); 36 } 37 38 return 0; 39 }
輸入一個整數(位數不超過9位)代表一個人民幣值(單位為元),請轉換成財務要求的大寫中文格式。如23108元,轉換后變成“貳萬叄仟壹百零捌”元。為了簡化輸出,用小寫英文字母a-j順序代表大寫數字0-9,用S、B、Q、W、Y分別代表拾、百、仟、萬、億。於是23108元應被轉換輸出為“cWdQbBai”元。
輸入格式:
輸入在一行中給出一個不超過9位的非負整數。
輸出格式:
在一行中輸出轉換后的結果。注意“零”的用法必須符合中文習慣。
輸入樣例1:
813227345
輸出樣例1:
iYbQdBcScWhQdBeSf
輸入樣例2:
6900
輸出樣例2:
gQjB
1 //813227345//6900//23108//100001//0 2 #include <stdio.h> 3 #include <string.h> 4 int main() 5 { 6 const char *num1 = "abcdefghij"; 7 const char *num2 = "GSBQWSBQY"; 8 char x[10] = ""; 9 scanf("%s", x); 10 11 int start = 0;//字符串下標start 12 int end = strlen(x)-1;//字符串下標end 13 while(x[start])//當前字符 14 { 15 if(x[start]!='0'){//當前字符不為'0' 16 printf("%c",num1[x[start]-'0']); 17 if(end-start>=1) 18 printf("%c",num2[end-start]); 19 } 20 else{//當前字符為'0', 要么輸出'a', 要么輸出'W' 21 if(end-start==4) printf("%c",num2[end-start]); //且萬位4,輸出'W' 22 //且不到字符串尾部且下一個字符不是'0', 或字符串只一個'0' 23 else if( end > start && x[start+1]!='0' || !end )//輸出'a' 24 printf("%c",num1[x[start]-'0']); 25 } 26 start++; 27 } 28 return 0; 29 }
分數可以表示為分子/分母的形式。編寫一個程序,要求用戶輸入一個分數,然后將其約分為最簡分式。最簡分式是指分子和分母不具有可以約分的成分了。如6/12可以被約分為1/2。當分子大於分母時,不需要表達為整數又分數的形式,即11/8還是11/8;而當分子分母相等時,仍然表達為1/1的分數形式。
輸入格式:
輸入在一行中給出一個分數,分子和分母中間以斜杠/分隔,如:12/34表示34分之12。分子和分母都是正整數(不包含0,如果不清楚正整數的定義的話)。
提示:在scanf的格式字符串中加入/,讓scanf來處理這個斜杠。
輸出格式:
在一行中輸出這個分數對應的最簡分式,格式與輸入的相同,即采用分子/分母的形式表示分數。如 5/6表示6分之5。
輸入樣例:
66/120
輸出樣例:
11/20
1 #include <stdio.h> 2 int main() 3 { 4 int a,b; 5 scanf("%d/%d",&a,&b); 6 int t = a<b?a:b; 7 while(t) 8 { 9 if(a%t==0&&b%t==0) 10 break; 11 t--; 12 } 13 printf("%d/%d",a/t,b/t); 14 return 0; 15 }
輸入一個整數,輸出每個數字對應的拼音。當整數為負數時,先輸出fu字。十個數字對應的拼音如下:
0: ling 1: yi 2: er 3: san 4: si 5: wu 6: liu 7: qi 8: ba 9: jiu
輸入格式:
輸入在一行中給出一個整數,如:1234。
提示:整數包括負數、零和正數。
輸出格式:
在一行中輸出這個整數對應的拼音,每個數字的拼音之間用空格分開,行末沒有最后的空格。如 yi er san si。
輸入樣例:
-600
輸出樣例:
fu liu ling ling
1 #include <stdio.h> 2 int main() 3 { 4 const char* num[10] = 5 {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"}; 6 char s[256] = ""; 7 scanf("%s",s); 8 9 int index=0, flag = 0; 10 while(s[index]) 11 { 12 if(flag) 13 printf(" "); 14 if(s[index]=='-') 15 printf("%s","fu"); 16 else 17 printf("%s",num[s[index]-'0']); 18 index++; 19 flag = 1; 20 } 21 return 0; 22 }
7-26 單詞長度
你的程序要讀入一行文本,其中以空格分隔為若干個單詞,以.結束。你要輸出每個單詞的長度。這里的單詞與語言無關,可以包括各種符號,比如it's算一個單詞,長度為4。注意,行中可能出現連續的空格;最后的.不計算在內。
輸入格式:
輸入在一行中給出一行文本,以.結束
提示:用scanf("%c",...);來讀入一個字符,直到讀到.為止。
輸出格式:
在一行中輸出這行文本對應的單詞的長度,每個長度之間以空格隔開,行末沒有最后的空格。
輸入樣例:
It's great to see you here.
輸出樣例:
4 5 2 3 3 4
1 #include <stdio.h> 2 int main() 3 { 4 char s[256] = ""; 5 gets(s); 6 int i=0, count=0,flag=0; 7 while(s[i]) 8 { 9 if(s[i++]!=' ' && s[i-1]!='.') 10 count++; 11 else{ 12 if(!count) 13 continue; 14 if(flag) 15 printf(" "); 16 printf("%d",count); 17 count = 0; 18 flag = 1; 19 } 20 } 21 return 0; 22 }
7-27 冒泡法排序
將N個整數按從小到大排序的冒泡排序法是這樣工作的:從頭到尾比較相鄰兩個元素,如果前面的元素大於其緊隨的后面元素,則交換它們。通過一遍掃描,則最后一個元素必定是最大的元素。然后用同樣的方法對前N−1個元素進行第二遍掃描。依此類推,最后只需處理兩個元素,就完成了對N個數的排序。
本題要求對任意給定的K(<N),輸出掃描完第K遍后的中間結果數列。
輸入格式:
輸入在第1行中給出N和K(1≤K<N≤100),在第2行中給出N個待排序的整數,數字間以空格分隔。
輸出格式:
在一行中輸出冒泡排序法掃描完第K遍后的中間結果數列,數字間以空格分隔,但末尾不得有多余空格。
輸入樣例:
6 2
2 3 5 1 6 4
輸出樣例:
2 1 3 4 5 6
1 #include <stdio.h> 2 int main() 3 { 4 int n,k; 5 int arr[256] = {0}; 6 scanf("%d%d",&n,&k); 7 for(int i=0; i<n; ++i){ 8 scanf("%d",&arr[i]); 9 } 10 for(int i=0; i<k; ++i){ 11 for(int j=1; j<n-i; ++j){ 12 if(arr[j-1]>arr[j]){ 13 int t = arr[j-1]; 14 arr[j-1] = arr[j]; 15 arr[j] = t; 16 } 17 } 18 } 19 for(int i=0; i<n; ++i) 20 { 21 if(i) printf(" "); 22 printf("%d",arr[i]); 23 } 24 return 0; 25 }
7-28 猴子選大王
一群猴子要選新猴王。新猴王的選擇方法是:讓N只候選猴子圍成一圈,從某位置起順序編號為1~N號。從第1號開始報數,每輪從1報到3,凡報到3的猴子即退出圈子,接着又從緊鄰的下一只猴子開始同樣的報數。如此不斷循環,最后剩下的一只猴子就選為猴王。請問是原來第幾號猴子當選猴王?
輸入格式:
輸入在一行中給一個正整數N(≤1000)。
輸出格式:
在一行中輸出當選猴王的編號。
輸入樣例:
11
輸出樣例:
7
1 #include <stdio.h> 2 int main() 3 { 4 int arr[1000] = {0}; 5 int n; 6 scanf("%d",&n); 7 int t = n, i=0; 8 int count = 0; 9 while(t>1) 10 { 11 if(i==n) 12 i = 0; 13 if(!arr[i]) 14 count++; 15 if(count==3){ 16 count=0; 17 arr[i] = 1; 18 t--; 19 } 20 i++; 21 } 22 for(int i=0; i<n; ++i) 23 if(!arr[i]) printf("%d",i+1); 24 return 0; 25 }
7-29 刪除字符串中的子串
輸入2個字符串S1和S2,要求刪除字符串S1中出現的所有子串S2,即結果字符串中不能包含S2。
輸入格式:
輸入在2行中分別給出不超過80個字符長度的、以回車結束的2個非空字符串,對應S1和S2。
輸出格式:
在一行中輸出刪除字符串S1中出現的所有子串S2后的結果字符串。
輸入樣例:
Tomcat is a male ccatat
cat
輸出樣例:
Tom is a male
1 /* 2 char * strcat ( char * destination, const char * source ); 3 destination and source shall not overlap. 4 */ 5 #include <stdio.h> 6 #include <string.h> 7 int main() 8 { 9 char s1[100] = ""; 10 char s2[100] = ""; 11 char t[100] = ""; 12 gets(s1); 13 gets(s2); 14 15 char* p = NULL; 16 while(p = strstr(s1,s2)) 17 { 18 *p = '\0'; 19 strcpy(t,p+strlen(s2)); 20 strcat(s1,t); 21 } 22 printf("%s",s1); 23 return 0; 24 }
7-30 字符串的冒泡排序
我們已經知道了將N個整數按從小到大排序的冒泡排序法。本題要求將此方法用於字符串序列,並對任意給定的K(<N),輸出掃描完第K遍后的中間結果序列。
輸入格式:
輸入在第1行中給出N和K(1≤K<N≤100),此后N行,每行包含一個長度不超過10的、僅由小寫英文字母組成的非空字符串。
輸出格式:
輸出冒泡排序法掃描完第K遍后的中間結果序列,每行包含一個字符串。
輸入樣例:
6 2
best
cat
east
a
free
day
輸出樣例:
best
a
cat
day
east
free
1 #include <stdio.h> 2 #include <string.h> 3 #define N 256 4 int main() 5 { 6 int n,k; 7 char str[N][N] = {0}; 8 scanf("%d%d",&n,&k); 9 for(int i=0; i<n; ++i){ 10 scanf("%s",&str[i]); 11 } 12 for(int i=0; i<k; ++i){ 13 for(int j=1; j<n-i; ++j){ 14 if(strcmp(str[j-1],str[j])>0){ 15 char t[N] = ""; 16 strcpy(t,str[j-1]); 17 strcpy(str[j-1],str[j]); 18 strcpy(str[j],t); 19 } 20 } 21 } 22 for(int i=0; i<n; ++i) 23 printf("%s\n",str[i]); 24 25 return 0; 26 }
輸入一個字符串和一個非負整數N,要求將字符串循環左移N次。
輸入格式:
輸入在第1行中給出一個不超過100個字符長度的、以回車結束的非空字符串;第2行給出非負整數N。
輸出格式:
在一行中輸出循環左移N次后的字符串。
輸入樣例:
Hello World!
2
輸出樣例:
llo World!He
1 #include <stdio.h> 2 #include <string.h> 3 #define N 101 4 int main () 5 { 6 char str[N] = ""; 7 int n; 8 gets(str); 9 scanf("%d",&n); 10 11 int index = strlen(str)-1; 12 while(n--) 13 { 14 char ch = str[0]; 15 for(int i=1; i<=index; ++i) 16 str[i-1] = str[i]; 17 str[index] = ch; 18 } 19 puts(str); 20 return 0; 21 }
給定一句英語,要求你編寫程序,將句中所有單詞的順序顛倒輸出。
輸入格式:
測試輸入包含一個測試用例,在一行內給出總長度不超過500 000的字符串。字符串由若干單詞和若干空格組成,其中單詞是由英文字母(大小寫有區分)組成的字符串,單詞之間用若干個空格分開。
輸出格式:
每個測試用例的輸出占一行,輸出倒序后的句子,並且保證單詞間只有1個空格。
輸入樣例:
Hello World Here I Come
輸出樣例:
Come I Here World Hello
1 #include <stdio.h> 2 #include <string.h> 3 #define N 500001 4 int main () 5 { 6 char str[N] = ""; 7 gets(str); 8 int count=0,flag = 0; 9 char* p = str+strlen(str) - 1; 10 11 while(p>=str)/* Hello World Here I Come */ 12 { 13 if(*p>='a'&&*p<='z'||*p>='A'&&*p<='Z') 14 { 15 count++; 16 if(!(*(p-1)>='a'&&*(p-1)<='z'||*(p-1)>='A'&&*(p-1)<='Z')){ 17 if(flag) printf(" "); 18 for(int i=0; i<count; ++i) 19 printf("%c",*(p+i)); 20 count = 0; 21 flag = 1; 22 } 23 } 24 p--; 25 } 26 return 0; 27 }
本題要求編寫程序,計算兩個有理數的和。
輸入格式:
輸入在一行中按照a1/b1 a2/b2的格式給出兩個分數形式的有理數,其中分子和分母全是整形范圍內的正整數。
輸出格式:
在一行中按照a/b的格式輸出兩個有理數的和。注意必須是該有理數的最簡分數形式,若分母為1,則只輸出分子。
輸入樣例1:
1/3 1/6
輸出樣例1:
1/2
輸入樣例2:
4/3 2/3
輸出樣例2:
2
1 #include <stdio.h> 2 int main () 3 { 4 int a1,b1,a2,b2; 5 scanf("%d/%d%d/%d",&a1,&b1,&a2,&b2); 6 7 int a = a1*b2+a2*b1; 8 int b = b1*b2; 9 int t = a<b?a:b; 10 while(t) 11 { 12 if(a%t==0&&b%t==0) 13 break; 14 t--; 15 } 16 if(b/t==1) 17 printf("%d",a/t); 18 else 19 printf("%d/%d",a/t,b/t); 20 return 0; 21 }
通訊錄中的一條記錄包含下述基本信息:朋友的姓名、出生日期、性別、固定電話號碼、移動電話號碼。 本題要求編寫程序,錄入N條記錄,並且根據要求顯示任意某條記錄。
輸入格式:
輸入在第一行給出正整數N(≤10);隨后N行,每行按照格式姓名 生日 性別 固話 手機給出一條記錄。其中姓名是不超過10個字符、不包含空格的非空字符串;生日按yyyy/mm/dd的格式給出年月日;性別用M表示“男”、F表示“女”;固話和手機均為不超過15位的連續數字,前面有可能出現+。
在通訊錄記錄輸入完成后,最后一行給出正整數K,並且隨后給出K個整數,表示要查詢的記錄編號(從0到N−1順序編號)。數字間以空格分隔。
輸出格式:
對每一條要查詢的記錄編號,在一行中按照姓名 固話 手機 性別 生日的格式輸出該記錄。若要查詢的記錄不存在,則輸出Not Found。
輸入樣例:
3
Chris 1984/03/10 F +86181779452 13707010007
LaoLao 1967/11/30 F 057187951100 +8618618623333
QiaoLin 1980/01/01 M 84172333 10086
2 1 7
輸出樣例:
LaoLao 057187951100 +8618618623333 F 1967/11/30
Not Found
1 #include <stdio.h> 2 struct address{ 3 char name[20]; 4 char birthday[20]; 5 char sex[20]; 6 char tel[20]; 7 char mobile[20]; 8 }; 9 int main () 10 { 11 int n; 12 scanf("%d",&n); 13 struct address records[20]; 14 for(int i=0; i<n; ++i) 15 { 16 scanf("%s%s%s%s%s", 17 records[i].name, 18 records[i].birthday, 19 records[i].sex, 20 records[i].tel, 21 records[i].mobile); 22 } 23 /*輸出 */ 24 int k,record; 25 scanf("%d",&k); 26 for(int i=0; i<k; ++i) 27 { 28 scanf("%d",&record); 29 if(record>=0 && record<n) 30 printf("%s %s %s %s %s\n", 31 records[record].name, 32 records[record].tel, 33 records[record].mobile, 34 records[record].sex, 35 records[record].birthday); 36 else 37 printf("Not Found\n"); 38 } 39 return 0; 40 }
本題要求編寫程序,計算N個有理數的平均值。
輸入格式:
輸入第一行給出正整數N(≤100);第二行中按照a1/b1 a2/b2 …的格式給出N個分數形式的有理數,其中分子和分母全是整形范圍內的整數;如果是負數,則負號一定出現在最前面。
輸出格式:
在一行中按照a/b的格式輸出N個有理數的平均值。注意必須是該有理數的最簡分數形式,若分母為1,則只輸出分子。
輸入樣例1:
4
1/2 1/6 3/6 -5/10
輸出樣例1:
1/6
輸入樣例2:
2 4/3 2/3
輸出樣例2:
1
1 #include <stdio.h> 2 int reduce(int a,int b){ 3 int r=a%b; 4 while(r!=0){ 5 a=b; 6 b=r; 7 r=a%b; 8 } 9 return b; 10 } 11 int main () 12 { 13 int n, a1=0, b1=1, a2, b2, t; 14 scanf("%d",&n); 15 for(int i=0; i<n; ++i){ 16 scanf("%d/%d",&a2,&b2); 17 a1 = a1*b2+a2*b1; 18 b1 = b1*b2; 19 t = reduce(a1,b1); 20 a1 /= t; 21 b1 /= t; 22 } 23 t = reduce(a1,n*b1); 24 if(n*b1/t==1) 25 printf("%d",a1/t); 26 else 27 printf("%d/%d",a1/t,n*b1/t); 28 return 0; 29 }
本題要求編寫程序,計算2個復數的和、差、積、商。
輸入格式:
輸入在一行中按照a1 b1 a2 b2的格式給出2個復數C1=a1+b1i和C2=a2+b2i的實部和虛部。題目保證C2不為0。
輸出格式:
分別在4行中按照(a1+b1i) 運算符 (a2+b2i) = 結果的格式順序輸出2個復數的和、差、積、商,數字精確到小數點后1位。如果結果的實部或者虛部為0,則不輸出。如果結果為0,則輸出0.0。
輸入樣例1:
2 3.08 -2.04 5.06
輸出樣例1:
(2.0+3.1i) + (-2.0+5.1i) = 8.1i
(2.0+3.1i) - (-2.0+5.1i) = 4.0-2.0i
(2.0+3.1i) * (-2.0+5.1i) = -19.7+3.8i
(2.0+3.1i) / (-2.0+5.1i) = 0.4-0.6i
輸入樣例2:
1 1 -1 -1.01
輸出樣例2:
(1.0+1.0i) + (-1.0-1.0i) = 0.0 (1.0+1.0i) - (-1.0-1.0i) = 2.0+2.0i (1.0+1.0i) * (-1.0-1.0i) = -2.0i (1.0+1.0i) / (-1.0-1.0i) = -1.0
1 #include <stdio.h> 2 #include <math.h> 3 #define eps 1e-1 4 double a1,b1,a2,b2,a,b; 5 void print(char op) 6 { 7 printf("(%.1f%+.1fi) %c (%.1f%+.1fi) = ",a1,b1,op,a2,b2); 8 if(fabs(a)<eps&&fabs(b)<eps) //是小於不是小於等於 9 printf("0.0"); 10 else{ 11 if(fabs(a)>eps) printf("%.1f",a);//a不為0 12 if(b>0&&fabs(a)>eps&&fabs(b)>eps) printf("+");//a,b均不為0且b>0 13 if(fabs(b)>eps) printf("%.1fi",b);//b不為0 14 } 15 printf("\n"); 16 } 17 int main() 18 { 19 scanf("%lf%lf%lf%lf",&a1,&b1,&a2,&b2); 20 /* 1.+ */ 21 a = a1+a2; 22 b = b1+b2; 23 print('+'); 24 /* 2.- */ 25 a = a1-a2; 26 b = b1-b2; 27 print('-'); 28 /* 3.* */ 29 a = a1*a2-b1*b2; 30 b = a1*b2+b1*a2; 31 print('*'); 32 /* 4./ */ 33 a = (a1*a2+b1*b2)/(a2*a2+b2*b2); 34 b = (b1*a2-a1*b2)/(a2*a2+b2*b2); 35 print('/'); 36 return 0; 37 }
將一個正整數N分解成幾個正整數相加,可以有多種分解方法,例如7=6+1,7=5+2,7=5+1+1,…。編程求出正整數N的所有整數分解式子。
輸入格式:
每個輸入包含一個測試用例,即正整數N (0<N≤30)。
輸出格式:
按遞增順序輸出N的所有整數分解式子。遞增順序是指:對於兩個分解序列N1={,}和N2={,},若存在i使得,,但是ni+1<mi+1,則N1序列必定在N2序列之前輸出。每個式子由小到大相加,式子間用分號隔開,且每輸出4個式子后換行。
輸入樣例:
7
輸出樣例:
7=1+1+1+1+1+1+1;7=1+1+1+1+1+2;7=1+1+1+1+3;7=1+1+1+2+2
7=1+1+1+4;7=1+1+2+3;7=1+1+5;7=1+2+2+2
7=1+2+4;7=1+3+3;7=1+6;7=2+2+3
7=2+5;7=3+4;7=7
1 #include<stdio.h> 2 int N = 7; 3 int s[31]; // 數組棧 4 int top = -1; // 數組指針 5 int count = 0; // 統計輸出的次數 6 int sum = 0; // 拆分項累加和 7 8 void division (int i); 9 void print(); 10 int main () 11 { 12 scanf ("%d", &N); 13 division (1); 14 return 0; 15 } 16 void print() 17 { 18 count ++; //輸出計數 19 printf("%d=", N); 20 for (int k=0; k<top; k++) 21 printf("%d+", s[k]);//輸出 22 if (count%4 == 0 || s[top] == N) 23 printf("%d\n", s[top]);//輸出並換行 24 else 25 printf("%d;", s[top]);//輸出不換行 26 } 27 void division (int i) //拆分 28 { //j<=N-sum控制sum小於等於N 29 for (int j=i; j<=N-sum; j++) 30 { 31 s[++top] = j;//壓棧 32 sum += j; //累加 33 division (j);//sum不等於N,遞歸調用,繼續壓棧累加 34 if(sum == N)//sum==N打印 35 print(); 36 sum -= j;//退出上一個top的元素 37 top --; //彈出上一個top的元素 38 } 39 }
給定某數字A(1)以及非負整數N(0),求數列之和S=A+AA+AAA+⋯+AA⋯A(N個A)。例如A=1, N=3時,1。
輸入格式:
輸入數字A與非負整數N。
輸出格式:
輸出其N項數列之和S的值。
輸入樣例:
1 3
輸出樣例:
123
1 #include <stdio.h> 2 int main() 3 { 4 int s[200000] = {0}; 5 int a, n, t, c = 0; 6 scanf("%d%d",&a,&n); 7 /* 最小n */ 8 if(!n) 9 { 10 printf("0\n"); 11 return 0; 12 } 13 /* 數列之和s的個位是a的n倍,依次遞減 */ 14 t = n; //n進位遞增,t遞減 15 for(int i=0; i<n; i++) 16 { 17 s[i] = a*t + c;//c是進位 18 if(s[i]>9) 19 { 20 c = s[i]/10; 21 s[i] %= 10; 22 if(i==n-1) n++;//進位遞增 23 } 24 t--; 25 } 26 /* 從最高位讀起 */ 27 for(int i=n-1; i>=0; i--) 28 printf("%d",s[i]); 29 printf("\n"); 30 return 0; 31 }
