TJ
2024-08-17 14:13:45
发布于:广东
6阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
string fun(string a, string b){
string res = "";
// 1、判断a和b的大小, 如果a小于b,交换a和b,结果加上'-'
if (a.size() < b.size() || a.size() == b.size() && a < b){
swap(a, b);
res += '-';
}
//2、逆序存储
int la = a.size(), lb = b.size();
vector<int> A(la+10), B(la+10), C(la+10);
for (int i=0;i<la;i++) A[i] = a[la-i-1]-'0';
for (int i=0;i<lb;i++) B[i] = b[lb-i-1]-'0';
//3、逐位相减
for (int i = 0; i < la; i++){
C[i] += A[i] - B[i];
if (C[i] < 0){//需要借位
C[i+1]--;//高位借1
C[i]+=10;
}
}
//4、处理前导0
int len = la-1;
while (C[len]==0&&len>0) len--;
//5、逆序存入res
for (int i=len;i>=0;i--){
res += C[i]+'0';
}
return res;
}
int main(){
string a, b;
cin >> a >> b;
cout << fun(a, b);
return 0;
}
全部评论 1
#include<bits/stdc++.h> using namespace std; long long n, m, a[105][105]; int main() { cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { cin >> a[i][j]; } } for (int i = n - 1; i >= 1; i--) { for (int j = 1; j <= i; j++) { a[i][j] += max(a[i + 1][j], a[i + 1][j + 1]); } } cout << a[1][1] << endl; return 0; }
2024-08-17 来自 广东
0
有帮助,赞一个