题意:有n杯茶,分G,B两种,分别有a,b杯。 要求在连续喝一种茶不能超过k次。输出喝茶的顺序
题解:构造一下就好了,代码写的好挫啊 Σ( ° △ °|||)︴
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e5+10;
char str[maxn];
int main()
{
int n,k,a,b;
while(scanf("%d%d%d%d",&n,&k,&a,&b)!=EOF)
{
if(a>=b)
{
int cnt=a/k;//a被分成几块
double num=a*1.0/k;
if(num==cnt)
cnt--;
if(cnt==0)
cnt=1;
int count=cnt;
cnt=b/count;//b每一块最少要填几个
if(cnt>k)
cnt=k;
if(cnt==0)
cnt=1;
int temp=0;
while(a>0&&b>0)
{
cnt=a/k;
num=a*1.0/k;
if(num==cnt)
cnt--;
if(cnt==0)
cnt=1;
count=cnt;
cnt=b/count;//每次都要更新cnt
if(cnt>k)
cnt=k;
if(cnt==0)
cnt=1;
if(a>=k)
{
for(int i=0;i<k;++i)
str[temp++]='G';
a-=k;
}
else
{
for(int i=0;i<a;++i)
str[temp++]='G';
a-=a;
}
if(a==0)
cnt=k;
if(b>=cnt)
{
for(int i=0;i<cnt;++i)
str[temp++]='B';
b-=cnt;
}
else
{
for(int i=0;i<b;++i)
str[temp++]='B';
b-=b;
}
}
if(a>=k)
{
for(int i=0;i<k;++i)
str[temp++]='G';
a-=k;
}
else
{
for(int i=0;i<a;++i)
str[temp++]='G';
a-=a;
}
if(a!=0 || b!=0)
printf("NO\n");
else{
str[temp]='\0';
printf("%s\n",str);
}
}
else
{
int cnt=b/k;
double num=b*1.0/k;
if(num==cnt)
cnt--;
if(cnt==0)
cnt=1;
int count=cnt;
cnt=a/count;
if(cnt>k)
cnt=k;
if(cnt==0)
cnt=1;
int temp=0;
while(a>0&&b>0)
{
cnt=b/k;
num=b*1.0/k;
if(num==cnt)
cnt--;
if(cnt==0)
cnt=1;
count=cnt;
cnt=a/count;
if(cnt>k)
cnt=k;
if(cnt==0)
cnt=1;
if(b>=k)
{
for(int i=0;i<k;++i)
str[temp++]='B';
b-=k;
}
else
{
for(int i=0;i<b;++i)
str[temp++]='B';
b-=b;
}
if(b==0)
cnt=k;
if(a>=cnt)
{
for(int i=0;i<cnt;++i)
str[temp++]='G';
a-=cnt;
}
else
{
for(int i=0;i<a;++i)
str[temp++]='G';
a-=a;
}
}
if(b>=k)
{
for(int i=0;i<k;++i)
str[temp++]='B';
b-=k;
}
else
{
for(int i=0;i<b;++i)
str[temp++]='B';
b-=b;
}
if(a!=0 || b!=0)
printf("NO\n");
else{
str[temp]='\0';
printf("%s\n",str);
}
}
}
return 0;
}
/*
10 4 9 1
10 5 5 5
10 3 5 5
16 3 8 8
16 2 4 12
15 3 5 10
15 3 4 11
20 2 12 8
*/