题目1036:Old Bill

来源:互联网 发布:minitab比较多组数据 编辑:程序博客网 时间:2024/05/16 18:51
题目1036:Old Bill

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:3671

解决:2004

题目描述:

    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.

样例输入:
726 7 952 3 7780 0 5
样例输出:
3 2 5119 5 184750
来源:
2007年上海交通大学计算机研究生机试真题
答疑:

解题遇到问题?分享解题心得?讨论本题请访问:http://t.jobdu.com/thread-7760-1-1.html


思路:很简单其实。对最高位和最低位进行遍历即可

#include<iostream>#include<fstream>using namespace std;int main(){    ifstream in;    in.open("1.txt");    int n;    while(in>>n)    {        int x,y,z;        int first,last;        int total;        bool flag=false;        in>>x>>y>>z;        for(int i=9;i>=1 && flag==false;--i)        {            for(int j=9;j>=0 && flag==false;--j)            {                total=i*10000+x*1000+y*100+z*10+j;                if(total%n==0)                {                    flag=true;                    first=i;                    last=j;                    break;                }            }        }        if(flag==false)            cout<<"0"<<endl;        else cout<<first<<" "<<last<<" "<<total/n<<endl;    }    return 0;}


0 0
原创粉丝点击