【华为笔试】第一题


华为这次的题感觉比上次难好多啊,主要是输入好麻烦,就做出来了一道题,悲哀
第一题代码ac,写得很简陋(自我记录)
import java.util.ArrayList;
import java.util.Scanner;

public class huawei {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int n = in.nextInt();
ArrayList<String> arr = new ArrayList<>();
while(in.hasNext()){
arr.add(in.nextLine());
}
/*String[] str = new String[arr.size()];
for(int m = 0; m < arr.size(); m++){
str[m++] =arr.get(m);
}*/
int max = 0;
for(int m = 0; m < arr.size(); m++){
String []str = arr.get(m).split(",");
max = max > str.length ? max : str.length;
}
int ll = 0;
String result = "0";
while(ll < max){
for(int i = 0; i < arr.size(); i++){
String []str = arr.get(i).split(",");
for(int j = 0; j < n; j++){
if(ll + j < str.length)
result += str[ll + j] + ",";
}
if(i == arr.size() - 1){
ll = ll + n;
}
}
}
//System.out.println(result);
String []str = result.split(",");
for(int i = 1; i < str.length - 1; i++){
System.out.print(str[i] + ",");
}
System.out.print(str[str.length - 1]);

}

}


#华为##笔试题目##题解#
全部评论
厉害!
点赞
送花
回复
分享
发布于 2019-04-03 21:50
厉害!
点赞
送花
回复
分享
发布于 2019-04-03 21:55
秋招专场
校招火热招聘中
官网直投
有没有人告知 c++ 该如何处理这次的输入啊   
点赞
送花
回复
分享
发布于 2019-04-03 21:56
我本地测的都是对的,提交 显示未通过,说什么可能存在数组越界,0%。 **,打击好大
点赞
送花
回复
分享
发布于 2019-04-03 22:00
大哥你这个能策士多组数据么?我看着怎么不行啊
点赞
送花
回复
分享
发布于 2019-04-03 22:07
你这个感觉有问题啊
点赞
送花
回复
分享
发布于 2019-04-03 22:16
你ac了么
点赞
送花
回复
分享
发布于 2019-04-03 22:29
为撒我直接复制你的代码拉到idea上,断了不了输入,一直按enter而没用
点赞
送花
回复
分享
发布于 2019-04-03 23:22
#include <iostream> #include <vector> #include <string> #include <set> #include <map> #include <deque> #include <algorithm> using namespace std; deque<char> str2vec(string s){ deque<char> res; for (char c : s){ if (c !=','){ res.push_back(c); } } return res; } string vec2str(vector<char> s){ string res = ""; for (int i = 0; i < s.size(); i++){ if (i == s.size()-1) res+= s[i]; else res = res + s[i] + ','; } return res; } vector<char> process(vector<deque<char>> stringarr, int sum, string k){ vector<char> res; int i = 0; // 用以说明遍历到了第几组数 int cnt = 0; int num = atoi(k.c_str()); while (cnt < sum) { int j = 0; while(j++ < num) { if (stringarr[i].empty()) break; res.push_back(stringarr[i].front()); stringarr[i].pop_front(); cnt++; } i++; if (i >= stringarr.size()) i=0; } return res; } int main(){ while (1){ string k; string s; int sum = 0; bool flag = true; string ans; vector<deque<char>> stringarr; while (getline(cin, s)){ if (flag){ k = s; flag = false; continue; } if (s.empty()) break; deque<char> qs = str2vec(s); stringarr.push_back(qs); } for (auto i : stringarr) sum += i.size(); vector<char> res = process(stringarr, sum ,k); ans = vec2str(res); cout <<ans<<endl; } }这个可以吗?
点赞
送花
回复
分享
发布于 2019-04-03 23:38

相关推荐

点赞 评论 收藏
转发
5 27 评论
分享
牛客网
牛客企业服务