鱼C论坛

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

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

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

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

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

x
看图
最佳答案
2021-11-27 19:30:47
本帖最后由 jhq999 于 2021-11-27 19:37 编辑
  1. int main()
  2. {
  3.         int N=0,K=0,i=0,j=0,count=0,sum=0;
  4.         scanf("%d%d",&N,&K);
  5.         fflush(stdin);
  6.         char (*ch)[32]=(char (*)[32])(new char[N*32]);
  7.         i=0;
  8.         count=0;
  9.         sum=0;
  10.         while (i<N)
  11.         {
  12.                 scanf("%s",ch[i]);
  13.                 j=0;
  14.                 while (ch[i][++j]);
  15.                 ch[i][31]=j;
  16.                 i++;
  17.         }
  18.         for (i = 0; i < N; i++)
  19.         {
  20.                 count+=ch[i][31];
  21.                 sum++;
  22.                 if (count+((i==9)?31:ch[i+1][31])>K)
  23.                 {
  24.                         for (j = 0; j <sum-1; j++)
  25.                         {
  26.                                 printf("%s ",ch[i-sum+j+1]);
  27.                         }
  28.                         printf("%s\n",ch[i-sum+j+1]);
  29.                         sum=0;
  30.                         count=0;
  31.                 }

  32.         }


  33.         delete[] ch;
  34.         return 0;

  35. }
复制代码
  1. 10 1
  2. hello my name is Bessiess and this is my essayss
  3. hello
  4. my
  5. name
  6. is
  7. Bessiess
  8. and
  9. this
  10. is
  11. my
  12. essayss

  13. 10 5
  14. hello my name is Bessiess and this is my essayss
  15. hello
  16. my
  17. name
  18. is
  19. Bessiess
  20. and
  21. this
  22. is my
  23. essayss

  24. 10 7
  25. hello my name is Bessiess and this is my essayss
  26. hello my
  27. name is
  28. Bessiess
  29. and this
  30. is my
  31. essayss

  32. 10 10
  33. hello my name is Bessiess and this is my essayss
  34. hello my
  35. name is
  36. Bessiess
  37. and this is
  38. my essayss



复制代码
QQ20211127-0.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-11-27 17:41:28 | 显示全部楼层
很急
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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


先应急,后面再慢慢修改
  1. int main()
  2. {
  3.         int N=0,K=0,i=0,j=0,count=0,sum=0;
  4.         scanf("%d%d",&N,&K);
  5.         fflush(stdin);
  6.         char (*ch)[16]=(char (*)[16])(new char[N*16]);
  7.         i=0;
  8.         count=0;
  9.         sum=0;
  10.         while (i<N)
  11.         {
  12.                 scanf("%s",ch[i]);
  13.                 j=0;
  14.                 while (ch[i][++j]);
  15.                 if ((count+j)<=K)
  16.                 {
  17.                         count+=j;
  18.                         sum++;

  19.                 }
  20.                 else
  21.                 {   
  22.                         if (sum)
  23.                         {count=j;
  24.                         for (j =0; j<sum-1 ; j++)
  25.                         {
  26.                                 printf("%s ",ch[i-sum+j]);
  27.                         }
  28.                         printf("%s\n",ch[i-sum+j]);

  29.                         sum=1;

  30.                         }
  31.                         else
  32.                         {
  33.                                 printf("%s\n",ch[i]);
  34.                         }

  35.                 }
  36.                 i++;
  37.         }
  38.         for (j =0; j<sum-1 ; j++)
  39.         {
  40.                 printf("%s ",ch[i-sum+j]);
  41.         }
  42.         printf("%s\n",ch[i-sum+j]);
  43.         delete[] ch;
  44.         return 0;

  45. }
复制代码
  1. 10 5
  2. hello my name is Bessie and this is my essay
  3. hello
  4. my
  5. name
  6. is
  7. Bessie
  8. and
  9. this
  10. is my
  11. essay
复制代码
  1. 10 7
  2. hello my name is Bessie and this is my essay
  3. hello my
  4. name is
  5. Bessie
  6. and this
  7. is my
  8. essay

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-27 19:30:47 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jhq999 于 2021-11-27 19:37 编辑
  1. int main()
  2. {
  3.         int N=0,K=0,i=0,j=0,count=0,sum=0;
  4.         scanf("%d%d",&N,&K);
  5.         fflush(stdin);
  6.         char (*ch)[32]=(char (*)[32])(new char[N*32]);
  7.         i=0;
  8.         count=0;
  9.         sum=0;
  10.         while (i<N)
  11.         {
  12.                 scanf("%s",ch[i]);
  13.                 j=0;
  14.                 while (ch[i][++j]);
  15.                 ch[i][31]=j;
  16.                 i++;
  17.         }
  18.         for (i = 0; i < N; i++)
  19.         {
  20.                 count+=ch[i][31];
  21.                 sum++;
  22.                 if (count+((i==9)?31:ch[i+1][31])>K)
  23.                 {
  24.                         for (j = 0; j <sum-1; j++)
  25.                         {
  26.                                 printf("%s ",ch[i-sum+j+1]);
  27.                         }
  28.                         printf("%s\n",ch[i-sum+j+1]);
  29.                         sum=0;
  30.                         count=0;
  31.                 }

  32.         }


  33.         delete[] ch;
  34.         return 0;

  35. }
复制代码
  1. 10 1
  2. hello my name is Bessiess and this is my essayss
  3. hello
  4. my
  5. name
  6. is
  7. Bessiess
  8. and
  9. this
  10. is
  11. my
  12. essayss

  13. 10 5
  14. hello my name is Bessiess and this is my essayss
  15. hello
  16. my
  17. name
  18. is
  19. Bessiess
  20. and
  21. this
  22. is my
  23. essayss

  24. 10 7
  25. hello my name is Bessiess and this is my essayss
  26. hello my
  27. name is
  28. Bessiess
  29. and this
  30. is my
  31. essayss

  32. 10 10
  33. hello my name is Bessiess and this is my essayss
  34. hello my
  35. name is
  36. Bessiess
  37. and this is
  38. my essayss



复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-25 09:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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