指针与二维数组练习-文字排版(C程序设计进阶 第5周)

编程题#3:文字排版

来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

注意: 总时间限制: 1000ms 内存限制: 65536kB

描述

给一段英文短文,单词之间以空格分隔(每个单词包括其前后紧邻的标点符号)。请将短文重新排版,要求如下:

每行不超过80个字符;每个单词居于同一行上;在同一行的单词之间以一个空格分隔;行首和行尾都没有空格。

输入

第一行是一个整数n,表示英文短文中单词的数目. 其后是n个以空格分隔的英文单词(单词包括其前后紧邻的标点符号,且每个单词长度都不大于40个字母)。

输出

排版后的多行文本,每行文本字符数最多80个字符,单词之间以一个空格分隔,每行文本首尾都没有空格。

样例输入


 
 
1
2
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.

样例输出


 
 
1
2
3
4
5
6
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.

参考代码1:

//注意在windows下编程控制台默认为80字符宽度,到了80个字符会自动换行,其他环境下不一定如此,所以不用作特殊处理
#include <iostream>
#include<string.h>
using namespace std;

int main()
{
    int words = 0, sum = 0;
    cin >> words;
    char str[41]={0};
    for (int i = 0; i < words; i++)
    {
        cin >> str;
        if (sum + 1 + strlen(str) > 80)
        {
            cout << endl;               
            sum = 0;        
        } 
        else if(i>0)
        {
            cout << " ";
            sum++;  
        }
        cout << str;
        sum += strlen(str);
    }
    return 0;
}


参考代码2:

#include<stdio.h>
#include<string.h>
#define MAX 40
#define LINE 80

int main()
{
int i, n, len;
int end = 0;
char word[MAX+1];
scanf("%d", &n);
for (i = 0; i < n; i++)
{
scanf("%s", word);
len = strlen(word);
if (end + len +1 > LINE)
{
printf("\n");
end = 0;
} 
else if(i>0)
{
printf(" ");
end++;
}
printf("%s", word);
end += len;
}
return 0;
}


参考代码3:

#include <iostream>  
#include <cstring>  
using namespace std;  
int main()  
{  
    char words[1000][40]; //用于保存每一个单词  
    int wordsLen[1000]; // 记录每一个单词的长度  
    int n; // 需要处理的单词总数  
    cin >> n;  
    cin.get();//cin之后要用cin.get()读取换行符,一开始没注意到错误在这里,还是基础不牢靠  
    for (int i = 0; i < n;i++) //输入单词数据,处理后得到每个单词的长度  
    {  
        char temp;  
        for (int j = 0; j < 40;j++) {  
            cin.get(temp);                
                if (temp != ' '&&temp != '\n') {  
                    words[i][j] = temp;  
                      
                }  
                else  
                {  
                    wordsLen[i] = j;  
                    break;  
                }  
            }  
          
    }  
    //先输出第一个单词  
    int length = wordsLen[0];  
    for (int j = 0; j < wordsLen[0]; j++)  
        cout << words[0][j];  
    for (int i = 1; i < n; i++)  
    {  
        //如果该单词,连同前面的一个空格加入后不换行,则输出空格和该单词  
        if (length + 1 + wordsLen[i] <= 80)  
        {  
            length = length + 1 + wordsLen[i];  
            cout << ' ';  
            for (int j = 0; j < wordsLen[i]; j++)  
                cout << words[i][j];  
        }  
        else//如果该单词,连同前面的一个空格加入后换行,则输出回车和该单词,另外重置本行现有长度为单词长度  
        {  
            cout << endl;  
            length = wordsLen[i];  
            for (int j = 0; j < wordsLen[i]; j++)  
                cout << words[i][j];  
        }  
    }  
    return 0;  
}  




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值