Question
String processing
I don't know why WAF works. For more information, see the notes.
In addition, I use a line to read data and then use the string parse, Which is troublesome.
AvailableIstringstream or sscanf. It is better to format and read from the string.Reference: http://www.cnblogs.com/shixuehunk/archive/2011/07/22/2113728.html
Http://www.cppblog.com/rakerichard/archive/2011/04/07/143608.html
Code:
# Include <iostream> # include <cstdio> # include <cstring> # include <string> # include <iomanip> using namespace STD; Double P, I, U; double CAL (int p, int I, int U) {If (! U) return P/I; else if (! P) return u * I; else if (! I) Return P/U;} double s2d (string s) {double ret = 0.0; int ispre = 0; int isdot = 0; int dot = 1; for (INT I = 0; I <(INT) s. length (); I ++) {If (s [I] = 'M' | s [I] = 'K' | s [I] = 'M') {ispre = 1; break;} If (s [I] = '. ') {isdot = 1; continue;} If (isdot) Dot * = 10; ret = RET * 10 + s [I]-'0 ';} RET/= (double) Dot; If (ispre) {Switch (s [S. length ()-1]) {Case 'M': RET/= 1000; break; Case 'K': Ret * = 1000; break; Case'm ': Ret * = 1000000; break;} return ret;} int ISP, ISI, ISU; double solve (string s) {ISP = 0, ISI = 0, ISU = 0; int p1 = S. find ("u =", 0); int P2 = S. find ("I =", 0); int P3 = S. find ("P =", 0); If (P1! =-1) {ISU = 1; int P = S. find ('V', P1); string T = S. substr (P1 + 2, p-p1-2); U = s2d (t);} If (P2! =-1) {ISI = 1; int P = S. find ('A', P2); string T = S. substr (P2 + 2, p-p2-2); I = s2d (t);} If (P3! =-1) {ISP = 1; int P = S. find ('w', P3); string T = S. substr (P3 + 2, p-p3-2); P = s2d (t);} return CAL (ISP, ISI, ISU);} int main () {int T; cin> T; cin. get (); For (INT I = 1; I <= T; I ++) {string s; Getline (CIN, S ); cout <"problem #" <I <Endl;/* use cout to directly output the function return value, WA if (! ISP) cout <"p"; else if (! ISI) cout <"I"; else if (! ISU) cout <"U"; cout <"=" <setiosflags (IOs: fixed) <setprecision (2) <solve (s); If (! ISP) cout <"W"; else if (! ISI) cout <"A"; else if (! ISU) cout <"v"; cout <Endl; * // With cout, assign the return value of the function to ans before outputting ans, AC Double ans = solve (s); If (! ISP) cout <"p"; else if (! ISI) cout <"I"; else if (! ISU) cout <"U"; cout. SETF (IOs: fixed); cout. Precision (2); cout <"=" <ans; If (! ISP) cout <"W"; else if (! ISI) cout <"A"; else if (! ISU) cout <"v"; cout <Endl;/* uses printf AC. Double ans = solve (s); If (! ISP) printf ("P = %. 2fw \ n", ANS); else if (! ISI) printf ("I = %. 2fa \ n", ANS); else if (! ISU) printf ("u = %. 2fv \ n", ANS); printf ("\ n ");*/}}