题目描述:
Among grandfather's papers a bill was found.
找到个账单
72 turkeys $_679_
The first and the last digits of the number that obviously represented the total price of those turkeys are replaced here by blanks (denoted _),for they are faded and are illegible.
首 末位 都弄脏了 不可见
What are the two faded digits and what was the price of one turkey?
两位数是啥? 1个Turkeys的单价是啥? (turkey.火鸡 土耳其)
We want to write a program that solves a general version of the above problem.
写个程序 解决总的版本 of上述问题
N turkeys $_XYZ_
The total number of turkeys, N, is between 1 and 99, including both.
N个火鸡 n:1~99
The total price originally consisted of five digits, but we can see only the three digits in the middle.
中间三个数字可见,一共5位数
We assume that the first digit is nonzero, that the price of one turkeys is an integer number of dollars, and that all the
turkeys cost the same price.
我们假设第一个数字非0,5位数是整数(非double!!!),火鸡单价一致
Given N, X, Y, and Z, write a program that guesses the two faded digits and the original price.
给你NXYZ,猜
In case that there is more than one candidate for the original price, the output should be the most expensive one.
输出最贵的单价
That is, the program is to report the two faded digits and the maximum price per turkey for the turkeys.
输入:
The first line of the input file contains an integer N (0<N<100), which represents the number of turkeys.
In the following line, there are the three decimal digits X, Y, and Z., separated by a space, of the original price $_XYZ_.
输出:
For each case, output the two faded digits and the maximum price per turkey for the turkeys.
样例输入:
72 6 7 9 5 2 3 7 78 0 0 5
样例输出:
3 2 511 9 5 18475 0
来源:
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
bool isInt(double x) {
int a = int(x);
double res = x - a;
if (res <= 0.000001) {
return true;
}
return false;
}
int main() {
int n;
int x, y , z;
while (cin >> n >> x >> y >> z) {
int maxPrice = 0;
double price, total;
int firstDigit, lastDigit;
for (int i = 1; i <= 9; i++) {
for (int j = 0; j <= 9; j++) {
total = i * 10000 + x * 1000 + y * 100 + z * 10 + j;
price = total / n;
if (isInt(price) && price > maxPrice) {
maxPrice = price;
firstDigit = i;
lastDigit = j;
}
}
}
if (maxPrice > 0) {
cout << firstDigit <<" " << lastDigit << " " << maxPrice << endl;
} else {
cout << 0 << endl;
}
}
return 0;
}