1. 程式人生 > >ZOJ.2679 Old Bill【水】 2015/10/12

ZOJ.2679 Old Bill【水】 2015/10/12

Old Bill
Time Limit: 2 Seconds      Memory Limit: 65536 KB

Among grandfather��s papers a bill was found:

72 turkeys $_679_

The first and the last digits of the number that obviously represented thetotal price of those turkeys are replaced here by blanks (denoted _), forthey are faded and are now illegible. What are the two faded digits andwhat was the price of one turkey?

We want to write a program that solves a general version of the aboveproblem:

N turkeys $_XYZ_

The total number of turkeys, N, is between 1 and 99, including both. Thetotal price originally consisted of five digits, but we can see only the threedigits in the middle. We assume that the first digit is nonzero, that theprice of one turkey is an integer number of dollars, and that all the turkeyscost the same price.

Given N, X, Y , and Z, write a program that guesses the two faded digitsand the original price. In case that there is more than one candidate forthe 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 perturkey for the turkeys.

Input

The input consists of T test cases. The number of test cases (T) is givenon the first line of the input file. The first line of each test case contains aninteger N (0 < N < 100), which represents the number of turkeys. In thefollowing line, there are the three decimal digits X, Y , and Z, separatedby a space, of the original price $_XYZ_.

Output

For each test case, your program has to do the following. For a test case,there may be more than one candidate for the original price or there isnone. In the latter case your program is to report 0. Otherwise, if thereis more than one candidate for the original price, the program is to reportthe two faded digits and the maximum price per turkey for the turkeys.The following shows sample input and output for three test cases.

Sample Input

3
72
6 7 9
5
2 3 7
78
0 0 5

Sample Output

3 2 511
9 5 18475
0
Source: Asia 2003, Seoul (South Korea)

題意弄懂了就好做了,比較水,就是求一個最大五位數能整除n的,兩層for迴圈,注意第一層不能等於0,會WA

#include<iostream>
#include<cstdio>

using namespace std;

int main(){
    int n,x,y,z;
    int t;
    cin>>t;
    while(t--){
        cin>>n;
        cin>>x>>y>>z;
        bool flag = false;
        for( int i = 9 ; i > 0 ; --i ){
            for( int j = 9 ; j >= 0 ; --j ){
                int ret = i * 10000 + x*1000 + y*100 + z*10 + j;
                if( ret % n == 0 ){
                    printf("%d %d %d\n",i,j,ret/n);
                    flag = true;
                    break;
                }
            }
            if( flag ) break;
        }
        if( !flag ) printf("0\n");
    }
    return 0;
}