1 日期顯示(3分)
題目內容:
編寫一個程序, 接收用戶錄入的日期信息並且將其顯示出來. 其中, 輸入日期的形式為月/日/年(mm/dd/yy), 輸出日期的形式為年月日(yy.mm.dd)。
#include <stdio.h>
int main(){
int year,month,day;
printf("Enter a date (mm/dd/yy):\n");
scanf("%d/%d/%d",&month,&day,&year);
printf("You entered the date: %04d.%02d.%02d\n",year,month,day);
return 0;
}
2 產品信息格式化(3分)
題目內容:
編寫一個程序, 對用戶錄入的產品信息進行格式化。
int main(){
int year,month,day,item;
float price;
printf("Enter item number:\n");
scanf("%d",&item);
printf("Enter unit price:\n");
scanf("%f",&price);
printf("Enter purchase date (mm/dd/yy):\n");
scanf("%d/%d/%d",&month,&day,&year);
printf("Item Unit Purchase\n");
printf("%-9d$ %-9.2f%02d%02d%04d\n",item,price,month,day,year);
return 0;
}
3 計算兩個數的平方和(3分)
題目內容:
從鍵盤讀入兩個實數,編程計算並輸出它們的平方和,要求使用數學函數pow(x,y)計算平方值,輸出結果保留2位小數。 程序中所有浮點數的數據類型均為float。
提示:使用數學函數需要在程序中加入編譯預處理命令 #include <math.h>
int main(){
float x,y;
printf("Please input x and y:\n");
scanf("%f,%f",&x,&y);
printf("Result=%.2f\n",pow(x,2.0) + pow(y,2.0));
return 0;
}
4 逆序數的拆分計算(3分)
題目內容:
從鍵盤輸入一個4位數的整數,編程計算並輸出它的逆序數(忽略整數前的正負號)。例如,輸入-1234,忽略負號,由1234分離出其千位1、百位2、十位3、個位4,然后計算4*1000+3*100+2*10+1 = 4321,並輸出4321。再將得到的逆序數4321拆分為兩個2位數的正整數43和21,計算並輸出拆分后的兩個數的平方和的結果。
int main(){
int x;
printf("Input x:\n");
scanf("%d",&x);
//絕對值
x = fabs(x);
if(x >= 1000 && x <= 9999){
int a,b,c,d,e,f;
//千位
a = x / 1000;
//百位
b = (x / 100) % 10;
//十位
c = (x % 100) / 10;
//個位
d = x % 10;
e = d * 10 + c;
f = b * 10 + a;
printf("y=%d\n",d * 1000 + c * 100 + b * 10 + a);
printf("a=%d,b=%d\n",e,f);
printf("result=%d\n",(int)(pow(e,2) + pow(f,2)));
} else{
printf("輸入不合法!!!");
}
return 0;
}
5 拆分英文名(3分)
題目內容:
從鍵盤輸入某同學的英文名(小寫輸入,假設學生的英文名只包含3個字母。如: tom),編寫程序在屏幕上輸出該同學的英文名,且首字母大寫(如: Tom)。同時輸出組成該英文名的所有英文字符在26個英文字母中的序號。
int main(){
char a,b,c;
printf("Input your English name:\n");
scanf("%c%c%c",&a,&b,&c);
printf("%c%c%c\n",a - 32,b,c);
printf("%c:%d\n",a,a - 96);
printf("%c:%d\n",b,b - 96);
printf("%c:%d\n",c,c - 96);
return 0;
}
6 計算體指數(3分)
題目內容:
從鍵盤輸入某人的身高(以厘米為單位,如174cm)和體重(以公斤為單位,如70公斤),將身高(以米為單位,如1.74m)和體重(以斤為單位,如140斤)輸出在屏幕上,並按照以下公式計算並輸出體指數,要求結果保留到小數點后2位。程序中所有浮點數的數據類型均為float。
假設體重為w公斤,身高為h米,則體指數的計算公式為:
int main(){
int weight,height;
float h;
printf("Input weight, height:\n");
scanf("%d,%d",&weight,&height);
printf("weight=%d\n",weight * 2);
h = height / 100.0;
printf("height=%.2f\n",h);
printf("t=%.2f\n",weight / h / h);
return 0;
}