数组拼接
标题:数组拼接 | 时间限制:1秒 | 内存限制:32768K | 语言限制:不限
现在有多组整数数组,需要将它们合并成一个新的数组。合并规则,从每个数组里按顺序取出固定长度的内容合并到新的数组中,取完的内容会删除掉,如果该行不足固定长度或者已经为空,则直接取出剩余部分的内容放到新的数组中,继续下一行。
#include <iostream>
#include <vector>
#include <queue>
#include <string>
using namespace std;
int main()
{
int everyLen;
int arrNum;
cin>>everyLen>>arrNum;
vector<queue<int>> vec;
for(int i=0;i<arrNum;i++)
{
string str;
cin>>str;
int j=0;
string valStr="";
queue<int> cur;
while(j<str.size())
{
if(str[j]!=','){
valStr += str[j];
}
else{
cur.push(atoi(valStr.c_str()));
valStr = "";
}
j++;
}
if(valStr!="")
cur.push(atoi(valStr.c_str()));
vec.push_back(cur);
}
vector<int> result;
int hasFindedRowNum = 0;
int index = 0; //用于记录正在遍历哪一行。
while(hasFindedRowNum != arrNum)
{
index = index % arrNum;
if(vec[index].size()!=0){
for(int i=0;i<everyLen;i++)
{
if(vec[index].size()==0){
break;
}
result.push_back(vec[index].front());
vec[index].pop();
}
if(vec[index].size()==0){
hasFindedRowNum++;
}
}
index++;
}
for(int i=0;i<result.size();i++){
cout<<result[i];
if(i!=result.size()-1)
cout<<',';
}
return 0;
}
import java.util.Scanner;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int len = sc.nextInt();
ArrayList<String> lsit = new ArrayList();
sc.nextLine();
sc.nextLine();
while(sc.hasNext()){
lsit.add(sc.nextLine());
}
StringRes(lsit, len);
}
public static boolean isNull(ArrayList<String> list){
int i = 0;
for(i =0; i < list.size(); i++){
if (null != list.get(i)) {
break;
}
}
if(i<list.size()) {
return false;
}else{
return true;
}
}
public static void StringRes(ArrayList<String> lsit, int num){
String tem = "";
while(!isNull(lsit)){
for(int i = 0; i<lsit.size(); i++){
String sk = lsit.get(i);
if(sk == null) {
continue;
}
String[] gg = sk.split(",");
if(sk.length() == 0 ){
lsit.set(i, null);
}else{
if (gg.length <= num) {
tem = tem + sk + ",";
lsit.set(i, null);
}else{
for(int k = 0; k < num; k++) {
tem = tem+gg[k]+",";
}
String hh = "";
for(int l = num; l<gg.length; l++){
if(l == gg.length-1){
hh = hh + gg[l];
}else{
hh = hh + gg[l] + ",";
}
}
lsit.set(i,hh);
}
}
}
}
System.out.println(tem.substring(0,tem.length()-1));
}
}
拼多多集团-PDD公司氛围 753人发布