题目:赢球票
2016年国赛C/C++ C组第四题
感觉有点像莫比乌斯环那种
问题1:题目给的是一个环,如何模拟在环上的移动
回答1:拆环为链,当到达链的最后位置,返回到链的起点
问题2:如何表示指定位置的卡片被拿走
回答2:构建bool flag[N]
问题3:选择哪个为起点能拿走最多的卡片
回答3:枚举每个起点
问题4:游戏什么时候结束
回答4:最终结局一:全部卡片被拿走即cnt=n
最终结局二:数的数字大于n
答案
/*蓝桥杯 《赢球票》*/
#include <bits/stdc++.h>
using namespace std;
const int N = 1e2+10;
int n,arr[N],flag[N];
int main()
{
cin >> n;//牌数
for(int i = 1;i <= n; i++)
{
cin >> arr[i];//按序放置
}
int ans = 0;//球票总和
for(int i = 1;i <= n;i++)
{
//for(int j = 1;j <= n; j++) flag[j] = 0;
fill(flag + 1, flag + n + 1, 0); // 重置标记数组
int num = 1,pos = i,cnt = 0,sum = 0;
//num目前数的数字 pos轮到的位置,cnt拿到的卡片数目,sum球票总共和
while(1)
{
if(num > n || cnt == n) break;
//找到下一张未被拿走的卡片
while(flag[pos] == 1) {
pos++;
if(pos > n) pos = 1;
}
//符合拿走的条件
if(arr[pos] == num)
{
flag[pos]=1;
num = 1;
cnt++;
sum+=arr[pos];
}
else num++;
pos++;
if(pos>n) pos=1;
}
if(ans < sum) ans = sum;
}
cout << ans <<endl;
return 0;
}
得快点学了,不然白交300了呜呜呜呜orz
不知道上面为什么少了一条 if(pos>n) pos=1; 答案就不对了,评论区的大佬帮我看看呗