本文已参与「新人创作礼」活动,一起开启掘金创作之路。
@TOC
题目大意
一个商品原价A 现价B,问优惠了百分之多少
解题思路
ABC第一题,难度不会过高。 虽有精度问题,但是C++中double就够了。
注意事项
记得(a-b)/a后要×100 问的是百分之多少。
AC代码
C++版
#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
int main()
{
double a, b;
cin >> a >> b;
double c = 100 * (a - b) / a;
printf("%.10lf\n", c);
return 0;
}
Python高精度
import decimal
a, b = input().split(' ')
a = decimal.Decimal(a)
b = decimal.Decimal(b)
c = a - b
d = c / a
d *= 100
print(d)
Python普通版(3行)
a, b = input().split()
a, b = int(a), int(b)
print(100 * (a - b) / a)
Python精简版(2行)
a, b = map(int, input().split(' '))
print(100 * (a - b) / a)
同步发文于我的CSDN