|
|
back to board#include<iostream> #include<string> using namespace std; string Convert(string c) { unsigned int i; string number=""; for( i=0;i<c.size();i++) { if(c[i]=='i'||c[i]=='j') number=number+"1"; else if (c[i]=='a'||c[i]=='b'||c[i]=='c') number=number+"2"; else if(c[i]=='d'||c[i]=='e'||c[i]=='f') number=number+"3"; else if(c[i]=='g'||c[i]=='h') number=number+"4"; else if(c[i]=='k'||c[i]=='l') number=number+"5"; else if(c[i]=='m'||c[i]=='n') number=number+"6"; else if(c[i]=='p'||c[i]=='r'||c[i]=='s') number=number+"7"; else if(c[i]=='t'||c[i]=='u'||c[i]=='v') number=number+"8"; else if(c[i]=='w'||c[i]=='x'||c[i]=='y') number=number+"9"; else if(c[i]=='o'||c[i]=='q'||c[i]=='z') number=number+"0"; } return number; } int main() { string input_number = "0"; int no_words; int counter = 0; while(input_number != "-1") { string input_number; cin >> input_number; if(input_number == "-1") break; cin>>no_words; string *words; words = new string[no_words]; for (int i=0;i<no_words;i++) { cin>>words[i]; } string *converted_words; converted_words= new string [no_words]; for (int i=0;i<no_words;i++) converted_words[i]=Convert(words[i]); for (int i=0;i<no_words;i++) { if(converted_words[i] == input_number) { cout<< words[i] << " "; counter ++; break; } } if(counter == 0) { cout<<"no solution"; break; } } return 0; } could someone please point out the foolishness. this I think: cout<<"no solution"; cout<<"No solution"; actually should be "No solution." remember the period. May be you should decrease the counter. counter--; as you are trying to compare counter==0; Another issue: Major one, Your code is literally comparing the converted numbers directly to the input number. If no number is matched you have to check all the possible permutations of the converted number array. The question is asking minimum number of words required to match the number. (GOT IT?) |
|
|