POJ 1472

xiaoxiao2021-02-28  23

#include <iostream> #include <cstdio> #include <string> #include <sstream> #include <cstring> using namespace std; int string_to_int(const string &string1) { istringstream iss(string1); int a; iss>>a; return a; } bool solve(int *a) { string s; cin >> s; if (s[0] == 'B') { while (solve(a)); return true; } else if (s[0] == 'L') { string t; cin >> t; int temp[11] = {0}; while (solve(temp)); if (t == "n") { for (int i = 9; i >= 0; --i) { temp[i + 1] = temp[i]; } temp[0] = 0; } else { for (int i = 0; i < 11; ++i) { temp[i] *= string_to_int(t); } } for (int i = 0; i < 11; ++i) { a[i] += temp[i]; } return true; } else if (s[0] == 'O') { int num; cin >> num; a[0] += num; return solve(a); } else if (s[0] == 'E') return false; } void display(int * exp){ bool isfirst = true; bool has = false; for (int i = 10 ;i >=1 ;i--){ if (exp[i]){ has = true; if (isfirst){ if (exp[i]>1) cout<<exp[i]<<"*"; if (i>1) cout<<"n^"<<i; else cout<<"n"; isfirst = false; } else{ cout<<"+"; if (exp[i]>1) cout<<exp[i]<<"*"; if (i>1) cout<<"n^"<<i; else cout<<"n"; } } } if (exp[0]){ if (!isfirst){ cout<<"+"<<exp[0]; } else{ cout<<exp[0]; } } else{ if (!has) cout<<0; } } int main() { // freopen("../in.txt", "r", stdin); int coff[11] = {0}; int testcase; cin>>testcase; for (int t = 1; t <= testcase; ++t) { memset(coff, 0, sizeof(coff)); solve(coff); printf("Program #%d\n" "Runtime = ",t); display(coff); cout<<endl<<endl; } }

Reference

https://blog.csdn.net/lyy289065406/article/details/6648640

转载请注明原文地址: https://www.6miu.com/read-2602900.html

最新回复(0)