Monday, 8 February 2016

Uva - 10424 - Love Calculator Problem Solution.

One day I asked Saima that how much she loves me. Her answer was “71.43%”. I was surprised as well as shocked by her answer. I could not understand why she didn’t tell 100% and why she told a particular and peculiar fraction like 71.43. Looking at my surprised, shocked and nervous face she burst out laughing and told that she loves me more than any thing in this universe and it was nothing but a silly and funny love calculation. Then she described me the calculation. In this problem you will have to write a program so that any one can calculate love between any two persons very quickly (of course a very silly game).
Rules:
You will be given two names. These two names can have white space or some other non-alphabetical characters like $ & % etc. But only the alphabets from a to z or A to Z will participate in love calculation. Each alphabet has a particular value. The values are from 1 to 26 in ascending order of the alphabets. It’s like this, a = 1, b = 2, c = 3, ..., z = 26. Both upper case and lower case holds the same values. Then make the sum of these numbers until it comes in one digit. [For example, consider a name ‘bcz’. Here, b = 2, c = 3 and z = 26. So, the sum is (2+3+26) = 31 = (3+1) = 4.] Then the ratio of these two numbers in percentage will be the result.

Remember: Result can not be more than 100% . Take the ratio carefully to avoid this problem.

Input
Your input will be two names. Each name holds not more than 25 characters. End of file will indicate the end of input.

Output
For each pair of names your program will have to calculate the love between those two persons and give the result as output. In result two digits to be displayed after the decimal point. All the results must be in new lines.

Sample Input
saima
shanto
Pakistan
India
USA
USSR

Sample Output 
71.43 %
100.00 %
100.00 %

Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
char low(char ch){
    if(ch >= 'A' && ch <= 'Z')return ch+=32;
    return ch;
}
int main(){

    string s1,s2;
    int ar[26];
    for(int i = 1; i <= 26; i++)ar[i-1] = i;
    while(getline(cin,s1)){
        getline(cin,s2);
        int sum1 = 0,sum2 = 0,len = s1.length();
        for(int i = 0; i < len; i++){
            if((s1[i] >= 'A' && s1[i] <= 'Z') || (s1[i] >= 'a' && s1[i] <= 'z'))sum1 += ar[low(s1[i])-'a'];
        }
        len = s2.length();
        for(int i = 0; i < len; i++){
            if((s2[i] >= 'A' && s2[i] <= 'Z') || (s2[i] >= 'a' && s2[i] <= 'z'))sum2 += ar[low(s2[i])-'a'];
        }

        int tmps = 0,tmpdig1 = 0,tmpdig2 = 0;
        while(sum1 != 0){
            tmpdig1+= sum1 % 10;
            sum1 /= 10;
        }
        if(tmpdig1 > 9){
            tmps = tmpdig1;
            tmpdig1 = 0;
            while(tmps != 0){
                tmpdig1 += tmps%10;
                tmps/=10;
            }
        }
        while(sum2 != 0){
            tmpdig2 += sum2 % 10;
            sum2 /= 10;
        }
        if(tmpdig2 > 9){
            tmps = tmpdig2;
            tmpdig2 = 0;
            while(tmps != 0){
                tmpdig2 += tmps % 10;
                tmps /= 10;
            }
        }

        if(tmpdig1 > tmpdig2)printf("%0.2f %\n",((float)tmpdig2*100)/(float)tmpdig1 );
        else printf("%0.2f %\n",((float)tmpdig1*100)/(float)tmpdig2);
    }
    return 0;
}

No comments:

Post a Comment