Hardware Store | CodeChef Puzzle in C++

Problem

Aman has a hardware store in Una. One day when he went down to the store room to fetch a box of nails. He found that the price tag had faded out. The first and the last digits of the price were not readable. Although, the number of nails in the box could be read clearly. The box read,
72 nails, Rs. _679_
What were the two faded digits, and what was the price of one nail?

CodeChef Problem Link

Input

The input consists of T test cases. The number of test cases ( T ) is given on the first line of the input file. The first line of each test case contains an integer N , which represents the number of nails. In the following line, there are the three decimal digits X, Y, and Z, separated by 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 is none. In the latter case your program is to report 0. The program is to report the two faded digits. 

Constraints
  • 0 < N < 100
Sample Input
3
72
6 7 9
5
2 3 7
78
0 0 5
Sample Output
3 2 511
9 5 18475
0

Code Implementation

//
//  main.cpp
//  Hardware Store
//
//  Created by Himanshu on 20/02/22.
//

#include <iostream>
using namespace std;

long solve (long num, int n) {
    
    int i = 9, j = 9, flag = 0;
    long ans = 0;
    
    while (j>0) {
        i = 9;
        
        while (i > -1) {
            ans = num + j*10000 + i;
            
            if (ans % n == 0) {
                    flag = 1;
                    break;
            }

             i--;
        }

        j--;
        
        if (flag) {
            return ans;
        }

     }

     return 0;
}

int main() {
    int T, n;
    long x, y, z, a, b, p;
    cin>>T;
    
    while (T--) {
        cin>>n;
        cin>>x>>y>>z;
        
        long num = x*1000+y*100+z*10;
        num = solve(num,n);
        
        a = num/10000;
        b = num %10;
        p = num/n;
        
        if (num != 0) {
            printf("%ld %ld %ld\n",a,b,p);
        } else {
            printf("%d\n",0);
        }
    }
    
    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *