第一关:快递费用计算
#include<stdio.h>
int main(void)
{
/*********Begin*********/
int area; // 地区
float weight; // 重量
float beginWeight = 1.0; // 起重量
float price = 0.0; // 价格
scanf("%d,%f",&area,&weight);
if(area == 0){ // 同城
if(weight < beginWeight){
price = 10;
}else{
if((int)weight != weight){
price = 10 + ((int)(weight-beginWeight)+1)*3;
}else{
price = 10 + (weight-beginWeight)*3;
}
}
}else if(area == 1){
if(weight < beginWeight){
price = 10;
}else{
if((int)weight != weight){
price = 10 + ((int)(weight-beginWeight)+1)*4;
}else{
price = 10 + (weight-beginWeight)*4;
}
}
}else if(area == 2){
if(weight < beginWeight){
price = 15;
}else{
if((int)weight != weight){
price = 15 + ((int)(weight-beginWeight)+1)*5;
}else{
price = 15 + (weight-beginWeight)*5;
}
}
}else if(area == 3){
if(weight < beginWeight){
price = 15;
}else{
if((int)weight != weight){
price = 15 + ((int)(weight-beginWeight)+1)*6.5;
}else{
price = 15 + (weight-beginWeight)*6.5;
}
}
}else if(area == 4){
if(weight < beginWeight){
price = 15;
}else{
if((int)weight != weight){
price = 15 + ((int)(weight-beginWeight)+1)*10;
}else{
price = 15 + (weight-beginWeight)*10;
}
}
}else{
printf("Error in Area\n");
}
printf("Price: %.2f",price);
/*********End**********/
return 0;
}
第二关:计算一元二次方程根
#include<stdio.h>
// 加入开根号的数学头文件
#include<math.h>
int main(void)
{
/*********Begin*********/
float a,b,c,x1,x2;
printf("Please enter the coefficients a,b,c:\n");
scanf("%f,%f,%f",&a,&b,&c);
// 实根需要满足的条件 b^2-4ac>=0并且a!=0
if(b*b-4*a*c>=0&&a!=0){
// sqrt() 可以用来开根号
x1 = (-b + sqrt(b*b-4*a*c))/(2*a);
x2 = (-b - sqrt(b*b-4*a*c))/(2*a);
printf("x1=%.4f, x2=%.4f\n",x1,x2);
}else {
printf("error!");
}
/*********End**********/
return 0;
}
第三关:产品信息格式化
#include<stdio.h>
int main(void)
{
/*********Begin*********/
int number;
float price;
int month;
int day;
int year;
printf("Enter item number:");
scanf("%d",&number);
printf("\nEnter unit price:");
scanf("%f",&price);
printf("\nEnter purchase date (mm/dd/yy):");
scanf("%d/%d/%d",&month,&day,&year);
printf("\nItem Unit Purchase\n");
printf("%-9d$ %-9.2f%02d%02d%02d\n",number,price,month,day,year);
/*********End**********/
return 0;
}