Zoj 2971 give me the number; zoj 2311 inglish-number Translator (character processing, anti-air line, strstr)

Source: Internet
Author: User

Zoj 2971 give me the number question

Zoj 2311 inglish-number Translator

 

 

// The two questions are similar, and the details are a little different. Because they are not done together, the processing method is a little different-or the previous method is high-end.

 

 

// The AC code of zoj 2971 (using the strstr function ):

# Include <stdio. h> # include <string. h> char C [33] [10] = {"zero", "one", "two", "three", "four", "five", "Six ", "Seven", "eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen ", "Seventeen", "Eighteen", "Nineteen", "Twenty", "Thirty", "Forty", "Sixty", "Seventy", "Eighty ", "Ninety", "Hundred", "Thousand", "million", "and"}; char s [200], SSS [200]; int main () {int ne, sum, flag, I, j, ACC, Len; int tt; scanf ("% d", & TT); getchar (); While (TT --) {gets (s); Ne = 1; sum = 0; flag = 0; ACC = 0; Len = strlen (s); While (flag <Len) {While (s [flag] = '') Flag ++; // while (s [flag] = 'and') Flag ++; for (I = 31; i> = 0; I --) {If (strstr (& S [flag], C [I]) = & S [flag]) // locate C [I] in S. If yes, the return position is not found.-1 is returned, that is, false; & obtain the position {if (I <= 20) ACC + = I; else if (I <= 27) ACC + = (I-18) * 10; else if (I = 28) ACC * = 100; else if (I = 29) {sum + = ACC * 1000; ACC = 0 ;}else if (I = 30) {sum + = ACC * 1000000; ACC = 0;} else if (I = 31) Ne =-1; flag + = strlen (C [I]); break ;}}} printf ("% d \ n", (sum + ACC);} return 0 ;}
String converted

 

 

// The AC code of zoj 2311 (pay attention to this question,Anti-DDOS serviceThat is, if a row has no input, nothing should be output ):

// Simulate // convert an English sentence to an Arabic number. # Include <stdio. h> # include <string. h ># include <algorithm> using namespace STD; int main () {int Len, I, J, K, num, sign, ans; char STR [1010], word [20]; char W [33] [20] = {"negative", "zero", "one", "two", "three", "four ", "five", "Six", "Seven", "eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen ", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty", "Thirty", "Forty", "ty", "Sixty" "," Seventy "," Eighty "," Ninety "," Hundred "," Thousand "," million "}; while (gets (STR )) {If (strcmp (STR, "") = 0) continue; // air defense line Len = strlen (STR); STR [Len ++] = ''; num = 0; j = 0; Sign = 1; ans = 0; for (I = 0; I <Len; I ++) {If (STR [I]! = '') Word [J ++] = STR [I]; else {word [J ++] = '\ 0'; For (k = 0; k <32; k ++) {If (strcmp (W [K], word) = 0) {If (k = 0) Sign =-1; else if (k> = 1 & K <= 21) num + = K-1; else if (k> = 22 & K <= 28) num + = (K-19) * 10; else if (k = 29) num = num * 100; else if (k = 30) // consider 1000 {ans + = num * 1000; num = 0;} else if (k = 31) // and 1000000 can be {ans + = num * 1000000; num = 0;} break ;}} j = 0 ;}}ans + = num; printf ("% d \ n", ANS * sign);} return 0 ;}
View code

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.