|
发表于 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
复制代码 |
|