题目思路
简单分析找规律
p最小必为1,因而当A[i]==B[i]时默认取p[i] = 1;其余情况下,必须使得A和B不同的部分差,A更大
- cntA存储A[i] = 1,B[i] = 0的个数 cntB存储B[i] = 1,A[i] = 0的个数
1. 当A和B完全相同时,不可能存在解
2. 当A[i]和B[i]不一样时,且cntA == 0无解
3. 当cntA > cntB时,默认情况即可成立,max(p) = 1
4. 当cntA <= cntB时,应使得最后的A*p > B*p,即max(p) = cntB / cntA + 1
C++ 代码
#include<iostream>
#include<cstring>
using namespace std;
const int N = 110;
int A[N], B[N];
int n;
int main()
{
scanf("%d", &n);
int cntA = 0, cntB = 0;
for (int i = 0; i < n; i ++ ) scanf("%d", &A[i]);
for (int i = 0; i < n; i ++ ) scanf("%d", &B[i]);
if (memcmp(A, B, sizeof A) == 0)
{
printf("-1");
return 0;
}
for(int i = 0; i < n; i ++ )
{
if(A[i] != B[i])
if(A[i] == 1) cntA++;
else cntB++;
}
// printf("cntA is %d cntB is %d\n", cntA, cntB);
if(cntA == 0)
{
printf("-1");
return 0;
}
if(cntA <= cntB) printf("%d", cntB / cntA + 1);
else printf("1");
return 0;
}