提交时间:2023-10-10 13:14:40
运行 ID: 106174
#include <bits/stdc++.h> using namespace std; bool jw[1145]; int a[1145],b[1145],ans[1145],len1,len2,n; int comp(string x, string y)//比较大小 { if(x.length()>y.length())return 1; if(x.length()<y.length())return 0; else { for(int i = 0; i < x.length(); i++) { if(x[i]>y[i]) { return 1; } if(x[i]<y[i]) { return 0; } } } return 2; } void init(string aa, string bb) { int lhcsb = comp(aa,bb); len1 = aa.length();len2 = bb.length(); if(len2<len1) { string tmp=""; for(int i = 1; i <= len1-len2; i++)tmp+='0'; bb = tmp+bb; } len2 = n = len1; for(int i = 1; i <= len1; i++)a[i] = aa[len1-i]-'0'; for(int i = 1; i <= len2; i++)b[i] = bb[len2-i]-'0'; } void lcxsb(string maxx, string minx) { init(maxx,minx); for(int i = 1; i <= n; i++) { a[i]-=jw[i]; if(a[i]<0) { a[i] = 9; jw[i+1] = 1; } if(a[i]>=b[i])//算 { ans[i] = a[i]-b[i]; } else { ans[i] = a[i]+10-b[i]; jw[i+1] = 1; } } bool flag = false; for(int i = n; i >= 1; i--) { if(ans[i]&&!flag) { flag = true; } if(flag) { cout << ans[i]; } } if(!flag) { cout << 0; } } signed main() { int n,lhcsb; string czx; string maxx="0",minx="99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999"; cin >> n; for(int i = 1; i <= n; i++) { cin >> czx; lhcsb = comp(czx,maxx); if(lhcsb == 1)maxx = czx; lhcsb = comp(czx,minx); if(lhcsb == 0)minx = czx; } lcxsb(maxx,minx); return 0; }