ARC039-A A - B problem|AtCoder
A: A - B problem - AtCoder Regular Contest 039 | AtCoderを解きました。
#include <bits/stdc++.h> using namespace std; int main() { string a,b,temp1,temp2; int n{3}; int MIN = std::numeric_limits<int>::min(); cin>>a>>b; string tp1=a; string tp2=b; for(int i=0; i<n; i++){ temp1=a; temp2=b; temp1 = a.replace(i,1,"9"); temp2 = i==0?b.replace(i,1,"1"):b.replace(i,1,"0"); MIN=max(MIN,stoi(temp1)-stoi(tp2)); MIN=max(MIN,stoi(tp1)-stoi(temp2)); a=tp1; b=tp2; } cout << MIN << endl; return 0; }
何故かつらかった...。
1回目の提出で場合分けを書きすぎて全てのパターンに対応出来なかった。
1回全部コードを消して、考え直した。
全部の計算パターンを試してmax取って最大値を更新していくだけ。
std::numeric_limits
それと、replace()(置換)を初めて使いました!
最近覚えた i==0?b.replace(i,1,"1"):b.replace(i,1,"0") も便利!
ARC-A問題も残すとこあと僅か...!
3月中に全部解くと目標を立てたけど...どうだろう(?)