对一个八位数有三种操作: 加一、减一、反转 。 至少多少次操作可以把一个八位数A变成八位数B。
反转是指总体反转
#include<bits/stdc++.h> using namespace std; const int N = 6e5 + 5; void get(int a[], int v) { int cnt = 0; while(v) { a[++cnt] = v%10; v /= 10; } reverse(a + 1, a + cnt + 1); } int a[15], b[15]; int main() { #ifndef ONLINE_JUDGE freopen("b.txt", "r", stdin); #endif // ONLINE_JUDGE int A, B; scanf("%d %d", &A, &B); get(a, A); get(b, B); int a1 = 0, a2 = 0, b1 = 0, b2 = 0; for(int i = 4; i >= 1; i--) a1 = a1 * 10 + a[i]; for(int i = 4; i >= 1; i--) b1 = b1 * 10 + b[i]; for(int i = 5; i <= 8; i++) a2 = a2 * 10 + a[i]; for(int i = 5; i <= 8; i++) b2 = b2 * 10 + b[i]; // cout << a1 << " " << a2 << "\n"; // cout << b1 << " " << b2 << "\n"; int ans1 = abs(b1-a2) + abs(a1-b2) + 1; int ans2 = abs(a1-b1) + abs(a2-b2) + 2; if(a1 == b1) ans2 = abs(a2-b2); printf("%d\n", min(ans1, ans2)); return 0; }