2020 GDUT Rating Contest III E. Word Processor

E. Word Processor

链接

题目描述
给出一个含n个单词的句子,要求输出这个句子,且每行字母数超过k个时换行,若输入某单词的过程中该行字母数超过k个,将该单词输到下一行。

题目分析
按照题目要求,在输入字符串的时候把句子分割成符合要求的几块,再输出即可。注意题目说There should be no space at the end of any line.

代码

#include <bits/stdc++.h>

using namespace std;

int main(){
	int n,m,p1=0,p2=0,len,sum=0;
	char ch,words[101][1501],temp[16];
	cin>>n>>m;
	for(int i=0;i<n;i++){
		scanf("%s",temp);
		len=strlen(temp);
		if(sum+len<=m){
			for(int j=0;j<len;j++)
				words[p1][p2++]=temp[j];
			words[p1][p2++]=' ';
			sum+=len;
		}
		else{
			words[p1][p2-1]='\n';
			p1++,p2=0,sum=len;
			for(int j=0;j<len;j++)
				words[p1][p2++]=temp[j];
			words[p1][p2++]=' ';
		}
		if(i==n-1)
			words[p1][p2-1]='\n';
	}
	for(int i=0;i<=p1;i++)
		printf("%s",words[i]);
}

发布了24 篇原创文章 · 获赞 1 · 访问量 658

猜你喜欢

转载自blog.csdn.net/palax0/article/details/104827690
今日推荐