View Code of Problem 16

#include<bits/stdc++.h>
using namespace std;
stack<int>st;
int main() {
	int t;
	string word[10]= {"zero","one","two","three","four","five","six","seven","eight","nine"};
	cin>>t;
	while(t--) {
		int flag,flag_=0;
		int a=0,b=0;
		cin>>flag;
		string s;
		while(cin>>s&&s!="=") {
			if(s=="+")
				flag_=1;
			else {
				int d;
				if(!flag_) {
					d=0;
					for(d; d<10; ++d) {
						if(word[d]==s)
							break;
					}
					if(d==10)
						a = stoi(s.c_str());
					else
						a=a*10+d;
				} else {
					d=0;
					for(d; d<10; ++d) {
						if(word[d]==s)
							break;
					}
					if(d==10)
						b= stoi(s.c_str());
					else
						b=b*10+d;
				}
			}
		}
		int ans=a+b;
		if(flag) {
			while(ans!=0) {
				st.push(ans%10);
				ans/=10;
			}
			cout<<word[st.top()];
			st.pop();
			while(!st.empty()){
				cout<<" "<<word[st.top()];
				st.pop();
			}
			cout<<endl;
		} else
			cout<<a+b<<endl;
	}

	return 0;
}

Double click to view unformatted code.


Back to problem 16