现在的位置: 首页 > 综合 > 正文

acm–字符串的格式化

2014年11月09日 ⁄ 综合 ⁄ 共 542字 ⁄ 字号 评论关闭
/*
*程序的版权和版本声明部分:
*Copyright(c)2013,烟台大学计算机学院学生
*All rights reserved.
*文件名称:
*作者:田成琳
*完成日期:2013年 12月19 日
*版本号:v1.0
*对任务及求解方法的描述部分:
*输入描述: 输入一个字符串
           Sample Input
           asdfasdf
           3
*问题描述:输入一个字符串,将其按给定的长度n格式化并输出,若n=0,则输出原字符串
*程序输出:格式化并输出
           Sample Output
           asd
           fas
           df
*问题分析:
*算法设计:
*/
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{
    char str[100];
    int pos,j;
    cin>>str>>pos;
    int l=strlen(str),z;
    if(pos==0)
    {
        cout<<str;
    }
    else
    {
        z=l/pos;
        for(j=1; j<=z; j++)
        {
            for(int i=pos*(j-1); i<pos*j; i++)
            {
                cout<<str[i];
            }
            cout<<endl;
        }
        for(int u=z*pos;u<l;u++)
        {
            cout<<str[u];
        }
    }
    return 0;
}

运行结果:

心得体会:

抱歉!评论已关闭.