第行表示第一种类型的内容,字符数量<=100,空格分隔。比如说
1 2 3 4 5 6 7 8 9
a b c
合并两种内容流,输出
1 2 3 4 a 5 6 7 8 b 9 c
1 2 3 4 5 6 7 8 9 a b c
1 2 3 4 a 5 6 7 8 b 9 c
import java.util.*; public class Main{ public static void main(String [] args) { Scanner scanner = new Scanner(System.in); String str1 = scanner.nextLine(); String str2 = scanner.nextLine(); List<String> list = doInsert(str1.trim().split(" "),str2.trim().split(" ")); for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i) + " "); } } private static List<String> doInsert(String[] c1, String[] c2) { List<String> list = new ArrayList<>(); int j = 0; int count = 0; for (int i = 0; i < c1.length; i++) { list.add(c1[i]); count++; if (count == 4 && j < c2.length){ count = 0; list.add(c2[j]); j++; } } while(j < c2.length){ list.add(c2[j++]); } return list; } }
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s1 = scanner.nextLine(); String s2 = scanner.nextLine(); String[] split1 = s1.split("\\s"); String[] split2 = s2.split("\\s"); ArrayList<String> list1 = new ArrayList<>(Arrays.asList(split1)); ArrayList<String> list2 = new ArrayList<>(Arrays.asList(split2)); for (int i = 4; i <= list1.size(); i += 4) { if (list2.size() > 0){ list1.add(i, list2.get(0)); list2.remove(0); i++; } } if (list2.size() > 0) { list1.addAll(list2); } System.out.println(String.join(" ", list1)); } }
importjava.util.*; publicclassMain{ publicstaticvoidmain(String[] args){ Scanner sc = newScanner(System.in); String[] S1 = sc.nextLine().split(" "); String[] S2 = sc.nextLine().split(" "); StringBuilder sb = newStringBuilder(); intindex2 = 0; inti = 0; while(i < S1.length){ intindex = 0; while(index < 4&& index + i < S1.length){ sb.append(S1[i+index]); index++; } if(index2 < S2.length){ sb.append(S2[index2]); index2++; } i += 4; } if(i < S1.length - 1){ for(intj = i; j < S1.length; j++){ sb.append(S1[j]); } } if(index2 < S2.length){ while(index2 < S2.length){ sb.append(S2[index2]); index2++; } } for(intj = 0; j < sb.length() - 1; j++){ System.out.print(sb.charAt(j)+" "); } System.out.print(sb.charAt(sb.length() - 1)); } } |
//字符串分割(楼上的分割函数) + 控制输出(逻辑比较清晰的控制输出) #include <bits/stdc++.h> using namespace std; vector<string> split(string in, string patten) { in += patten; vector<string> ve; int pos = in.find(patten); while (pos != string::npos) { string tem = in.substr(0, pos); ve.push_back(tem); in = in.substr(pos+1); pos = in.find(patten); } return ve; } int main() { string stra, strb; vector<string> vea, veb; getline(cin, stra); getline(cin, strb); vea = split(stra, " "); //分割字符串 veb = split(strb, " "); int anum = vea.size(); int bnum = veb.size(); int i = 0; int j = 0; int num ; vector<string> res; while(anum - 4 >=0 && bnum-1 >=0){ num = 4; while(num > 0){ res.push_back(vea[i]); num--; i++; } res.push_back((veb[j])); j++; anum = anum -4; bnum = bnum - 1; } for(;i<vea.size();i++){ res.push_back(vea[i]); } for(;j<veb.size();j++){ res.push_back(veb[j]); } int s; for(s=0;s<res.size()-1;s++){ cout<<res[s]<<" "; } cout<<res[s]<<endl; return 0; }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s1 = scanner.nextLine(); String s2 = scanner.nextLine(); System.out.println(combine(s1,s2)); } public static String combine(String s1, String s2){ String[] split1 = s1.split(" ", -1); String[] split2 = s2.split(" ", -1); StringBuilder sb = new StringBuilder(); int count = split1.length/4; for (int i = 0; i < split1.length; i++) { sb.append(split1[i]+" "); if ((i+1)%4==0&&((i+1)/4-1)<split2.length) sb.append(split2[(i+1)/4-1]+" "); } for (int i = count; i < split2.length; i++) { sb.append(split2[i]+" "); } sb.delete(sb.length()-1,sb.length()); return sb.toString(); } }
sl1 = input().split(" ") sl2 = input().split(" ") start = 0 while len(sl2)!=0: start += 4 if start <= len(sl1): sl1.insert(start,sl2[0]) del sl2[0] else: sl1.append(sl2[0]) del sl2[0] start += 1 str = sl1[0] if len(sl1) > 1: for i in range(1, len(sl1)): str += " "+sl1[i] print(str)
import java.util.Scanner; public class MergeStreams { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String stream1 = sc.nextLine(); String stream2 = sc.nextLine(); System.out.println(merge(stream1, stream2).trim()); } private static String merge(String stream1, String stream2) { String[] s1 = stream1.split(" "); String[] s2 = stream2.split(" "); int len1 = s1.length; int len2 = s2.length; int len = len1 / 4; boolean hasRemain = len1 % 4 != 0; int p = 0, k = 0; StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i++) { for (int j = 0; j < 4; j++) { sb.append(s1[p + j] + " "); } sb.append(s2[k] + " "); p += 4; k += 1; if (k == len2) { break; } } if (k == len2) { for (int i = p; i < len1; i++) { sb.append(s1[i] + " "); } } else { if (hasRemain) { for (int i = p; i < len1; i++) { sb.append(s1[i] + " "); } } for (int i = k; i < len2; i++) { sb.append(s2[i] + " "); } } return sb.toString(); } }
a = input().split() b = input().split() j, k = 0, 0 for u in b: a.insert(j + 4 + k, u) j += 4 k += 1 print(" ".join(a))
import java.util.*; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); String s1 = input.nextLine(); String s2 = input.nextLine(); int count = 3; int j = 0; StringBuffer res = new StringBuffer(); for (int i = 0; i < s1.length(); i = i + 2) { res.append(s1.charAt(i)); if (count == 0 && j < s2.length()) { res.append(s2.charAt(j)); count = 4; j = j + 2; } count--; } for (int k = j; k < s2.length(); k = k + 2) { res.append(s2.charAt(k)); } for (int i = 0; i < res.length(); i++) { System.out.print(res.charAt(i) + " "); } } }
function insert(str1,str2){ //var arr = [] var arr1 = str1.split(' ') var arr2 = str2.split(' ') var len = arr1.length var i = 4 var j = 0 while(i <= len){ arr1.splice(i,0,arr2[j]) j++ if(j >= arr2.length){ return arr1.join(' ') } i = i + 5 } for(var i = j ;j<arr2.length;j++){ arr1.push(arr2[j]) } return arr1.join(' ') }
word1=input().split() word2=input().split() count=0 for x in range(len(word1)): if(count==3): count=0 print(word1.pop(0),end=" ") print(word2.pop(0),end=" ") else: print(word1.pop(0),end=" ") count+=1 while len(word2)>0: print(word2.pop(0),end=" ")不知道为啥只有80%。
#include <iostream> (720)#include <vector> #include <string> using namespace std; vector<char> solve(vector<char>& v1, vector<char>& v2){ vector<char> res; if(v1.empty() && v2.empty()){ return res; } if(v1.empty()){ return v2; } if(v2.empty()){ return v1; } if(v1.size() < 4){ for(auto e : v1){ res.push_back(e); } for(auto e : v2){ res.push_back(e); } } else{ int j = 0; res.push_back(v1[0]); for(int i = 1; i < v1.size();){ if(i % 4 == 0 && j < v2.size()){ res.push_back(v2[j++]); } res.push_back(v1[i++]); } while(j < v2.size()){ res.push_back(v2[j++]); } } return res; } int main(){ vector<char> v1; vector<char> v2; vector<char> ans; char tmp, tep; while(cin >> tmp){ v1.push_back(tmp); if(cin.get() == '\n'){ break; } } while(cin >> tep){ v2.push_back(tep); if(cin.get() == '\n'){ break; } } ans = solve(v1, v2); for(auto e : ans){ cout << e << ' '; } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); String[]a1=sc.nextLine().split("\\s+"); String[]a2=sc.nextLine().split("\\s+"); String[]res=mergeStr(a1,a2); for(int i=0;i<res.length;i++){ if(i==res.length-1) System.out.print(res[i]); else System.out.print(res[i]+" "); } } public static String[]mergeStr(String[] a1,String[] a2){ if(a1.length<1&&a1.length<1) return null; if(a1.length<1) return a2; if(a2.length<1) return a1; int n=a1.length+a2.length; String[]res=new String[n]; int p1=0; int p2=0; for(int i=0;i<n;i++){ if(p1>=a1.length||(i+1)%5==0&&p2<a2.length) res[i]=a2[p2++]; else res[i]=a1[p1++]; } return res; } }