本文主要是介绍E - Easy Arithmetic Gym - 100801E,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题意:
添加加减号使得其结果最大
思路:
只需要修改负数(带减号的数),第一位输出,后面为0则直接输出0,否则放上加号退出。
ACNEW
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <string>
#include <iostream>
#include <cmath>using namespace std;
typedef long long ll;const int maxn = 1e3 + 7;
char s[maxn];int main() {freopen("easy.in","r",stdin);freopen("easy.out", "w", stdout);scanf("%s",s + 1);int n = strlen(s + 1);for(int i = 1;i <= n;i++) {printf("%c",s[i]);if(s[i] == '-') {i++;printf("%c",s[i]);i++;if(s[i] == '-' || s[i] == '+' || i == n + 1) {i--;continue;}while(s[i] == '0' && i != n) {printf("+%c",s[i]);i++;}if(s[i] == '-' || s[i] == '+' || i == n + 1) {i--;continue;}else {printf("+%c",s[i]);}}}printf("\n");return 0;
}
//1+1-10+10-100-333
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>using namespace std;typedef long long ll;char s[1005];
vector<int>a,sign;int b[1005],cnt,n;void solve() {scanf("%s",s + 1);n = strlen(s + 1);s[n + 1] = '+';int sta = 1;int flag = 1;if(s[1] == '+') {sta++;flag = 1;}else if(s[1] == '-'){sta++;flag = -1;}for(int i = sta;i <= n;i++) {if(flag == 1) {printf("+");while(s[i] != '-' && s[i] != '+') {printf("%c",s[i]);i++;}}else if(flag == -1) {printf("-");printf("%c",s[i]);i++;int ok = 0;while(s[i] != '-' && s[i] != '+') {if(ok) {printf("%c",s[i]);}else if(s[i] == '0'){printf("+0");}else {ok = 1;printf("+%c",s[i]);}i++;}}if(s[i] == '-') flag = -1;else flag = 1;}
}int main() {freopen("easy.in","r",stdin);freopen("easy.out","w",stdout);solve();return 0;
}
这篇关于E - Easy Arithmetic Gym - 100801E的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!