Hướng dẫn giải của VM 12 Bài 03 - Thử máy
Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này
Code mẫu của flashmt
#include <iostream> #include <string> #include <cctype> #include <cmath> using namespace std; int checkType(string s) { int res = 0; for (int i = 0; i < s.size(); i++) if (s[i] >= '0' && s[i] <= '9') res |= 1; else if (tolower(s[i]) >= 'a' && tolower(s[i]) <= 'z') res |= 2; return res; } double getSum(string s) { double res = 0, x = 0, power = 1; s += ' '; for (int i = 0, dotted = 0, sign = 1; i < s.size(); i++) if (s[i] == ' ') res += x/power*sign, power = 1, x = 0, dotted = 0, sign = 1; else if (s[i] == '-') sign = -1; else if (s[i] == '.') dotted = 1; else x = x * 10 + s[i] - '0', power *= (dotted > 0? 10: 1); return res; } string concatStr(string s) { string res = ""; for (int i = 0; i < s.size(); i++) if (s[i] != ' ') res += s[i]; return res; } int main() { string s; while (getline(cin,s)) { if (s=="?") break; int type = checkType(s); if (type == 3) puts("Error!"); else if (type == 1) printf("%.6lf\n",getSum(s)); else cout << concatStr(s) << endl; } }
Code mẫu của ladpro98
const fi=''; var inp:text; s:string; i,l,code:longint; t,sum:extended; err:boolean; begin assign(inp,fi);reset(inp); while true do begin readln(inp,s); err:=false; while s[1]=' ' do delete(s,1,1); if s[1]='?' then break; if s[1] in ['A'..'z'] then begin for i:=1 to length(s) do if (s[i]<>' ') and (not (s[i] in ['A'..'z'])) then begin writeln('Error!'); err:=true; break; end; if err then continue; for i:=length(s) downto 1 do if s[i]=' ' then delete(s,i,1); writeln(s); continue; end; if (s[1]='.') or (s[1]='-') or (s[1] in ['0'..'9']) then begin sum:=0; for i:=length(s) downto 2 do if (s[i]=' ') and (s[i-1]=' ') then delete(s,i,1); if s[length(s)]<>' ' then s:=s+' '; l:=1; for i:=1 to length(s) do if s[i]=' ' then begin Val(copy(s,l,i-l),t,code); l:=i+1; if code<>0 then begin writeln('Error!'); err:=true; break; end; sum:=sum+t; end; if err then continue; writeln(sum:0:6); continue; end; writeln('Error!'); end; close(inp); end.
Code mẫu của RR
//#pragma comment(linker, "/STACK:66777216") #include <iomanip> #include <sstream> #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> #include <vector> #include <set> #include <map> #include <stack> #include <queue> #include <string> #include <deque> #include <complex> #include <bitset> #define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; ++i) #define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; --i) #define REP(i,a) for(int i=0,_a=(a); i<_a; ++i) #define ll long long #define F first #define S second #define PB push_back #define MP make_pair #define DEBUG(x) cout << #x << " = " << x << endl; #define PR(a,n) cout << #a << " = "; FOR(i,1,n) cout << a[i] << ' '; puts(""); #define PR0(a,n) cout << #a << " = "; REP(i,n) cout << a[i] << ' '; puts(""); using namespace std; const double PI = acos(-1.0); char s[100111]; bool allNum() { REP(i,strlen(s)) if (s[i] != ' ') if (s[i] != '-' && s[i] != '.') if (s[i] < '0' || s[i] > '9') return false; return true; } bool allChar() { REP(i,strlen(s)) if (s[i] != ' ') if (s[i] < 'a' || s[i] > 'z') if (s[i] < 'A' || s[i] > 'Z') return false; return true; } int main() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); while (gets(s)) { if (s[0] == '?') break; if (allNum()) { istringstream sin(s); long double res = 0.0, x; while (sin >> x) { res += x; } cout << (fixed) << setprecision(6) << res << endl; } else if (allChar()) { istringstream sin(s); string res = "", x; while (sin >> x) { res += x; } cout << res << endl; } else puts("Error!"); } return 0; }
Code mẫu của hieult
#include <set> #include <map> #include <cmath> #include <deque> #include <queue> #include <stack> #include <cstdio> #include <string> #include <vector> #include <cstdlib> #include <cstring> #include <sstream> #include <iostream> #include <algorithm> using namespace std; #define Rep(i,n) for(typeof(n) i = 0; i < (n); ++i) #define Repd(i,n) for(typeof(n) i = (n)-1; i >= 0; --i) #define For(i,a,b) for(typeof(b) i = (a); i <= (b); ++i) #define Ford(i,a,b) for(typeof(a) i = (a); i >= (b); --i) bool isword(string s) { int len = s.length(); Rep(i, len) if (!isalpha(s[i])) return false; return true; } bool isnumber(string s) { int minus = 0, dot = 0, posminus = -1, posdot = -1; int len = s.length(); Rep(i, len) { if (s[i] == '-') { ++minus; posminus = i; } else if (s[i] == '.') { ++dot; posdot = i; } else if (!isdigit(s[i])) return false; } if (minus > 1 || dot > 1) return false; if (minus == 1 && posminus != 0) return false; return true; } double convert(string s) { stringstream ss; ss << s; double r; ss >> r; return r; } string line, s; vector<string> a; void process() { int n = a.size(); if (n == 0) { puts("Error!"); return; } double sum = 0; string cat = ""; bool allnumber = true, allword = true; Rep(i, n) { if (isnumber(a[i])) { sum += convert(a[i]); allword = false; } else if (isword(a[i])) { cat = cat + a[i]; allnumber = false; } else { puts("Error!"); return; } } if (allword) cout << cat << endl; else if (allnumber) printf("%.6lf\n", sum); else puts("Error!"); } int main() { while (getline(cin, line) && line != "?") { a.clear(); istringstream iss(line); while (iss >> s) a.push_back(s); process(); } return 0; }
Code mẫu của skyvn97
program test; uses crt; var s,sb,t:string; a,r:real; b,ck:integer; i,j:integer; fst:boolean; num:boolean; str:boolean; err:boolean; function ckstr(s:string):integer; var i:integer; begin b:=0; val(s,a,b); if (b<>0) then begin for i:=1 to length(s) do begin if ord('A')>ord(s[i]) then begin ckstr:=-1; exit; end; if ord('z')<ord(s[i]) then begin ckstr:=-1; exit; end; if (ord('Z')<ord(s[i])) and (ord('a')>ord(s[i])) then begin ckstr:=-1; exit; end; end; ckstr:=1; exit; end else begin ckstr:=0; exit; end; end; begin repeat readln(s); if (length(s)<>1) or (s[1]<>'?') then begin while (pos(' ',s)<>0) do delete(s,pos(' ',s),1); fst:=true; err:=false; str:=false; num:=false; r:=0; t:=''; if s[1]<>' ' then s:=' '+s; if s[length(s)]<>' ' then s:=s+' '; while pos(' ',s)<>length(s) do begin i:=pos(' ',s); for j:=i+1 to length(s) do if s[j]=' ' then break; sb:=copy(s,i+1,j-i-1); ck:=ckstr(sb); if ck<0 then begin err:=true; break; end; if ck=0 then begin if fst then begin fst:=false; num:=true; r:=a; end else begin if str then err:=true else r:=r+a; end; end; if ck>0 then begin if fst then begin fst:=false; str:=true; t:=sb; end else begin if num then err:=true else t:=t+sb; end; end; if err then break; delete(s,i,j-i); end; if err then begin writeln('Error!'); continue; end; if num then begin writeln(r:0:6); continue; end; if str then begin writeln(t); continue; end; end else halt; until false; end.
Bình luận