题解
本题目是可以使用N^2暴力飘过的 ij枚举两个数字注意题目要求不能改变顺序 使用add函数将两个整数拼接在一起求最大值即可
不能使用字符串拼接 使用整数时两个1e9拼接在一起会超过long long范围 使用unsigned long long存储
AC代码
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const int MAXN = 6e3 + 10;
int a[MAXN];
ull add(ull a, ull b) //将整数a加在整数b前面
{
ull t = b;
while (t) //根据b的位数给a乘上相应数的10
a *= 10, t /= 10;
return a + b;
}
int main()
{
#ifdef LOCAL
freopen("C:/input.txt", "r", stdin);
#endif
int T;
cin >> T;
for (int ti = 1; ti <= T; ti++)
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
ull ans = 0;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
ans = max(ans, add(a[i], a[j]));
printf("Case #%d: %llu\n", ti, ans);
}
return 0;
}

本文介绍了一种使用N^2暴力算法解决特定问题的方法,该问题要求在不改变数字顺序的情况下,从给定的整数数组中选择两个数,通过自定义的整数拼接函数来生成最大的可能值。文章提供了详细的AC代码实现,包括如何避免使用字符串拼接以及如何处理大数值的技巧。
491

被折叠的 条评论
为什么被折叠?



