725 – Division UVA Solution

This is an easy problem from uva judge https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=666

Complete search, brute force, pruning.

How I did it

  • I read N until I get 0.
  • With this formula abcde / fghij = N, I traverse from 01234 to 98765, I set fghij with these values and compute abcde = N * fghij;
  • If abcde is greater than 98765 I break the loop. No more operations required.
  • I check if abcde and fghij have the digits 0123456789.
  • I print the answer.
  • Accepted!!

Hints

  • The bounds are 01234 to 98765.
  • If abcde is greater than 98765 I break the loop. Otherwise TLE.
  • I print a blank line between the cases.(The last case does not need the blank line).

Code

/*
*Author: Fabian Calsina
*Date: 25/12/2016
*/
#include <bits/stdc++.h>
using namespace std;

bool checkValues(int a, int b){
   map<int,int> data;

   while(a != 0){
        data[a%10]++;
        a/=10;
   }

   while(b != 0){
        data[b%10]++;
        b/=10;
   }

   if(data.size() != 10)
        return false;

   for(map<int,int>::iterator it = data.begin(); it != data.end(); it++){
        if(it->second != 1)
            return false;
   }

   return true;
}

int main(){
    int abcde, fghij, N, flagOne, flagTwo;
    bool hasAnswer, spaceAnswer = false;

    while(scanf("%d", &N)){
        if(!N)
            break;

        if(!spaceAnswer)
            spaceAnswer = true;
        else
            printf("\n");

        hasAnswer = false;

        for(fghij=1234; fghij<=98765; fghij++){
            abcde = N * fghij;
            if(abcde > 98765)
                break;
            if(abcde < 10000)
                flagOne = abcde * 10;
            else
                flagOne = abcde;
            if(fghij < 10000)
                flagTwo = fghij * 10;
            else
                flagTwo = fghij;

            if(checkValues(flagOne, flagTwo)){
                if(abcde < 10000)
                    printf("0%d / %d = %d\n", abcde, fghij, N);
                else if(fghij < 10000)
                    printf("%d / 0%d = %d\n", abcde, fghij, N);
                else
                    printf("%d / %d = %d\n", abcde, fghij, N);
                hasAnswer = true;
            }

        }

        if(!hasAnswer)
            printf("There are no solutions for %d.\n", N);
    }
return 0;
}

13012 – Identifying tea UVA Solution

This a is an easy problem from uva judge https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=4900

  • Just compare the tea Id with the 5 inputs.

Hints

  • Read until EOF.

Code

/*
*Author: Fabian Calsina
*Date: 25/12/2016
*/
#include<stdio.h>

int main(){

    int x, teaType, input, answer;

    while( scanf("%d", &teaType) != EOF ){
        answer = 0;
        for( x=0; x<5; x++ ){
            scanf("%d", &input);
            if(input == teaType)
                answer++;
        }

        printf("%d\n", answer);
    }
return 0;
}