题目描述
Given are strings s and t of length N each, both consisting of lowercase English letters.
Let us form a new string by alternating the characters of S and the characters of T, as follows: the first character of S, the first character of T, the second character of S, the second character of T, ..., the N-th character of S, the N-th character of T. Print this new string.
Constraints
·1≤N≤100
·|S|=|T|=N
·S and T are strings consisting of lowercase English letters.
输入
Input is given from Standard Input in the following format:
N
S T
输出
Print the string formed.
样例输入 Copy
【样例1】 2 ip cc 【样例2】 8 hmhmnknk uuuuuuuu 【样例3】 5 aaaaa aaaaa
样例输出 Copy
【样例1】 icpc 【样例2】 humuhumunukunuku 【样例3】 aaaaaaaaaa
这道题要AC起来十分简单,不要被题目迷惑了,傻傻地建立两个字符串,只需要建立一个字符数组 即可。根据题目我们可以得知,第一个字符串是放在奇数位置的,第二个字符串是放在偶数位置的。那么我们就把字符数组的奇数位置输入第一个字符串的信息,偶数位置输入第二个字符串的信息,在打印输出字符数组即可。
AC代码:
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
char a[201];
int n;
cin>>n;
for(int i=0;i<2*n;i+=2)
cin>>a[i];
for(int j=1;j<2*n;j+=2)
cin>>a[j];
for(int i=0;i<2*n;i++)
cout<<a[i];
return 0;
}
以上是个人小白的见解,如有更好解法,请指教!