Wednesday, 11 February 2015

Uva 11530 - SMS Typing Solution

/* Uva 11530 - SMS Typing Solution */

Solution:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int T;
    char c;
    scanf("%d%c",&T,&c);
    for(int t = 0; t < T;t++){

        char s[200];
        gets(s);
        int len = strlen(s);
        int i,ans = 0;
        for(i = 0; i < len; i++){
            if(s[i]=='a'||s[i]=='d'||s[i]=='g'||s[i]=='j'||s[i]=='m'||s[i]=='p'||s[i]=='t'||s[i]=='w'||s[i]==' '){
                ans+=1;
            }
            else if(s[i]=='b'||s[i]=='e'||s[i]=='h'||s[i]=='k'||s[i]=='n'||s[i]=='q'||s[i]=='u'||s[i]=='x'){
                ans+=2;
            }
            else if(s[i]=='c'||s[i]=='f'||s[i]=='i'||s[i]=='l'||s[i]=='o'||s[i]=='r'||s[i]=='v'||s[i]=='y'){
                ans+=3;
            }
            else if(s[i]=='s'||s[i]=='z'){
                ans+=4;
            }
        }
        cout << "Case #" << t+1 << ": " << ans << endl;
    }
    return 0;
}


Solution 2:


#include <bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/

int main() {
    int T;
    cin >> T; cin.ignore();
    for(int t = 1; t <= T; t++){
        string str;
        getline(cin,str);
        int val = 0,len = str.length();
        for(int i = 0; i < len; i++){
            if(str[i] == 'a' || str[i] == 'b' || str[i] == 'c')val+= (str[i] - 'a')+1;
            else if(str[i] == 'd' || str[i] == 'e' || str[i] == 'f')val+= (str[i] - 'd')+1;
            else if(str[i] == 'g' || str[i] == 'h' || str[i] == 'i')val+= (str[i] - 'g')+1;
            else if(str[i] == 'j' || str[i] == 'k' || str[i] == 'l')val+= (str[i] - 'j')+1;
            else if(str[i] == 'm' || str[i] == 'n' || str[i] == 'o')val+= (str[i] - 'm')+1;
            else if(str[i] == 'p' || str[i] == 'q' || str[i] == 'r' || str[i] == 's')val+= (str[i] - 'p')+1;
            else if(str[i] == 't' || str[i] == 'u' || str[i] == 'v')val+= (str[i] - 't')+1;
            else if(str[i] == 'w' || str[i] == 'x' || str[i] == 'y' || str[i] == 'z')val+= (str[i] - 'w')+1;
            else if(str[i] == ' ')val+=1;
        }
        cout << "Case #" << t << ": " << val << endl;
    }
    return 0;
}

No comments:

Post a Comment