[ create a new paste ] login | about

Link: http://codepad.org/TEV4utVx    [ raw code | output | fork ]

C++, pasted on Jan 10:
map<long int, string> CharNumberValue ;

int main()
{
	CharNumberValue[0] ="zero";
	CharNumberValue[1] ="one";
	CharNumberValue[2] ="two";
	CharNumberValue[3] ="three";
	CharNumberValue[4] ="four";
	CharNumberValue[5] ="five";
	CharNumberValue[6] ="six";
	CharNumberValue[7] ="seven";
	CharNumberValue[8] ="eight";
	CharNumberValue[9] ="nine";
	CharNumberValue[10] ="ten";
	CharNumberValue[11] ="eleven";
	CharNumberValue[12] ="twelve";
	CharNumberValue[13] ="thirteen";
	CharNumberValue[14] ="fourteen";
	CharNumberValue[15] ="fifteen";
	CharNumberValue[16] ="sixteen";
	CharNumberValue[17] ="seventeen";
	CharNumberValue[18] ="eighteen";
	CharNumberValue[19] ="nineteen";
	CharNumberValue[20] ="twenty";

	CharNumberValue[30] ="thirty";
	CharNumberValue[40] ="forty";
	CharNumberValue[50] ="fifty";
	CharNumberValue[60] ="sixty";
	CharNumberValue[70] ="seventy";
	CharNumberValue[80] ="eighty";
	CharNumberValue[90] ="ninety";
	CharNumberValue[100] ="hundred";//Sameer,remember 100 is one hundred
	CharNumberValue[1000] ="thousand";//Sameer,remember 1000 is one thousand

    long int count = 0; 


	for(int i=1; i<= 1000; i++)
	{
	
		if(i<=100)
		{
			int ten = (i/10)*10;
			int unit = i%10;	
			map<long int, string>:: iterator it = CharNumberValue.find(i);
			map<long int, string>:: iterator it1 = CharNumberValue.find(unit);
			map<long int, string>:: iterator it2 = CharNumberValue.find(ten);
			
			if(i<10)
			{
				count  += it1->second.length();
			}
			if(i>=10 && i<=20)
			{
				count  += it->second.length();//These are unique
			}
			if(i>20 && i<=100) 
			{
				count  += it1->second.length();
				count  += it2->second.length();
			}			
			
		}
		if(i>100 && i<1000)
		{
			int hun = i/100;
			int ten=i%100;
			int mten = (ten/10)*10;//modified ten
			int unit = ten%10;

			map<long int, string>:: iterator it = CharNumberValue.find(unit);
			map<long int, string>:: iterator it1 = CharNumberValue.find(mten);			
			map<long int, string>:: iterator it2 = CharNumberValue.find(ten);
			map<long int, string>:: iterator it3 = CharNumberValue.find(hun);			
			int counttemp = CharNumberValue[100].length();
			count  += it3->second.length() + CharNumberValue[100].length() + 3 /*for and*/ ;
			if( ten<=20)
			{
				count  += it2->second.length();//These are unique
			}
			if(ten>20 && ten<=99) 
			{
				count  += it1->second.length();			 
				count  += it3->second.length();
			}

			
			
		}
	}
	cout<< count;

	return 0;
}


Output:
1
21450


Create a new paste based on this one


Comments: