OpenJudge计算概论-文字排版
/*======================================================================
文字排版
总时间限制: 1000ms 内存限制: 65536kB
描述
给一段英文短文,单词之间以空格分隔(每个单词应包括其前后紧邻的标点符号)。请将短文重新排版,要求如下:
每行不超过80个字符;每个单词居于同一行上;在同一行的单词之间以一个空格分隔;行首和行尾都没有空格。 输入
第一行是一个整数n,表示英文短文中单词的数目. 其后是n个以空格分隔的英文单词(单词包括其前后紧邻的标点符号,且每个单词长度都不大于40个字母)。
输出
排版后的多行文本,每行文本字符数最多80个字符,单词之间以一个空格分隔,每行文本首尾都没有空格。
样例输入
84
One sweltering day, I was scooping ice cream into cones and told my four children they could "buy" a cone from me for a hug. Almost immediately, the kids lined up to make their purchases. The three youngest each gave me a quick hug, grabbed their cones and raced back outside. But when my teenage son at the end of the line finally got his turn to "buy" his ice cream, he gave me two hugs. "Keep the changes," he said with a smile.
样例输出
One sweltering day, I was scooping ice cream into cones and told my four
children they could "buy" a cone from me for a hug. Almost immediately, the kids
lined up to make their purchases. The three youngest each gave me a quick hug,
grabbed their cones and raced back outside. But when my teenage son at the end
of the line finally got his turn to "buy" his ice cream, he gave me two hugs.
"Keep the changes," he said with a smile.
========================================================================*/
大概分三类情况做不同处理,详细分类如图所示:

#include<stdio.h>
#include<string.h>
int main()
{
int n,i;
char a[]={'\0'},b[]={'\0'};//a数组是每次读取的一个单词(以空格分割的字符串),b数组是准备输出的一行(符合题目要求的字符串)。
int lenA=,lenB=;
freopen("4.in","r",stdin);
freopen("result.out","w",stdout);
scanf("%d",&n);
for(i=;i<n;i++)
{
scanf("%s",a);
lenA=strlen(a);
if(lenB++lenA<)
{
if(lenB>)
strcat(b," ");
strcat(b,a);
lenB=strlen(b);
}
else if(lenB++lenA==||lenB++lenA==)
{
printf("%s %s\n",b,a);
lenB=;
b[]='\0';//清空b数组的数据
}
else //当lenB+1+lenA>80
{
printf("%s\n",b);
strcpy(b,a);
lenB=strlen(b);
}
}
printf("%s\n",b);//前面的循环可能会使最后一行没输出并一直留在b数组里面
return ;
}
OpenJudge计算概论-文字排版的更多相关文章
- OpenJudge计算概论-取石子游戏
OpenJudge计算概论-取石子游戏[函数递归练习] /*====================================================================== ...
- Openjudge计算概论——数组逆序重放【递归练习】
/*===================================== 数组逆序重放 总时间限制:1000ms 内存限制:65536kB 描述 将一个数组中的值按逆序重新存放. 例如,原来的顺 ...
- OpenJudge计算概论-计算书费
/*============================================== 计算书费 总时间限制: 1000ms 内存限制: 65536kB 描述 下面是一个图书的单价表: 计算 ...
- OpenJudge计算概论-最高的分数
/*======================================================== 最高的分数 总时间限制: 1000ms 内存限制: 65536kB 描述 孙老师 ...
- OpenJudge计算概论-比饭量【枚举法、信息数字化】
/*====================================================================== 比饭量 总时间限制: 1000ms 内存限制: 655 ...
- Openjudge计算概论-角谷猜想
/*===================================== 角谷猜想 总时间限制: 1000ms 内存限制: 65536kB 描述 所谓角谷猜想,是指对于任意一个正整数,如果是奇数 ...
- OpenJudge计算概论-字符串最大跨距
/*====================================================================== 字符串最大跨距 总时间限制: 1000ms 内存限制: ...
- Openjudge计算概论-求序列中的众数
/*===================================== 求序列中的众数 总时间限制: 1000ms 内存限制: 65536kB 描述 输入一个长度为N的整数序列 (不多于128 ...
- OpenJudge计算概论-计算鞍点
/*======================================================================== 计算鞍点 总时间限制: 1000ms 内存限制: ...
随机推荐
- BZOJ 1486 最小圈
二分答案是显然的,我们需要dfs版spfa判一下负环. 看起来是n^2其实很快. #include<iostream> #include<cstdio> #include< ...
- ERP员工入登记查询(六)
实现的功能:
- coins_多重背包
ps:原来用新浪,可是代码的排版不是很好,所以用博客园啦,先容许我把从八月份开始的代码搬过来,从这里重新出发,希望这里可以一直见证我的成长. Time Limit: 2000/1000 MS (Jav ...
- CODEVS1533 互斥的数(哈希表)
给定一个集合,要求一个最大子集,满足两两之间不互斥.对两个数x,y互斥的定义是,y=p*x. 先对集合中的数从小到大排序后线性扫,若一个数x可以取则取,取完之后p*x这个数不可取.由于数字较大,使用哈 ...
- 判断字符串中是否有SQL攻击代码
判断一个输入框中是否有SQL攻击代码 public const string SQLSTR2 = @"exec|cast|convert|set|insert|select|delete|u ...
- @Html.DropDownListFor 绑定列表项
MVC中为 DropDownListFor 绑定列表项, 一种方案从后台加载列表内容,通过ViewData传递到前台页面. View: <div class="editor-label ...
- 不错的nginx文章,找个时间好好看下。
http://blog.csdn.net/chosen0ne/article/category/915324
- 【题解】【字符串】【Leetcode】Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- Customize R initiallization in Mac
First, we need to find a "Rprofile" document. locate Rprofile Then we find Rprofile docume ...
- jquery 下了框
//选中特定值 $("#Select_GBType").attr("value", "选中项ID"); //获取选中项的值 $(" ...