这个贪心怎么做?
看图 很急 本帖最后由 jhq999 于 2021-11-27 19:01 编辑张育玮 发表于 2021-11-27 17:41
很急
先应急,后面再慢慢修改
int main()
{
int N=0,K=0,i=0,j=0,count=0,sum=0;
scanf("%d%d",&N,&K);
fflush(stdin);
char (*ch)=(char (*))(new char);
i=0;
count=0;
sum=0;
while (i<N)
{
scanf("%s",ch);
j=0;
while (ch[++j]);
if ((count+j)<=K)
{
count+=j;
sum++;
}
else
{
if (sum)
{count=j;
for (j =0; j<sum-1 ; j++)
{
printf("%s ",ch);
}
printf("%s\n",ch);
sum=1;
}
else
{
printf("%s\n",ch);
}
}
i++;
}
for (j =0; j<sum-1 ; j++)
{
printf("%s ",ch);
}
printf("%s\n",ch);
delete[] ch;
return 0;
}
10 5
hello my name is Bessie and this is my essay
hello
my
name
is
Bessie
and
this
is my
essay
10 7
hello my name is Bessie and this is my essay
hello my
name is
Bessie
and this
is my
essay
本帖最后由 jhq999 于 2021-11-27 19:37 编辑
int main()
{
int N=0,K=0,i=0,j=0,count=0,sum=0;
scanf("%d%d",&N,&K);
fflush(stdin);
char (*ch)=(char (*))(new char);
i=0;
count=0;
sum=0;
while (i<N)
{
scanf("%s",ch);
j=0;
while (ch[++j]);
ch=j;
i++;
}
for (i = 0; i < N; i++)
{
count+=ch;
sum++;
if (count+((i==9)?31:ch)>K)
{
for (j = 0; j <sum-1; j++)
{
printf("%s ",ch);
}
printf("%s\n",ch);
sum=0;
count=0;
}
}
delete[] ch;
return 0;
}
10 1
hello my name is Bessiess and this is my essayss
hello
my
name
is
Bessiess
and
this
is
my
essayss
10 5
hello my name is Bessiess and this is my essayss
hello
my
name
is
Bessiess
and
this
is my
essayss
10 7
hello my name is Bessiess and this is my essayss
hello my
name is
Bessiess
and this
is my
essayss
10 10
hello my name is Bessiess and this is my essayss
hello my
name is
Bessiess
and this is
my essayss
页:
[1]