# amasoudfam/Solved-Programming-Problems

Switch branches/tags
Nothing to show
c6fa436 Apr 6, 2018
1 contributor

### Users who have contributed to this file

executable file 45 lines (40 sloc) 1.11 KB
 /* * 8328 - Column Addition * ACM-ICPC Live Archive, Regionals 2017, Asia, Tehran * * Read Farsi solution guide: http://www.algorithmha.ir * */ #include #include using namespace std; int toDigit(char ch){ return ch - '0'; } int main() { int n; string a, b, c; while(true){ cin >> n; if(!n) break; cin >> a >> b >> c; int t1 = toDigit(a[n - 1]) + toDigit(b[n - 1]), t2 = toDigit(c[n - 1]); int matrix[n + 1][2]; matrix[n][0] = 0; matrix[n][1] = n; for(int i = n - 1; i > -1; i--){ t1 = toDigit(a[i]) + toDigit(b[i]); t2 = toDigit(c[i]); matrix[i][0] = min(matrix[i + 1][0] + 1, t1 == t2 ? matrix[i + 1][0] : (t1 + 1 == t2 ? matrix[i + 1][1] : n)); matrix[i][1] = min(matrix[i + 1][1] + 1, t1 > 9 && t1 - 10 == t2 ? matrix[i + 1][0]: (t1 > 8 && t1 - 9 == t2 ? matrix[i + 1][1] : n)); } cout << matrix[0][0] << endl; } return 0; }