鱼C论坛

 找回密码
 立即注册
查看: 789|回复: 3

[已解决]这个贪心怎么做?

[复制链接]
发表于 2021-11-27 17:05:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
看图
最佳答案
2021-11-27 19:30:47
本帖最后由 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)[32]=(char (*)[32])(new char[N*32]);
        i=0;
        count=0;
        sum=0;
        while (i<N)
        {
                scanf("%s",ch[i]);
                j=0;
                while (ch[i][++j]);
                ch[i][31]=j;
                i++;
        }
        for (i = 0; i < N; i++)
        {
                count+=ch[i][31];
                sum++;
                if (count+((i==9)?31:ch[i+1][31])>K)
                {
                        for (j = 0; j <sum-1; j++)
                        {
                                printf("%s ",ch[i-sum+j+1]);
                        }
                        printf("%s\n",ch[i-sum+j+1]);
                        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
QQ20211127-0.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-11-27 17:41:28 | 显示全部楼层
很急
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-11-27 18:39:46 | 显示全部楼层
本帖最后由 jhq999 于 2021-11-27 19:01 编辑


先应急,后面再慢慢修改
int main()
{
        int N=0,K=0,i=0,j=0,count=0,sum=0;
        scanf("%d%d",&N,&K);
        fflush(stdin);
        char (*ch)[16]=(char (*)[16])(new char[N*16]);
        i=0;
        count=0;
        sum=0;
        while (i<N)
        {
                scanf("%s",ch[i]);
                j=0;
                while (ch[i][++j]);
                if ((count+j)<=K)
                {
                        count+=j;
                        sum++;

                }
                else
                {    
                        if (sum)
                        {count=j;
                        for (j =0; j<sum-1 ; j++)
                        {
                                printf("%s ",ch[i-sum+j]);
                        }
                        printf("%s\n",ch[i-sum+j]);

                        sum=1;

                        }
                        else
                        {
                                printf("%s\n",ch[i]);
                        }

                }
                i++;
        }
        for (j =0; j<sum-1 ; j++)
        {
                printf("%s ",ch[i-sum+j]);
        }
        printf("%s\n",ch[i-sum+j]);
        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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-27 19:30:47 | 显示全部楼层    本楼为最佳答案   
本帖最后由 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)[32]=(char (*)[32])(new char[N*32]);
        i=0;
        count=0;
        sum=0;
        while (i<N)
        {
                scanf("%s",ch[i]);
                j=0;
                while (ch[i][++j]);
                ch[i][31]=j;
                i++;
        }
        for (i = 0; i < N; i++)
        {
                count+=ch[i][31];
                sum++;
                if (count+((i==9)?31:ch[i+1][31])>K)
                {
                        for (j = 0; j <sum-1; j++)
                        {
                                printf("%s ",ch[i-sum+j+1]);
                        }
                        printf("%s\n",ch[i-sum+j+1]);
                        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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-23 03:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表