当前位置: 代码迷 >> 综合 >> 【c/c++】Old Bill
  详细解决方案

【c/c++】Old Bill

热度:60   发布时间:2023-12-29 16:00:00.0

题目链接

题目描述

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.

在祖父的文件中发现了一张账单。72只火鸡$_679_,显然代表这些火鸡的总价格的数字的第一个和最后一个数字在这里被空白代替(表示_),因为它们已经褪色,难以辨认。那两个褪色的数字是什么?一只火鸡的价格是多少?我们打算编写一个程序来解决一般情况下的上述问题。N只火鸡$XYZ,火鸡的总数N介于1和99之间。总价原本是五位数字,但我们只看到中间三位。我们假设第一个数字是非零,一只火鸡的价格是美元的整数,所有火鸡的价格都是一样的。给定N, X, Y和Z,编写一个程序来猜测这两个褪色的数字和原始价格。如果有一个以上的候选原始价格,输出最昂贵的。也就是说,该程序将报告这两个褪色的数字和每只火鸡的最高价格。

输入描述

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_.

输入的第一行包含一个整数N (0<N<100),代表火鸡的数量。下一行是原始价格$_XYZ_的三位十进制数字X、Y和Z,用空格分隔。

输出描述

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

思路

循环遍历两个数字找到可能的整数结果,因为取最贵的原始价格,直接从9倒序遍历到出现结果即可。

代码

#include<bits/stdc++.h>int main(){
    int num;int x,y,z;int sum;while(scanf("%d\n%d %d %d",&num,&x,&y,&z)!=EOF){
    int a,b,price=0; //账单不见的两位数字 单价for(int i=9;i>0;i--){
    for(int j=9;j>=0;j--){
    sum = i*10000+x*1000+y*100+z*10+j;if(sum%num==0){
    a = i;b = j;price = sum/num;break;}//if}//forif(price){
    printf("%d %d %d\n",a,b,price);break;}}//forif(!price){
    printf("0\n");}}//while return 0;
}