10424 Love Calculator

Problem Link: 10424 – Love Calculator

 

 

#include<bits/stdc++.h>

using namespace std;

int digit(int n);

int main()
{
    char a[30],b[30];

    int alpha[128];

    for(int i='A',j=1;i<='Z';i++,j++)
        alpha[i]=j;

    for(int i='a',j=1;i<='z';i++,j++)
        alpha[i]=j;

    while(scanf(" %[^\n] %[^\n]",a,b)==2){

        int c=0,d=0,e;
        for(int i=0;a[i]!='\0';i++){
            if(isalpha(a[i])){
                e=a[i];
                c+=alpha[e];
            }
        }

        for(int i=0;b[i]!='\0';i++){
            if(isalpha(b[i])){
                e=b[i];
                d+=alpha[e];
            }
        }

        while(c>9)
            c=digit(c);

        while(d>9)
            d=digit(d);

        if(c>d)
            swap(c,d);

        double x=(double)c,y=(double)d;

        double z=(x/y)*100;

        cout << fixed << setprecision(2) << z << " %" << endl;

    }

    return 0;
}

int digit(int n){

    int sum=0;

    while(n!=0){
        sum+=n%10;
        n=n/10;
    }

    return sum;

}

Advertisements