哈工大C语言公开课练兵编程(二)
PS:由于平常沒時(shí)間做,一般三周更新一次,希望大家原諒。
第四周
1 檢測(cè)用戶錯(cuò)誤輸入(4分)
題目?jī)?nèi)容:根據(jù)scanf()的返回值判斷scanf()是否成功讀入了指定的數(shù)據(jù)項(xiàng)數(shù),使程序在用戶輸入123a時(shí),能輸出如下運(yùn)行結(jié)果:
123a↙
Input error!
輸入格式:
“%d %d”
輸出格式:
如果成功讀入指定的數(shù)據(jù)項(xiàng)數(shù),輸出格式為:”a = %d, b = %d\n” (注意:等號(hào)的兩邊各有一個(gè)空格)
輸入非法數(shù)據(jù),輸出格式為:”Input error!”
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
2 閏年判斷(6分)
題目?jī)?nèi)容:從鍵盤任意輸入一個(gè)公元年份(大于等于1),判斷它是否是閏年。若是閏年輸出“Yes”,否則輸出“No”。要求對(duì)輸入數(shù)據(jù)進(jìn)行合法性判斷。
提示:
已知符合下列條件之一者是閏年:
(1)能被4整除,但不能被100整除;
(2)能被400整除。
示例:
示例1:
2015↙
No
示例2:
2016↙
Yes
示例3:
-123↙
Input error!
例4:
a↙
Input error!
輸入格式:
“%d”
輸出格式:
是閏年,輸出:”Yes\n”
不是閏年,輸出:”No\n”
輸入數(shù)據(jù)不合法,輸出:”Input error!\n”
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
3 程序改錯(cuò)v1.0(7分)
題目?jī)?nèi)容:下面代碼的功能是將百分制成績(jī)轉(zhuǎn)換為5分制成績(jī),具體功能是:如果用戶輸入的是非法字符或者不在合理區(qū)間內(nèi)的數(shù)據(jù)(例如輸入的是a,或者102,或-45等),則程序輸出 Input error!,否則將其轉(zhuǎn)換為5分制輸出。目前程序存在錯(cuò)誤,請(qǐng)將其修改正確。并按照下面給出的運(yùn)行示例檢查程序。
//需要修改的程序#include<stdio.h>int main(){int score;char grade;printf("Please input score:");scanf("%d", &score);if (score < 0 || score > 100) printf("Input error!\n");else if (score >= 90) grade = 'A’;else if (score >= 80)grade = 'B'; else if (score >= 70)grade = 'C'; else if (score >= 60)grade = 'D'; elsegrade = 'E'; printf("grade:%c\n", grade);return 0;}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
示例:
示例1:
Please input score:
-1↙
Input error!
示例2:
Please input score:
95↙
grade: A
示例3:
Please input score:
82↙
grade: B
示例4:
Please input score:
72↙
grade: C
示例5:
Please input score:
66↙
grade: D
示例6:
Please input score:
32↙
grade: E
示例7:
Please input score:
127↙
Input error!
輸入輸出:
輸入提示信息:”Please input score:\n”
輸入格式: “%d”
輸出格式:
用戶輸入存在錯(cuò)誤時(shí),輸出提示信息:”Input error!\n”
輸出格式:”grade: %c\n” (注意:%c前面有一個(gè)空格)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
4 字符類型判斷(4分)
題目?jī)?nèi)容:從鍵盤鍵入任意一個(gè)字符,判斷該字符是英文字母(不區(qū)分大、小寫)、數(shù)字字符還是其它字符。
若鍵入字母,則屏幕顯示 It is an English character.;若鍵入數(shù)字則屏幕顯示It is a digit character. ;若輸入其它字符,則屏幕顯示:It is other character.
示例:
程序的運(yùn)行示例1:
Input simple:
b↙
It is an English character.
程序的運(yùn)行示例2:
Input simple:
6↙
It is a digit character.
程序的運(yùn)行示例3:
Input simple:
*↙
It is other character.
程序的運(yùn)行示例4:
Input simple:
A↙
It is an English character.
輸入輸出:
輸入信息提示:”Input simple:\n”
輸入格式: “%c”
輸出格式:
英文字符的輸出格式:”It is an English character.\n”
數(shù)字的輸出格式:”It is a digit character.\n”
其它字符的輸出格式:”It is other character.\n”
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
5 快遞費(fèi)用計(jì)算(7分)
題目?jī)?nèi)容:上海市的某快遞公司根據(jù)投送目的地距離公司的遠(yuǎn)近,將全國(guó)劃分成5個(gè)區(qū)域:
| 同城 | 臨近兩省 | 1500公里(含)以內(nèi) | 1500——2500公里 | 2500公里以上 |
| 上海 | 江蘇,浙江 | 北京,天津,河北,遼寧,河南,安微,陜西,湖北,江西,湖南,福建,廣東,山西。 | 吉林,遼寧,甘肅,四川,重慶,青海,廣西,云南,海南,內(nèi)蒙古,黑龍江,貴州。 | 新疆,西藏。 |
快遞費(fèi)按郵件重量計(jì)算,由起重費(fèi)用、續(xù)重費(fèi)用兩部分構(gòu)成:
(1) 起重(首重)1公斤按起重資費(fèi)計(jì)算(不足1公斤,按1公斤計(jì)算),超過首重的重量,按公斤(不足1公斤,按1公斤計(jì)算)收取續(xù)重費(fèi);
(2) 同城起重資費(fèi)10元,續(xù)重3元/公斤;
(3) 寄往1區(qū)(江浙兩省)的郵件,起重資費(fèi)10元,續(xù)重4元;
(4) 寄往其他地區(qū)的郵件,起重資費(fèi)統(tǒng)一為15元。而續(xù)重部分,不同區(qū)域價(jià)格不同:2區(qū)的續(xù)重5元/公斤,3區(qū)的續(xù)重6.5元/公斤,4區(qū)的續(xù)重10元/公斤。
編寫程序,從鍵盤輸入郵件的目的區(qū)域編碼和重量,計(jì)算并輸出運(yùn)費(fèi),計(jì)算結(jié)果保留2位小數(shù)。程序中所有浮點(diǎn)數(shù)的數(shù)據(jù)類型均為float。
提示:續(xù)重部分不足一公斤,按1公斤計(jì)算。因此,如包裹重量2.3公斤:1公斤算起重,剩余的1.3公斤算續(xù)重,不足1公斤按1公斤計(jì)算,1.3公斤折合續(xù)重為2公斤。如果重量應(yīng)大于0、區(qū)域編號(hào)不能超出0-4的范圍。
示例:
程序運(yùn)行結(jié)果示例1:
4,4.5↙
Price: 55.00
程序運(yùn)行結(jié)果示例2:
5,3.2↙
Error in Area
Price: 0.00
輸入輸出:
輸入格式:
用逗號(hào)分隔的兩個(gè)數(shù)字,第一個(gè)表示區(qū)域、第二個(gè)是重量:”%d,%f”
輸出格式:
價(jià)格的輸出格式:”Price: %5.2f\n”
區(qū)域錯(cuò)誤的提示信息:”Error in Area\n”
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
6 數(shù)位拆分v2.0(4分)
題目?jī)?nèi)容:從鍵盤上輸入一個(gè)4位數(shù)的整數(shù)n,編寫程序?qū)⑵洳鸱譃閮蓚€(gè)2位數(shù)的整數(shù)a和b,計(jì)算并輸出拆分后的兩個(gè)數(shù)的加、減、乘、除和求余運(yùn)算的結(jié)果。例如n=-4321,設(shè)拆分后的兩個(gè)整數(shù)為a,b,則a=-43,b=-21。除法運(yùn)算結(jié)果要求精確到小數(shù)點(diǎn)后2位,數(shù)據(jù)類型為float。求余和除法運(yùn)算需要考慮除數(shù)為0的情況,即如果拆分后b=0,則輸出提示信息”The second operater is zero!”
示例:
程序的運(yùn)行結(jié)果示例1:
Please input n:
1200↙
12,0
sum=12,sub=12,multi=0
The second operator is zero!
程序的運(yùn)行結(jié)果示例2:
Please input n:
-2304↙
-23,-4
sum=-27,sub=-19,multi=92
dev=5.75,mod=-3
輸入輸出:
輸入提示信息:”Please input n:\n”
輸入格式: “%d”
輸出格式:
拆分后的兩個(gè)整數(shù)的輸出格式:”%d,%d\n”
加法、減法、乘法的輸出格式:”sum=%d,sub=%d,multi=%d\n”
除法和求余的輸出格式:”dev=%.2f,mod=%d\n”
除數(shù)為0的提示信息:”The second operator is zero!\n”
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
7 出租車計(jì)價(jià)(4分)
題目?jī)?nèi)容:已知某城市普通出租車收費(fèi)標(biāo)準(zhǔn)為:起步里程為3公里,起步費(fèi)為8元,10公里以內(nèi)超過起步里程的部分,每公里加收2元,超過10公里以上的部分加收50%的回空補(bǔ)貼費(fèi),即每公里3元。出租車營(yíng)運(yùn)過程中,因堵車和乘客要求臨時(shí)停車等客的,按每5分鐘加收2元計(jì)算,不足5分鐘的不計(jì)費(fèi)。從鍵盤任意輸入行駛里程(精確到0.1公里)和等待時(shí)間(精確到分鐘),請(qǐng)編程計(jì)算并輸出乘客應(yīng)支付的車費(fèi),對(duì)結(jié)果進(jìn)行四舍五入,精確到元。
示例:
程序運(yùn)行結(jié)果示例1:
Input distance and time:2,2↙
fee = 8
程序運(yùn)行結(jié)果示例2:
Input distance and time:5,5↙
fee = 14
程序運(yùn)行結(jié)果示例3:
Input distance and time:12,15↙
fee = 34
程序運(yùn)行結(jié)果示例4:
Input distance and time:20,0↙
fee = 52
輸入輸出:
輸入提示信息:”Input distance and time:”
輸入格式:
用逗號(hào)分隔的兩個(gè)數(shù)字,第一個(gè)表示距離、第二個(gè)表示時(shí)間:”%f,%d”
輸出格式:”fee = %.0f\n” (注意:等號(hào)的兩邊各有一個(gè)空格)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
8 數(shù)據(jù)區(qū)間判斷(6分)
題目?jī)?nèi)容:從鍵盤輸入一個(gè)int型的正整數(shù)n(已知:0
#include<stdio.h> #include<stdlib.h> #include<string.h> int main(){int x;char str[10];printf("Please enter the number:\n");scanf("%d",&x);sprintf(str,"%d",x);if(x<=0||strlen(str)>4) printf("error!\n");else if(strlen(str)==1) printf("%d: 0-9\n",x);else if(strlen(str)==2) printf("%d: 10-99\n",x);else if(strlen(str)==3) printf("%d: 100-999\n",x);else if(strlen(str)==4) printf("%d: 1000-9999\n",x);return 0; }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
9 計(jì)算一元二次方程的根v2.0(4分)
題目?jī)?nèi)容:根據(jù)下面給出的求根公式,計(jì)算并輸出一元二次方程ax2+bx+c=0ax2+bx+c=0的兩個(gè)實(shí)根,要求精確到小數(shù)點(diǎn)后4位。其中a,b,c的值由用戶從鍵盤輸入。如果用戶輸入的系數(shù)不滿足求實(shí)根的要求,輸出錯(cuò)誤提示 “error!”。程序中所有的數(shù)據(jù)類型均為float。
x1,2=?b±b2?4ac???????√2ax1,2=?b±b2?4ac2a
示例:
程序運(yùn)行結(jié)果示例1:
Please enter the coefficients a,b,c:
1,2,1↙
x1=-1.0000, x2=-1.0000
程序運(yùn)行結(jié)果示例2:
Please enter the coefficients a,b,c:
2,6,1↙
x1=-0.1771, x2=-2.8229
程序運(yùn)行結(jié)果示例3:
Please enter the coefficients a,b,c:
2,1,6↙
error!
輸入輸出:
輸入提示信息:”Please enter the coefficients a,b,c:\n”
輸入格式: “%f,%f,%f”
輸出格式: “x1=%7.4f, x2=%7.4f\n”
如果輸入的系數(shù)不滿足求實(shí)根的要求,輸出錯(cuò)誤提示信息:”error!\n”
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
第五周
1 判斷一個(gè)整型數(shù)據(jù)有幾位v2.0(4分)
題目?jī)?nèi)容:從鍵盤輸入一個(gè)整型數(shù)據(jù)(int型),編寫程序判斷該整數(shù)共有幾位,并輸出包含各個(gè)數(shù)字的個(gè)數(shù)。例如,從鍵盤輸入整數(shù)16644,該整數(shù)共有5位,其中有1個(gè)1,2個(gè)6,2個(gè)4。
示例:
程序運(yùn)行結(jié)果示例1:
Please enter the number:
12226↙
12226: 5 bits
1: 1
2: 3
6: 1
程序運(yùn)行結(jié)果示例2:
Please enter the number:
-12243↙
-12243: 5 bits
1: 1
2: 2
3: 1
4: 1
輸入輸出:
輸入格式: “%d”
輸出格式:
輸入提示信息:”Please enter the number:\n”
判斷該整數(shù)共有幾位:”%d: %d bits\n”
包含數(shù)字0的個(gè)數(shù):”0: %d\n”
包含數(shù)字1的個(gè)數(shù):”1: %d\n”
包含數(shù)字2的個(gè)數(shù):”2: %d\n”
包含數(shù)字3的個(gè)數(shù):”3: %d\n”
包含數(shù)字4的個(gè)數(shù):”4: %d\n”
包含數(shù)字5的個(gè)數(shù):”5: %d\n”
包含數(shù)字6的個(gè)數(shù):”6: %d\n”
包含數(shù)字7的個(gè)數(shù):”7: %d\n”
包含數(shù)字8的個(gè)數(shù):”8: %d\n”
包含數(shù)字9的個(gè)數(shù):”9: %d\n”
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
2 獎(jiǎng)金計(jì)算(6分)
題目?jī)?nèi)容:企業(yè)發(fā)放的獎(jiǎng)金根據(jù)利潤(rùn)提成。利潤(rùn)低于或等于10萬(wàn)元時(shí),獎(jiǎng)金可提10%;利潤(rùn)高于10萬(wàn)元,低于20萬(wàn)元時(shí),低于10萬(wàn)元的部分按10%提成,高于10萬(wàn)元的部分,可提成7.5%;20萬(wàn)到40萬(wàn)之間時(shí),高于20萬(wàn)元的部分,可提成5%;40萬(wàn)到60萬(wàn)之間時(shí),高于40萬(wàn)元的部分,可提成3%;60萬(wàn)到100萬(wàn)之間時(shí),高于60萬(wàn)元的部分,可提成1.5%,高于100萬(wàn)元時(shí),超過100萬(wàn)元的部分按1%提成,從鍵盤輸入當(dāng)月利潤(rùn)i,求應(yīng)發(fā)放獎(jiǎng)金總數(shù)?
示例:
程序運(yùn)行結(jié)果示例1:
789↙
bonus=78
程序運(yùn)行結(jié)果示例2:
789516↙
bonus=36342
輸入格式: “%ld”
輸出格式:”bonus=%ld\n”
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
3 程序修改—1(4分)
題目?jī)?nèi)容:修改下面這個(gè)程序使其快速計(jì)算1+2+3……+n的值,n從鍵盤輸入。并按照下面給出的運(yùn)行示例檢查程序。
#include <stdio.h>int main(){ int i, j, sum = 0, n=100; for (i=1,j=n; i<=j; i++,j--) {sum = sum + i + j;}printf("sum = %d", sum);return 0;}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
示例:
程序運(yùn)行結(jié)果示例1:
5↙
sum = 15
程序運(yùn)行結(jié)果示例2:
6↙
sum = 21
輸入輸出:
輸入格式: “%d”
輸出格式: “sum = %d” (注意:等號(hào)兩側(cè)各有一個(gè)空格)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
4 程序修改—2(4分)
題目?jī)?nèi)容:修改下面這個(gè)用do-while語(yǔ)句實(shí)現(xiàn)的程序,改用while語(yǔ)句實(shí)現(xiàn),并對(duì)比其優(yōu)缺點(diǎn)。
#include <stdio.h>int main(){ int sum = 0, m;do{printf("Input m:\n");scanf("%d", &m);sum = sum + m;printf("sum = %d\n", sum);}while (m != 0);return 0;}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
示例:
程序運(yùn)行結(jié)果示例:
Input m:
1↙
sum = 1
Input m:
2↙
sum = 3
Input m:
3↙
sum = 6
Input m:
4↙
sum = 10
Input m:
0↙
輸入輸出:
輸入格式:”%d”
輸出格式:
輸入提示: “Input m:\n”
輸出累加和: “sum = %d\n”(注意:等號(hào)兩側(cè)各有一個(gè)空格)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
5 程序改錯(cuò)-1(4分)
題目?jī)?nèi)容:我國(guó)古代的《張丘建算經(jīng)》中有這樣一道著名的百雞問題:“雞翁一,值錢五;雞母一,值錢三;雞雛三,值錢一。百錢買百雞,問雞翁、母、雛各幾何?”其意為:公雞每只5元,母雞每只3元,小雞3只1元。用100元買100只雞,問公雞、母雞和小雞各能買多少只?目前程序運(yùn)行結(jié)果有誤,請(qǐng)問為什么會(huì)比正確答案多出三個(gè)解?不僅要找出錯(cuò)誤和修正錯(cuò)誤,還要求利用以前學(xué)過的知識(shí)分析錯(cuò)誤的原因。
#include <stdio.h>int main(){int x, y, z;for (x=0; x<=20; x++){for (y=0; y<=33; y++){z = 100 - x - y;if (5*x + 3*y + z/3 == 100){printf("x=%d, y=%d, z=%d\n", x, y, z);}}}return 0;}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
程序目前的運(yùn)行結(jié)果:
x=0, y=25, z=75
x=3, y=20, z=77
x=4, y=18, z=78
x=7, y=13, z=80
x=8, y=11, z=81
x=11, y=6, z=83
x=12, y=4, z=84
程序正確的運(yùn)行結(jié)果:
x=0, y=25, z=75
x=4, y=18, z=78
x=8, y=11, z=81
x=12, y=4, z=84
輸入輸出:
輸入格式: 無(wú)
輸出格式:
“x=%d, y=%d, z=%d\n
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
6 程序改錯(cuò)-2(5分)
題目?jī)?nèi)容:從鍵盤任意輸入一個(gè)正整數(shù),編程判斷它是否是素?cái)?shù),若是素?cái)?shù),輸出“Yes!”,否則輸出“No!”。已知負(fù)數(shù)、0和1都不是素?cái)?shù)。請(qǐng)找出下面程序的錯(cuò)誤并改正之,同時(shí)按照給出的運(yùn)行示例檢查修改后的程序。
#include <stdio.h>#include <math.h>int main(){int n, i;printf("Input n:\n");scanf("%d", &n);for (i=2; i<=sqrt(n); i++){if (n % i = 0){printf("No!\n");}}printf("Yes!\n");return 0;}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
示例:
程序的運(yùn)行結(jié)果示例1:
Input n:
-3↙
No!
程序的運(yùn)行結(jié)果示例2:
Input n:
0↙
No!
程序的運(yùn)行結(jié)果示例3:
Input n:
1↙
No!
程序的運(yùn)行結(jié)果示例4:
Input n:
6↙
No!
程序的運(yùn)行結(jié)果示例5:
Input n:
7↙
Yes!
輸入輸出:
輸入格式: “%d”
輸出格式:
輸入提示信息: “Input n:\n”
是素?cái)?shù): “Yes!\n”
不是素?cái)?shù): “No!\n”
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
7 程序改錯(cuò)-3(4分)
題目?jī)?nèi)容:從鍵盤任意輸入兩個(gè)符號(hào)各異的整數(shù),直到輸入的兩個(gè)整數(shù)滿足要求為止,然后打印這兩個(gè)數(shù)。請(qǐng)通過測(cè)試找出下面這個(gè)程序存在的問題(不止一個(gè)問題哦),并改正。同時(shí)用下面給出的運(yùn)行結(jié)果示例檢查修改后的程序。
#include <stdio.h>int main(){int x1, x2;do{printf("Input x1, x2:");scanf("%d,%d", &x1, &x2);}while (x1 * x2 > 0);printf("x1=%d,x2=%d\n", x1, x2);return 0;}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
程序正確的運(yùn)行結(jié)果示例:
Input x1, x2:
a,s↙
Input x1, x2:
a,1↙
Input x1, x2:
2,s↙
Input x1, x2:
1,2↙
Input x1, x2:
-1,-2↙
Input x1, x2:
0,3↙
Input x1, x2:
1.2,3.4↙
Input x1, x2:
1.2,5↙
Input x1, x2:
-1,3↙
x1=-1,x2=3
輸入輸出:
輸入格式: “%d,%d”
輸出格式:
輸入提示信息:”Input x1, x2:\n”
輸出: “x1=%d,x2=%d\n”
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
8 猴子吃桃程序_擴(kuò)展1(4分)
題目?jī)?nèi)容:猴子第一天摘了若干個(gè)桃子,吃了一半,不過癮,又多吃了1個(gè)。第二天早上將剩余的桃子又吃掉一半,并且又多吃了1個(gè)。此后每天都是吃掉前一天剩下的一半零一個(gè)。到第n天再想吃時(shí),發(fā)現(xiàn)只剩下1個(gè)桃子,問第一天它摘了多少桃子?為了加強(qiáng)交互性,由用戶輸入不同的天數(shù)n進(jìn)行遞推,即假設(shè)第n天的桃子數(shù)為1。
示例:
程序的運(yùn)行結(jié)果示例1:
Input days:
5↙
x=46
程序的運(yùn)行結(jié)果示例2:
Input days:
10↙
x=1534
輸入輸出:
輸入格式: “%d”
輸出格式:
輸入提示信息:”Input days:\n”
輸出:”x=%d\n”
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
9 猴子吃桃程序_擴(kuò)展2(4分)
題目?jī)?nèi)容:猴子第一天摘了若干個(gè)桃子,吃了一半,不過癮,又多吃了1個(gè)。第二天早上將剩余的桃子又吃掉一半,并且又多吃了1個(gè)。此后每天都是吃掉前一天剩下的一半零一個(gè)。到第n天再想吃時(shí),發(fā)現(xiàn)只剩下1個(gè)桃子,問第一天它摘了多少桃子?為了加強(qiáng)交互性,由用戶輸入不同的天數(shù)n進(jìn)行遞推,即假設(shè)第n天的桃子數(shù)為1。同時(shí)還要增加對(duì)用戶輸入數(shù)據(jù)的合法性驗(yàn)證(如:不允許輸入的天數(shù)是0和負(fù)數(shù))
示例:
程序運(yùn)行結(jié)果示例:
Input days:
0↙
Input days:
-5↙
Input days:
a↙
Input days:
3↙
x=10
輸入輸出:
輸入格式: “%d”
輸出格式:
輸入提示信息:”Input days:\n”
輸出:”x=%d\n”
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
10 6位密碼輸入檢測(cè)(4分)
題目?jī)?nèi)容:從鍵盤輸入6位僅由數(shù)字0~9組成的密碼。用戶每輸入一個(gè)密碼并按回車鍵后,程序給出判斷:如果是數(shù)字,則原樣輸出該數(shù)字,并提示用戶目前已經(jīng)輸入了幾位密碼,同時(shí)繼續(xù)輸入下一位密碼;否則,程序提示”error”,并讓用戶繼續(xù)輸入下一位密碼。直到用戶輸入的密碼全部是數(shù)字為止。
示例:
程序的運(yùn)行結(jié)果示例:
Input your password:
1↙
1, you have enter 1-bits number
6↙
6, you have enter 2-bits number
a↙
error
d↙
error
4↙
4, you have enter 3-bits number
6↙
6, you have enter 4-bits number
8↙
8, you have enter 5-bits number
2↙
2, you have enter 6-bits number
輸入輸出:
輸入提示信息:”Input your password:\n”
輸入格式: “%c”
輸出格式:
如果輸入的是數(shù)字,輸出格式為:”%c, you have enter %d-bits number\n”
如果輸入的不是數(shù)字,輸出提示信息:”error\n”
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
11 判斷一個(gè)整型數(shù)據(jù)有幾位v1.0(4分)
題目?jī)?nèi)容:從鍵盤輸入一個(gè)整型數(shù)據(jù)(int型),編寫程序判斷該整數(shù)共有幾位。例如,從鍵盤輸入整數(shù)16644,該整數(shù)共有5位。
示例:
程序運(yùn)行結(jié)果示例1:
Please enter the number:
21125↙
21125: 5 bits
程序運(yùn)行結(jié)果示例2:
Please enter the number:
-12234↙
-12234: 5 bits
輸入輸出:
輸入提示信息:”Please enter the number:\n”
輸入格式: “%d”
輸出格式:”%d: %d bits\n”
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
12 檢測(cè)輸入數(shù)據(jù)中奇數(shù)和偶數(shù)的個(gè)數(shù)(4分)
題目?jī)?nèi)容:從鍵盤輸入一系列正整數(shù),輸入-1表示輸入結(jié)束(-1本身不是輸入的數(shù)據(jù))。編寫程序判斷輸入數(shù)據(jù)中奇數(shù)和偶數(shù)的個(gè)數(shù)。如果用戶輸入的第一個(gè)數(shù)據(jù)就是-1,則程序輸出”over!”。否則。用戶每輸入一個(gè)數(shù)據(jù),輸出該數(shù)據(jù)是奇數(shù)還是偶數(shù),直到用戶輸入-1為止,分別統(tǒng)計(jì)用戶輸入數(shù)據(jù)中奇數(shù)和偶數(shù)的個(gè)數(shù)。
示例:
程序運(yùn)行結(jié)果示例1:
Please enter the number:
1↙
1:odd
5↙
5:odd
8↙
8:even
9↙
9:odd
12↙
12:even
17↙
17:odd
-1↙
The total number of odd is 4
The total number of even is 2
程序運(yùn)行結(jié)果示例2:
Please enter the number:
-1↙
over!
The total number of odd is 0
The total number of even is 0
輸入輸出:
輸入提示信息:”Please enter the number:\n”
輸入格式: “%d”
輸出格式:
用戶輸入的第一個(gè)數(shù)據(jù)就是-1,輸出格式:”over!\n”
奇數(shù)的輸出格式:”%d:odd\n”
偶數(shù)的輸出格式:”%d:even\n”
輸入數(shù)據(jù)中奇數(shù)的個(gè)數(shù)統(tǒng)計(jì):”The total number of odd is %d\n”
輸入數(shù)據(jù)中偶數(shù)的個(gè)數(shù)統(tǒng)計(jì):”The total number of even is %d\n”
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
第六章
1 繪制金字塔(4分)
題目?jī)?nèi)容:要求用戶從鍵盤輸入一個(gè)大寫字母,使用嵌套循環(huán)產(chǎn)生像下面這樣的金字塔圖案:
A
ABA
ABCBA
ABCDCBA
示例:
程序運(yùn)行結(jié)果示例1:
Please input a capital:
D↙
____A
___ABA
__ABCBA
_ABCDCBA
程序運(yùn)行結(jié)果示例2:
Please input a capital:
F↙
______A
_____ABA
____ABCBA
___ABCDCBA
__ABCDEDCBA
_ABCDEFEDCBA
(說(shuō)明:上面運(yùn)行結(jié)果示例中,每行字母前面的下劃線”_”代表屏幕上實(shí)際輸出的是空格,最后一行前面有一個(gè)空格,倒數(shù)第二行有兩個(gè)空格,以此類推。)
輸入輸出:
輸入提示信息:”Please input a capital:\n”
輸入格式: “%c”
輸出格式:”%c”
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
2 循環(huán)嵌套的應(yīng)用(4分)
題目?jī)?nèi)容:編寫程序產(chǎn)生如下輸出:
F
FE
FED
FEDC
FEDCB
FEDCBA
輸入輸出:
輸入格式: 無(wú)
輸出格式:”%c”
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
3 利用泰勒級(jí)數(shù)計(jì)算sinx的值(4分)
題目?jī)?nèi)容:利用泰勒級(jí)數(shù)計(jì)算sinx的值,要求最后一項(xiàng)的絕對(duì)值小于,并統(tǒng)計(jì)出此時(shí)累加了多少項(xiàng)。請(qǐng)用“利用前項(xiàng)來(lái)計(jì)算后項(xiàng)”的方法計(jì)算累加項(xiàng),不要使用pow函數(shù)編寫程序。程序中所有實(shí)數(shù)的數(shù)據(jù)類型都是double類型。
sin\approx x-\frac{x^3}{3!}+\frac{x^5}{5!}-\frac{x^7}{7!}+\frac{x^9}{9!}-\cdots
示例:
程序的運(yùn)行結(jié)果示例1:
Input x:
3↙
sin(x)=0.141,count=9
程序的運(yùn)行結(jié)果示例2:
Input x:
10↙
sin(x)=-0.544,count=18
輸入提示信息:”Input x:\n”
輸入格式: “%lf”
輸出格式:”sin(x)=%.3f,count=%d\n”
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
4 計(jì)算100~200之間的所有素?cái)?shù)之和(4分)
題目?jī)?nèi)容:計(jì)算100~200之間的所有素?cái)?shù)之和,判別一個(gè)數(shù)是否是素?cái)?shù)請(qǐng)用給定的函數(shù)實(shí)現(xiàn)。
函數(shù)原型:int fun(int m);
說(shuō)明:
參 數(shù):m 是要進(jìn)行判斷的數(shù);
返回值:若數(shù) m 是素?cái)?shù),則返回值為1;否則返回值為0。
輸入輸出:
輸入格式: 無(wú)
輸出格式: “sum=%d\n”
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
5 編程實(shí)現(xiàn)一個(gè)輸入指定范圍內(nèi)的整數(shù)的函數(shù)(4分)
題目?jī)?nèi)容:編程實(shí)現(xiàn)一個(gè)輸入指定范圍內(nèi)的整數(shù)的函數(shù)getint,其完整的函數(shù)原型為:int getint(int min, int max);,它負(fù)責(zé)接收用戶的輸入進(jìn)行驗(yàn)證,保證接收的一定是一個(gè)介于min和max之間([min, max]區(qū)間內(nèi))的一個(gè)整數(shù)并最后返回該整數(shù)。如果用戶輸入不合法,則會(huì)提示繼續(xù)輸入,直到輸入合法時(shí)為止。要求編寫完整的程序并測(cè)試你所寫的getint函數(shù)。
示例:
程序的運(yùn)行結(jié)果示例:
Please enter min,max:
3,100↙
Please enter an integer [3..100]:
-2↙
Please enter an integer [3..100]:
0↙
Please enter an integer [3..100]:
116↙
Please enter an integer [3..100]:
58↙
The integer you have entered is:58
輸入輸出:
輸入提示信息:
“Please enter min,max:\n”
“Please enter an integer [%d..%d]:\n”
輸入格式:
輸入數(shù)據(jù)區(qū)間的最小值和最大值:”%d,%d”
輸入指定范圍內(nèi)的整數(shù): “%d”
輸出格式:”The integer you have entered is:%d\n”
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
6 程序改錯(cuò)v2.0(5分)
題目?jī)?nèi)容:下面代碼的功能是將百分制成績(jī)轉(zhuǎn)換為5分制成績(jī),具體功能是:如果用戶輸入的是非法字符或者不在合理區(qū)間內(nèi)的數(shù)據(jù)(例如輸入的是a,或者102,或-45等),則程序輸出 Input error!,并允許用戶重新輸入,直到輸入合法數(shù)據(jù)為止,并將其轉(zhuǎn)換為5分制輸出。目前程序存在錯(cuò)誤,請(qǐng)將其修改正確。并按照下面給出的運(yùn)行示例檢查程序。
#include<stdio.h>int main(){int score;char grade;printf("Please input score:");scanf("%d", &score);if (score < 0 || score > 100) printf("Input error!\n");else if (score >= 90) grade = 'A’;else if (score >= 80)grade = 'B'; else if (score >= 70)grade = 'C'; else if (score >= 60)grade = 'D'; elsegrade = 'E'; printf("grade:%c\n", grade);return 0;}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
示例:
程序運(yùn)行結(jié)果示例1:
Please input score:
a↙
Input error!
Please input score:
-12↙
Input error!
Please input score:
230↙
Input error!
Please input score:
92↙
grade: A
程序運(yùn)行結(jié)果示例2:
Please input score:
88↙
grade: B
程序運(yùn)行結(jié)果示例3:
Please input score:
73↙
grade: C
程序運(yùn)行結(jié)果示例4:
Please input score:
65↙
grade: D
程序運(yùn)行結(jié)果示例5:
Please input score:
27↙
grade: E
輸入輸出:
輸入提示信息:”Please input score:\n”
輸入格式: “%d”
輸出格式:
輸入錯(cuò)誤時(shí)的提示信息:”Input error!\n”
輸出格式:”grade: %c\n” (注意:%c前面有一個(gè)空格)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
7 編程計(jì)算a+aa+aaa+…+aa…a(n個(gè)a)的值(4分)
題目?jī)?nèi)容:編程計(jì)算 a+aa+aaa+…+aa…a(n個(gè)a)的值,n和a的值由鍵盤輸入。例如,當(dāng)n=4,a=2,表示計(jì)算2+22+222+2222的值。
示例:
程序運(yùn)行結(jié)果示例:
Input a,n:
2,4↙
sum=2468
輸入輸出:
輸入提示信息:”Input a,n:\n”
輸入格式: “%d,%d”(先輸入a,后輸入n)
輸出格式: “sum=%ld\n”
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
8 搬磚問題(4分)
**題目?jī)?nèi)容:**n塊磚( 27
#include<stdio.h> int main(){int i, n, men, women, children, sum=36;printf("Input n(27<n<=77):\n");scanf("%d",&n);for(men=0;men<=n/4;men++){for(women=0;women<n/3;women++){if(men+women+2*(n-4*men-3*women)==sum)printf("men=%d,women=%d,children=%d\n",men,women,sum-men-women);}}return 0; }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
9 編程輸出某年某月有多少天(考慮到閏年)(5分)
題目?jī)?nèi)容:從鍵盤輸入一個(gè)年份和月份,輸出該月有多少天(考慮閏年),用switch語(yǔ)句編程。
示例:
程序運(yùn)行結(jié)果示例1:
Input year,month:
2015,3↙
31 days
程序運(yùn)行結(jié)果示例2:
Input year,month:
2015,4↙
30 days
程序運(yùn)行結(jié)果示例3:
Input year,month:
2016,2↙
29 days
程序運(yùn)行結(jié)果示例4:
Input year,month:
2014,2↙
28 days
程序運(yùn)行結(jié)果示例5:
Input year,month:
2015,13↙
Input error!
輸入輸出:
輸入提示信息:”Input year,month:\n”
輸入格式: “%d,%d”
輸出格式:
輸入錯(cuò)誤時(shí)的提示信息:
“Input error!\n”
“31 days\n”
“30 days\n”
“29 days\n”
“28 days\n”
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
PS:有不少人像我反映,有的題目沒有按照題目要求做。的確由于時(shí)間原因,我并不是每道題都認(rèn)真看完題目了,但是以上代碼都是能AC的,只能說(shuō)明這個(gè)OJ系統(tǒng)也只是對(duì)輸出進(jìn)行字符匹配,并不能做其他check。想按照題目要求解的,自行解決,畢竟題目都不難。。。Good Luck!!!
一共18周,我有時(shí)間就寫,爭(zhēng)取每個(gè)月寫點(diǎn),如果有一個(gè)月沒寫,應(yīng)該會(huì)抽時(shí)間補(bǔ)上,畢竟我還要刷LeetCode的人工智能的題。。。
總結(jié)
以上是生活随笔為你收集整理的哈工大C语言公开课练兵编程(二)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浏览器播放rtsp视频流方案(ffmpe
- 下一篇: 初探EntityFramework——空