数组拼接

标题:数组拼接 | 时间限制: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));
    }
}


全部评论
int length = Integer.parseInt(scanner.next()); int n = Integer.parseInt(scanner.next()); LinkedList<string> queue = new LinkedList<>(); for (int i = 0; i < n; i++) { String next = scanner.next(); queue.offer(next.split(",")); } List<string> resList = new ArrayList<>(); while (!queue.isEmpty()) { String[] poll = queue.poll(); int arrayLength = poll.length; if (arrayLength <= length) { resList.addAll(Arrays.asList(poll)); } else { resList.addAll(Arrays.asList(Arrays.copyOfRange(poll, 0, length))); queue.offer(Arrays.copyOfRange(poll, length, arrayLength)); } } System.out.println(resList.toString().replaceAll("\\[|\\]",""));</string></string>
1 回复 分享
发布于 2023-07-26 17:25 湖北

相关推荐

三分入剑:我觉得还是学历问题 如果你真的想要进大厂不想在小厂的话读个211得研究生吧 我感觉简历还没你好呢 我都实习了俩月了 我投了一百多份能投出20多份简历 能面试六七次 我们部门只招研究生了都 现在连9本都很难找到像样的大厂了 你又没打过rm这种 我觉得想要进步的话就考个研究生吧
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务