YTUOJ-A--A Repeating Characters

题目描述

For this problem,you will write a program that takes a string of characters,S,and creates a new string of characters,T,with each character repeated R times.That is,R copies of the first character of S,followed by R copies of the second character of S,and so on.Valid characters for S are the QR
Code “alphanumeric” characters:
     0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ$%*+-. /:

输入

The first line of input contains a single integer P,(1<=P<=1000),which is the number of data sets that follow. Each data set is single line of input consisting of the data set number N,followed by a space,followed by the repeat count R,(1<=R<=8),followed by a space ,followed by the string S.The length of string S will always be at least one and  no more than 20 characters.All the characters will be from the set of characters shown above.

输出

For each data set there is one line of output. It contains the data set number,
N, followed by a single apace which is then followed by the new string T,which is made of each character in S repeated R times.

样例输入

2
1 3 ABC
2 5 /HTP

样例输出

1 AAABBBCCC
2 /HHHHHTTTTTPPPPP

提示

代码如下:

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

int main()
{
    int P,N,num;
    char c[20];
    cin>>P;
    while (P--)
    {
        int i=0,j;
        cin>>N>>num>>c;
        cout<<N<<" ";
        while(c[i]!='\0')
        {
            for (j=1; j<=num; j++)
            {
                cout<<c[i];
            }
            i++;
        }
        cout<<endl;

    }
    return 0;
}


运行结果:

 

学习心得:

我真是哔了GOU了,,,由于之前在书上看到了有一个WERTYU的字符串的题目,这次做的时候首先想的就是按照那个方法去做,结果输入3之后如果加空格了再输入ABC的话输出的结果就是1    AAABBBCCC连同这个空格一起输出了3遍,然后就开始对getchar各种处理,if(c=='\0')或者&&c!='\0'等等完全没有作用,反而会不再输出字符,最后抛开了那个方法用了再简单不过的cin和while(c[i]!='\0')居然直接AC了,,,字符串的输入输出上还得下狠功夫啊。这次参加ACM也有两道题就是败在了这个上面。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值