时间限制: 1 Sec 内存限制: 128 MB
题目描述
现有商场进行降价促销,当购买商品总额在[100,200]之间时,折扣为95折,即总价=95%*总额,购买商品大于200元时,折扣为92折,购买金额不足100元时,无折扣。请编写程序计算需要支付的实际费用。
在程序的主函数中输入购买各个商品的费用总额(一组小数,以空格分隔,以’\n’或EOF结束输入,最多支持100件商品),请编写名为calPrice的函数计算实际费用,要求其函数原型为:
float calPrice(float price);
其中:形参price为各个商品的折扣前费用总额。返回为需要支付的实际费用。
在程序主函数中输出费用总额及实际费用,如:“167.00 158.65”。注意需要精确到分(小数点后两位),两数之间用两个制表符分隔。
输入
一组小数,以空格分隔,以’\n’或EOF结束,程序最多支持100件商品
输出
两个制表符分隔的消费总额及折扣后的实际费用,每条消费信息显示为一行结果。
样例输入
167 78.3
样例输出
167.00 158.65
78.30 78.30
杂谈
考试水题之我就考转义字符和分支
考试不水题之我就考你读题你一定想不到
代码
/*那么我也水题之注释我不想写了
然后就出问题了,这个玩意他还要求了100个输出输出
*/
#include<stdio.h>
float calPrice(float price);
int main () {
float x;
char ch;
int count=100;
while (count&&scanf("%f",&x)!=EOF) {
ch=getchar();
printf("%.2f\t\t%.2f\n",x,calPrice(x));
if(ch=='\n') break;
count--;
}
return 0;
}
float calPrice(float p) {
if(p<100) return p;
else if(p>200) return 0.92*p;
else return 0.95*p;
}
既然要刺激那就贯彻到底的代码
#include<stdio.h>
#define COUNT 1e-2-1e-3
float calPrice(float price);
int qread (float *x);
int main () {
float x;
int flag;
int count=100;
do{
flag=qread(&x);
if(x<100&&100-x<COUNT) x=99.99;
printf("%.2f\t\t%.2f\n",x,calPrice(x));
count--;
} while (flag&&count);
return 0;
}
int qread (float *x ) {//负数会输出0
char ch=getchar();
float tem=0,temn=10;
int flag=0,f=1;
while (ch==' '||ch=='-') {
if(ch=='-') f=0;
ch=getchar();
}
while (ch!=' '&&ch!=EOF&&ch!='\n') {
if(ch<='9'&&ch>='0') {
if(flag) {
tem+=(ch-48)/temn;
temn*=10;
}
else {
tem*=10;
tem+=ch-'0';
}
}
else if(ch=='.') {
flag=1;
}
ch=getchar();
}
*x=f*tem;
if(ch==EOF||ch=='\n') return 0;
else return 1;
}
float calPrice(float p) {
if(p<100) return p;
else if(p-200>COUNT) return 0.92*p;
else return 0.95*p;
}