买衣服打折(discount)–算法竞赛入门经典习题1-5:一件衣服95元,若消费满300元,可打85折。输入购买衣服件数,输出需要支付的金额(单位:元),保留2位小数。(C++实现)
前言
使用了自定义类型#define 形式,设置常量p的替换词
1.程序代码如下:
#include<iostream>
#include <cstdlib>
#define p 95
using namespace std;
int main()
{
int n;
float s = 0;
cout << "请输入购买衣服件数n" << endl;
cin >> n;
if ((n * p) >= 300)
s = float(n * p) * 0.85;
else
s = float(n * p);
printf("%.2f", s);
return 0;
}
2.示例运行结果如图