循环遍历数组即可,注意临界条件的判断。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 102400;
int p[maxn], q[maxn];
int main()
{
//freopen("in.txt", "r", stdin);
int T; cin >> T;
for(int t = 1; t <= T; ++t){
int n; cin >> n;
for(int i = 0; i < n; ++i)
cin >> p[i];
for(int i = 0; i < n; ++i)
cin >> q[i];
bool ok = true;
int beg = 0;
for(int i = 0, j = 0; i < n; ++i){
int sum = 0;
for(j = 0; j < n; ++j){
int cur = (i+j)%n;
sum += p[cur];
if(sum >= q[cur])
sum -= q[cur];
else{
if(cur < i || cur == n-1) {ok = false; break;}
else {i = cur; beg = cur + 1; break;}
}
}
if(j == n || !ok) break;
}
if(!ok) printf("Case %d: Not possible\n", t);
else printf("Case %d: Possible from station %d\n", t, beg + 1);
}
return 0;
}