題意是給你五個輪子, 每個輪子上有一些開口, 現在每個輪子從0位置開始轉動,問最短啥時候5個輪子的開口相遇,剛開始看這道題的時候沒啥思路, 就像等到最後在寫,最後還沒思路看了題解, 直接模拟就行, 代碼如下:(注意處理邊界,他不是離散的360個口,而是一個區間。
/*
ID: m1500293
LANG: C++
PROG: spin
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
bool whell[6][390];
int w[5];
int idx[5] = {0, 0, 0, 0, 0};
int check()
{
for(int i=0; i<360; i++)
{
bool res = true;
for(int j=0; j<5; j++)
{
res = res&whell[j][(idx[j]+i)%360];
}
if(res)
return true;
}
return false;
}
int main()
{
freopen("spin.in", "r", stdin);
freopen("spin.out", "w", stdout);
memset(whell, 0, sizeof(whell));
for(int i=0; i<5; i++)
{
scanf("%d", &w[i]);
int t;
scanf("%d", &t);
for(int j=0; j<t; j++)
{
int st, wi;
scanf("%d%d", &st, &wi);
for(int k=st; k<=st+wi; k++)
whell[i][k%360] = 1;
}
}
for(int i=0; i<360; i++)
{if(check())
{
printf("%d\n", i);
return 0;
}
for(int j=0; j<5; j++)
{
idx[j] = ((idx[j]-w[j])%360+360)%360; //注意這裡可能為負數,wa了一次
}
}
printf("none\n");
}
)
轉載于:https://www.cnblogs.com/xingxing1024/p/5087380.html