2016 ACM-ICPC Asia Regional A – Best Matched Pair(打表)

来源:互联网 发布:PHP zend val 编辑:程序博客网 时间:2024/06/03 13:49
A – Best Matched Pair

给出n个数字,任意两个数字可以乘积,求乘积最大并且乘积结果是连续递增的数字的一个解。例如1234是连续递增的数字,但135不是。


思路:将所有连续递增的数打表,再暴力匹配即可。时间复杂度O(n^2)。


代码:

#include <bits/stdc++.h>using namespace std;typedef long long LL;const int maxn =  1005;int a[maxn];set < int > cnt; void init(){for(int len = 1; len <= 8; len++){for(int a = 1; a + len - 1 <= 9; a++){int b = a;int num = 0;for(int i = 1; i <= len; i++){num = num * 10 + b;b++;}cnt.insert(num);}}} int main(){init();int n;while(scanf("%d", &n) != EOF){for(int i = 1; i <= n; i++){scanf("%d", &a[i]);}int ans = -1;for(int i = 1; i <= n; i++){for(int j = i + 1; j <= n; j++){if(cnt.count(a[i] * a[j])){ans = max(ans, a[i] * a[j]);}}}printf("%d\n", ans);}return 0;}

阅读全文
1 0