1 分數比較(4分)
題目內容:
利用人工方式比較分數大小的最常見的方法是:對分數進行通分后比較分子的大小。請編程模擬手工比較兩個分數的大小。首先輸入兩個分數分子分母的值,例如"11/13,17/19",比較分數大小后輸出相應的提示信息。例如,第一個分數11/13小於第二個分數17/19,則輸出"11/13<17/19"。
int main(){
int a,b,c,d;
printf("Input a/b, c/d:" );
scanf("%d/%d,%d/%d",&a,&b,&c,&d);
if((float) a / b > (float) c / d){
printf("%d/%d>%d/%d\n",a,b,c,d);
} else if((float) a / b < (float) c / d){
printf("%d/%d<%d/%d\n",a,b,c,d);
} else{
printf("%d/%d=%d/%d\n",a,b,c,d);
}
return 0;
}
2 存款利率計算器v2.0(4分)
題目內容:
設capital是最初的存款總額(即本金),rate是整存整取的存款年利率,n 是儲蓄的年份,deposit是第n年年底賬號里的存款總額。已知如下兩種本利之和的計算方式:
-
按復利方式計息的本利之和計算公式為:

-
按普通計息方式計算本利之和的公式為:
deposit = capital * (1 + rate * n)
編程從鍵盤輸入存錢的本金、存款期限以及年利率,然后再輸入按何種方式計息,最后再計算並輸出到期時能從銀行得到的本利之和,要求結果保留到小數點后4位。
提示:使用數學函數需要加入頭文件 <math.h>
int main(){
double rate,capital,deposit;
int year;
char c;
printf("Input rate, year, capital:");
scanf("%lf,%d,%lf",&rate,&year,&capital);
printf("Compound interest (Y/N)?");
scanf(" %c",&c);
if(c == 'Y' || c == 'y'){
deposit = pow((1.0 + rate),year) * capital;
} else{
deposit = capital * (1 + rate * year);
}
printf("deposit = %.4f\n",deposit);
return 0;
}
3 存款利率計算器v3.0(9分)
題目內容:
設capital是最初的存款總額(即本金),rate是整存整取的存款年利率,n 是儲蓄的年份,deposit是第n年年底賬號里的存款總額。已知如下兩種本利之和的計算方式:
-
按復利方式計息的本利之和計算公式為:

-
按普通計息方式計算本利之和的公式為:
deposit = capital * (1 + rate * n)
已知銀行整存整取不同期限存款的年息利率分別為:
存期1年,利率為 0.0225
存期2年,利率為 0.0243
存期3年,利率為 0.0270
存期5年,利率為 0.0288
存期8年,利率為 0.0300
若輸入其他年份,則輸出"Error year!"
編程從鍵盤輸入存錢的本金和存款期限,然后再輸入按何種方式計息,最后再計算並輸出到期時能從銀行得到的本利之和,要求結果保留到小數點后4位。
int main(){
double capital,deposit,rate;
int year;
char c;
printf("Input capital, year:");
scanf("%lf,%d",&capital,&year);
printf("Compound interest (Y/N)?");
scanf(" %c",&c);
if(c == 'y' || c == 'Y') {
switch (year) {
case 1:
rate = 0.0225;
deposit = capital * pow((1.0 + rate),year);
break;
case 2:
rate = 0.0243;
deposit = capital * pow((1.0 + 0.0243),year);
break;
case 3:
rate = 0.0270;
deposit = capital * pow((1.0 + rate),year);
break;
case 5:
rate = 0.0288;
deposit = capital * pow((1.0 + rate),year);
break;
case 8:
rate = 0.0300;
deposit = capital * pow((1.0 + rate),year);
break;
default:
printf("Error year!\n");
return 0;
}
} else{
switch (year) {
case 1:
rate = 0.0225;
deposit = capital * (1.0 + rate * year);
break;
case 2:
rate = 0.0243;
deposit = capital * (1.0 + rate * year);
break;
case 3:
rate = 0.0270;
deposit = capital * (1.0 + rate * year);
break;
case 5:
rate = 0.0288;
deposit = capital * (1.0 + rate * year);
break;
case 8:
rate = 0.0300;
deposit = capital * (1.0 + rate* year);
break;
default:
printf("Error year!\n");
return 0;
}
}
printf("rate = %.4f, deposit = %.4f\n",rate,deposit);
return 0;
}
4 博弈論之Best Response(6分)
題目內容:
在博弈論中,有一種決策稱為Best Response,通俗的意思就是選擇一種策略使得團體利益最大化。C語言學習成績的評定方式分為兩種,一種是自由刷題模式(compete),沒有固定標准,刷題越多者排名越靠前,其期末分數越高;另一種是規定每個人必須做夠多少道題(standard),達到要求就能取得相應分數。
假設一個班級中的學生分為A、B兩類,A類同學學習熱情很高,樂於做題,采用compete模式可以獲得成就感並且在期末拿到高分,compete模式可以讓他們有10分的收益;采用standard模式他們也可以在期末拿到高分,但不能滿足他們的求知欲,standard模式可以讓他們有8分的收益。B類同學僅僅希望期末拿高分,如果采用compete模式,他們競爭不過A類同學,期末成績不理想,因此compete模式能給他們6分的收益;如果采用standard模式,他們可以完成規定任務並拿到高分,因此standard模式可以讓他們有10分的收益。
編程輸入A類和B類同學分別占班級總人數的百分比,分別計算並輸出采用compete和standard兩種刷題模式下的全班總收益,並輸出這個班級在這場博弈中的Best Response是哪種模式。
注: 程序中使用的數據類型為float
int main(){
float a,b,compete,standard;
printf("Input percent of A and B:");
scanf("%f%f",&a,&b);
compete = a * 10 + b *6;
standard = a * 8 + b *10;
printf("compete = %.4f\nstandard = %.4f\n",compete,standard);
if(compete <= standard){
printf("The Best Response is standard!");
} else{
printf("The Best Response is compete!");
}
return 0;
}
