1 輸出逆序數(3分)
題目內容:
從鍵盤任意輸入一個3位整數,編程計算並輸出它的逆序數(忽略整數前的正負號)。例如,輸入-123,則忽略負號,由123分離出其百位1、十位2、個位3,然后計算3*100+2*10+1 = 321,並輸出321。
提示:
1. 從鍵盤輸入數據可以使用函數scanf()。例如,scanf("%d", &x); 表示從鍵盤輸入整數並存入整形變量x中。
2. 利用取絕對值函數 fabs()忽略輸入數據的負號。fabs(x)表示計算變量x的絕對值。
3.使用數學函數,需要在程序開頭加上編譯預處理指令 #include <math.h>
#include <stdio.h>
#include <math.h>
int main() {
//用戶輸入的三位數
int x;
//逆序數
int y;
//獲取鍵盤輸入
scanf("%d",&x);
//轉換成絕對值
x = fabs(x);
//判斷是否為3位數
if(100 <= x && x <= 999){
int a,b,c;
//百位
a = x / 100;
//十位
b = x /10 %10;
//個位
c = x % 10;
y = c * 100 + b * 10 + a;
}
printf("Input x:\n");
printf("y=%d\n",y);
return 0;
}
2 計算總分和平均分(3分)
題目內容:
小明本學期共有5門課程,分別是英語、語文、數學、歷史和音樂。5科的期中考試成績分別是86分、74分、92分、77分、82分,期末考試成績分別是81分、87分、90分、62分、88分。已知期中和期末考試成績分別占總成績的30%和70%。定義相應的變量存放各科成績,並計算出小明5門課程的總分和平均分。要求平均分輸出兩種形式:帶2位小數的浮點數形式和不帶小數的整數形式。要求總分輸出帶2位小數的浮點數形式。程序中浮點數的數據類型均為float類型。
提示:
輸出不帶小數的平均分的整數形式可以使用強制類型轉換。
#include <stdio.h>
int main(){
//期中
float midtermEnglish = 86,midtermChinese = 74,midtermMath = 92,midtermHistory = 77,midtermMusic = 82;
//期末
float termEndEnglish = 81,termEndChinese = 87,termEndMath = 90,termEndHistory = 62,termEndMusic = 88;
float total,average;
total = (midtermEnglish + midtermChinese + midtermMath + midtermHistory + midtermMusic ) * 0.3 + (termEndEnglish + termEndChinese + termEndMath + termEndHistory + termEndMusic) * 0.7;
average = total / 5;
printf("total=%.2f\n",total);
printf("average=%.2f\n",average);
printf("average=%d\n",(int)average);
return 0;
}
3 存款利率計算器V1.0(3分)
題目內容:
設銀行定期存款的年利率rate為2.25%,已知存款期為n年,存款本金為capital元,試編程計算並輸出n年后的本利之和deposit。程序中所有浮點數的數據類型均為double類型。
提示:
1. 從鍵盤輸入數據可以使用函數scanf()。本例中為scanf("%lf,%d,%lf", &rate, &n, &capital);
2. 本程序最終計算的是復利。
3. 計算冪的數學函數為pow(a,n), 代表a的n次冪。
4. 使用數學函數,需要在程序開頭加上編譯預處理指令 #include <math.h>
#include <stdio.h>
#include <math.h>
int main(){
double capital,rate,deposit;
int year;
printf("Please enter rate, year, capital:\n");
scanf("%lf,%d,%lf",&rate,&year,&capital);
//deposit=pow((1.0 + rate),year)*capital;
for (; year > 0; year--) {
capital = capital + (capital * rate);
}
deposit = capital;
printf("deposit=%.3f\n",deposit);
return 0;
}
4 數位拆分v1.0(3分)
題目內容:
現有一個4位數的正整數n=4321(即n是一個已知的數,固定為4321),編寫程序將其拆分為兩個2位數的正整數43和21,計算並輸出拆分后的兩個數的加、減、乘、除和求余的結果。例如n=4321,設拆分后的兩個整數為a,b,則a=43,b=21。除法運算結果要求精確到小數點后2位,數據類型為float。
#include <stdio.h>
int main(){
int num = 4321;
int a,b;
int num1,num2,num3,num4;
//千位
num4 = num / 1000;
//百位
num3 = (num / 100) % 10;
//十位
num2 = (num % 100) / 10;
//個位
num1 = num % 10;
a = num4 * 10 + num3;
b = num2 * 10 + num1;
printf("a=%d,b=%d\n",a,b);
printf("a+b=%d\n",a + b);
printf("a-b=%d\n",a - b);
printf("a*b=%d\n",a * b);
printf("a/b=%.2f\n",(float)a / b);
printf("a%%b=%d\n",a % b);
return 0;
}
5 求正/負余數(3分)
題目內容:
在C語言中,如果被除數為負值,則對一個正數求余的時候,求出的余數也是一個負數。在某些場合下,我們需要求出它的正余數,例如:在C語言中有(-11)%5=-1,但是有時我們希望得到的余數不是-1,而是4。請編寫程序計算(-11)%5的負余數和正余數。
#include <stdio.h>
int main(){
int x,y;
x = -11;
y = 5;
printf("negative: %d\n",x % y);
printf("positive: %d\n",x % y + y);
return 0;
}
6 身高預測(3分)
題目內容:
已知小明(男孩)爸爸的身高是175厘米,媽媽的身高是162厘米。小紅(女孩)爸爸的身高是169厘米,媽媽的身高是153厘米,按照下面公式,預測並輸出小明和小紅的遺傳身高(不考慮后天因素)。
男性成人時身高=(faHeight + moHeight)×0.54cm
女性成人時身高=(faHeight×0.923 + moHeight)/2cm
#include <stdio.h>
int main(){
int man = (175 + 162) * 0.54;
int woman = (169 * 0.923 + 153) / 2;
printf("Height of xiao ming:%d\n",man);
printf("Height of xiao hong:%d\n",woman);
return 0;
}
7 求一元二次方程的根(3分)
題目內容:
根據下面給出的求根公式,計算並輸出一元二次方程的兩個實根,要求精確到小數點后4位。程序中所有浮點數的數據類型均為float.
提示:
1. 計算平方根的數學函數為sqrt()。
2. 使用數學函數,需要在程序開頭加上編譯預處理指令 #include <math.h>
#include <stdio.h>
#include <math.h>
int main(){
float x1,x2;
x1=(-3+sqrt(-3*-3-4*2*1))/(float)(2*2);
x2=(-3-sqrt(-3*-3-4*2*1))/(float)(2*2);//強制轉換
printf( "x1=%.4f\n",x1);
printf( "x2=%.4f\n",x2);
return 0;
}