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? We want to write a program that solves a general version of the above problem. N turkeys $XYZ The total number of turkeys, N, is between 1 and 99, including both. The total price originally consisted of five digits, but we can see only the three digits in the middle. 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. Given N, X, Y, and Z, write a program that guesses the two faded digits and the original price. 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.
示例1
输入
72
6 7 9
5
2 3 7
78
0 0 5
输出
3 2 511
9 5 18475
0
题解
首先明确题意,火鸡总价格total是ixyzj(xyz用户输入,ij是临时变量),共有n只火鸡,求MAX(单只火鸡最高整数价格),核心思想是total=10000i+1000x+100y+10z+j,设置bool:find来筛选”整数“,在for循环中比较total/n是否比上个MAX更大
C++11
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
using namespace std;
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
//在这个模板的同一级目录下建一个'1.txt'的文件
//然后把输入全部放进去保存,之后运行代码完全不用输入任何数据
#endif
int n,x,y,z;
while (cin>>n){
bool find=false;
int first,last,total,MAX=-9999;
cin>>x>>y>>z;
int temp=1000*x+100*y+10*z;
for(int i =1;i<=9;i++){
for(int j=0;j<=9;j++){
total=10000*i+temp+j;
if (total%n==0){
find=true;
if (total/n>MAX){
MAX=total/n;
first=i;
last=j;
}
}
}
}
if (!find){
cout<<0<<endl;
}
else{
cout<<first<<" "<<last<<" "<<MAX<<endl;
}
}
return 0;
}