包含多组测试数据,每组测试数据包含两行,代表长度相等的两个字符串S1和S2(仅由小写字母组成,长度不超过100)。
合并后的新字符串S
abc def
afbecd
//先循环短字符串长度的次数,然后把长的字符串剩下的拼接上 #include <iostream> #include <string> using namespace std; int main(){ int i; string s1,s2; while(cin>>s1>>s2){ int len1=s1.length(); int len2=s2.length(); string str=""; int len=len1>len2?len2:len1; for(i=0;i<len;i++){ str+=s1[i]; str+=s2[len2-i-1]; } if(len1>len2){ for(i=len2;i<len1;i++) str+=s1[i]; }else{ for(i=len1;i<len2;i++) str+=s2[i]; } cout<<str<<endl; } return 0; }
#include <iostream> #include <string> using namespace std; int main() { string S1, S2; while (cin >> S1 >> S2) { int lenS1 = S1.length(), lenS = 2 * lenS1; //定义并计算S1, S2, S的长度 for (int i = 0; i < lenS; i++) if (i % 2 == 0) //若循环变量为偶数,则打印S1的正序值 cout << S1[i / 2]; else //否则打印S2的逆序值 cout << S2[(lenS - i) / 2]; cout << endl; } }
#include<iostream> #include<cstring> #include<vector> #include<algorithm> using namespace std; vector<char> s;//存储合并后的字符串 void conslidate(string s1, string s2, int n) { //s.clear(); for (int i = 0; i < n; i++) { char a = s1[i]; s.push_back(a); char b = s2[i]; s.push_back(b); } } int main(void) { string s1, s2; cin >> s1 >> s2; reverse(s2.begin(), s2.end());//转置第二个字符串 int n = s1.length(); conslidate(s1, s2, n); for (vector<char>::iterator it = s.begin(); it != s.end(); it++) cout << *it; return 0; }
#include<bits/stdc++.h> using namespace std; stack<char>s; int main() { char s1[101],s2[101]; scanf("%s",s1); scanf("%s",s2); int j=0; while(s2[j]!='\0') { s.push(s2[j]); j++; } int i=0; while(j--) { printf("%c",s1[i]); if(!s.empty()) { printf("%c",s.top()); s.pop(); } i++; } return 0; }
#include<stdio.h>//先复制a字符串再倒序复制b字符串即可,别忘记i+=2而不是i++ int main() { char a[100],b[100],c[200]; scanf("%s%s",a,b); int i,j,n1,n2,n3,num; n1=strlen(a);n2=strlen(b);n3=n1+n2; c[n3]='\0';//别忘了\0要不然后面乱码 num=0; for(i=0;i<n3;i+=2) c[i]=a[num++]; num=n2-1; for(i=1;i<n3;i+=2) c[i]=b[num--]; printf("%s",c); }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); char[] a = scanner.next().toCharArray(); char[] b = scanner.next().toCharArray(); StringBuilder builder = new StringBuilder(); int i= 0; int j= b.length-1; while (i<a.length||j>=0) builder.append(a[i++]).append(b[j--]); System.out.println(builder.toString()); } }
#include<iostream> #include<string> using namespace std; int main(){ string str1; string str2; char str[200]; while(getline(cin , str1)){ getline(cin , str2); int len = str1.size(); for(int i=0;i<str1.size();i++){ str[2*i] = str1[i]; str[2*i+1] = str2[len-1-i]; } for(int i=0;i<2*len;i++){ cout<<str[i]; } cout<<endl; } return 0; }
//这个就是读一个输出一个就OK了,但是得要考虑两个长度不一样的情况 //好像改成或的方式会比较容易,没那么麻烦 #include<bits/stdc++.h> using namespace std; int main() { char s1[101], s2[101]; while (cin >> s1 >> s2) { int len1 = strlen(s1), len2 = strlen(s2), pos1 = 0, pos2 = len2 - 1; for (int i = 0; i<len1 + len2&&pos1 < len1&&pos2 >= 0; i++) { if (i % 2 == 0) cout << s1[pos1++]; else cout << s2[pos2--]; } if (pos2>=0 && pos1 == len1) { for (int i = pos2; i >= 0; i--) cout << s2[i]; } else { if (pos2 == -1 && pos1 < len1) { for (int i = pos1; i < len1; i++) cout << s1[i]; } } } }
#include<stdio.h> #include<string.h> int main (){//the shorter,the better. int i,j,l;char s1[100],s2[100]; for(;~scanf("%s%s",s1,s2);){ for (l=strlen(s1),i=0,j=strlen(s2)-1;i<l||j>=0;i>=l?:printf("%c",s1[i]),j<0?:printf("%c",s2[j]),++i,--j); printf("\n"); } }
#include <cstdio> #include <cstring> const int N = 101; int main() { char s1[N], s2[N], s3[N * 2]; while (scanf("%s%s", s1, s2) != EOF) { int len = strlen(s1); int j = 0; for (int i = 0; i < len; i++) { s3[j++] = s1[i]; s3[j++] = s2[len - 1 - i]; } s3[j] = '\0'; printf("%s\n", s3); } return 0; }
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner input = new Scanner(System.in); while(input.hasNext()){ String s1 = input.next(); String s2 = input.next(); String s=""; int i; for(i=0;i<s1.length()&&i<s2.length();i++) { s=s+s1.charAt(i); s=s+s2.charAt(s2.length()-i-1); } if(s1.length()>s2.length()) s=s+s1.substring(i, s1.length()); else s=s+s2.substring(i, s2.length()); System.out.println(s); } input.close(); } }
try: while 1: print ''.join([i for x in zip(raw_input(), raw_input()[::-1]) for i in x]) except: pass
//写个简单点的程序 #include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std; int main() { string str1,str2; while(cin>>str1>>str2) { string newstring=""; reverse(str2.begin(),str2.end()); for(int i=0;i<str1.length();i++) { newstring=newstring+str1[i]+str2[i]; } cout<<newstring<<endl; } return 0; }
#include<stdio.h> #include<string.h> int main(){ char a[101],b[101]; while(scanf("%s %s",a,b)!=EOF){ int len=strlen(a); for(int i=0;i<len;i++) printf("%c%c",a[i],b[len-1-i]); printf("\n"); } return 0; }
#include <algorithm> #include <iostream> #include <string> using namespace std; int main() { string s1, s2; while (cin >> s1 >> s2) { reverse(s2.begin(), s2.end()); string s; //合并后的新字符串 for (int i = 0; i < s1.length() && i < s2.length(); i++) { s += s1[i]; s += s2[i]; } if (s1.length() > s2.length()) { s += s1.substr(s2.length()); } if (s2.length() > s1.length()) { s += s2.substr(s1.length()); } cout << s << endl; } return 0; }
#include <cstdio> #include <string> using namespace std; int main(){ char s1[101]; char s2[101]; while(scanf("%s %s",s1,s2) != EOF){ string str1 = s1; string str2 = s2; int len = str1.length(); for(int i = 0; i < len; ++i){ printf("%c%c",str1[i],str2[len-1-i]);//一个字符一个字符遍历输出 } } return 0; }