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 内存限制: ...
随机推荐
- ios 从网络上获取图片并在UIImageView中显示
ios 从网络上获取图片 -(UIImage *) getImageFromURL:(NSString *)fileURL { NSLog(@"执行图片下载函数"); UIIm ...
- Python ~~~ 面向对象的利器
class Rectangle(): # 有没有括号都行 . def __init__(self,x,y): self.x=x self.y=y def getPeri(self): def getA ...
- Android Manifest.xml详解
一.关于AndroidManifest.xml AndroidManifest.xml 是每个android程序中必须的文件.它位于整个项目的根目录,描述了package中暴露的组件(activiti ...
- Java 集合深入理解(11):LinkedList
点击查看 Java 集合框架深入理解 系列, - ( ゜- ゜)つロ 乾杯~ 今天心情鱼肚白,来学学 LinkedList 吧! 日常开发中,保存一组数据使用的最多的就是 ArrayList, 其次就 ...
- Nullsoft教程 NSIS初学者图文教程一
Nullsoft教程 NSIS初学者图文教程一 来源:互联网 作者:佚名 时间:03-29 00:34:33 [大 中 小] Nullsoft Installation System(nsis) 是一 ...
- 结构体用于map,set时要重载运算符<
#include<iostream> #include<set> using namespace std; struct P { int entry; int time; bo ...
- [转]SQLite C/C++
SQLite C/C++ http://blog.csdn.net/diaoser/article/details/6830786 辅助工具工具 Sqlite数据库的管理工具有SQLiteMana ...
- Selenium - IWebDriver.SwitchTo() frame 和 Window 的用法
IWebDriver.SwitchTo().Frame(IWebElement frame) 如果一个页面是一个html元素, 只有一个head, 一个body, 那么使用IWebDriver.Fin ...
- OptionParser
给个例子: from optparse import OptionParser msg_usage = 'usage: %prog [-W] windows size [-H] h island si ...
- ANTLR3完全参考指南读书笔记[06]
前言 这段时间在公司忙的跟狗似的,但忙的是没多少技术含量的活儿. 终于将AST IR和tree grammar过了一遍,计划明天写完这部分的读书笔记. 内容 1 内部表示AST构建 2 树文法 ...