站点图标 陌路寒暄

A--A Repeating Characters

A--A Repeating Characters

时间: 1ms        内存:128M

描述:

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

提示:

参考答案(内存最优[1032]):

/*
 * A - Repeating Characters
 * Greater NY Regional, Oct 30, 2011
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define	MAX_REPEAT	8
#define	MAX_CHARS	20

int main(int argc, char **argv)
{
	char szBuf[64], szStr[32], szNew[MAX_REPEAT * MAX_CHARS + 1];
	int n, np, ds, r, i, io, idx, nLen;

	if(::fgets(&(szBuf[0]), sizeof(szBuf)-1, stdin) != NULL){
		np = ::atoi(&(szBuf[0]));
		for(n = 1; n <= np; n++){
			if(::fgets(&(szBuf[0]), sizeof(szBuf)-1, stdin) == NULL){
				break;
			}
			if(::sscanf(&(szBuf[0]), "%d %d %s", &(ds), &(r), &(szStr[0])) != 3){
				break;
			}
			if(ds != n){
				::fprintf(stderr, "Problem number mismatch: %d != %d\n", n, ds);
				break;
			}
			nLen = ::strlen(&(szStr[0]));
			for(io = 0, idx = 0; idx < nLen; idx++){
				for(i = 0; i < r; i++){
					szNew[io++] = szStr[idx];
				}
			}
			szNew[io] = '\0';
			::fprintf(stdout, "%d %s\n", n, &(szNew[0]));
		}
	}
	return(0);
}

参考答案(时间最优[0]):

/*
 * A - Repeating Characters
 * Greater NY Regional, Oct 30, 2011
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define	MAX_REPEAT	8
#define	MAX_CHARS	20

int main(int argc, char **argv)
{
	char szBuf[64], szStr[32], szNew[MAX_REPEAT * MAX_CHARS + 1];
	int n, np, ds, r, i, io, idx, nLen;

	if(::fgets(&(szBuf[0]), sizeof(szBuf)-1, stdin) != NULL){
		np = ::atoi(&(szBuf[0]));
		for(n = 1; n <= np; n++){
			if(::fgets(&(szBuf[0]), sizeof(szBuf)-1, stdin) == NULL){
				break;
			}
			if(::sscanf(&(szBuf[0]), "%d %d %s", &(ds), &(r), &(szStr[0])) != 3){
				break;
			}
			if(ds != n){
				::fprintf(stderr, "Problem number mismatch: %d != %d\n", n, ds);
				break;
			}
			nLen = ::strlen(&(szStr[0]));
			for(io = 0, idx = 0; idx < nLen; idx++){
				for(i = 0; i < r; i++){
					szNew[io++] = szStr[idx];
				}
			}
			szNew[io] = '\0';
			::fprintf(stdout, "%d %s\n", n, &(szNew[0]));
		}
	}
	return(0);
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

退出移动版