关于一道纽劢科技笔试题

第一行输入 t表示输出组数
比如
输入:
2(表示两组)
6(表示1 2 3 4 5 6)
2  (表示1 2)
输出
1 3 5 4 2 6
1 2
有什么好方法我运行超时了
#笔试题目#
全部评论
望指教,不懂为什么要用数组模拟?题目意思不是固定从1开始么,直接循环输出不就行了么
点赞 回复 分享
发布于 2019-04-05 13:41
没看懂要干嘛
1 回复 分享
发布于 2019-04-05 00:38
#include<iostream> #include<vector> using namespace std; int main() {     int T;     cin >> T;     int temp;     int count = 0;     vector<int> v;     while (T--) {         cin >> temp;         for (int i = 1; i <= temp; i++) {             v.push_back(i);         }         while (count < temp) {             for (int i = 0; i < v.size(); i += 2) {                 cout << v[i] << " ";                 count++;                 if (i + 1 < v.size()) {                     v.push_back(v[i + 1]);                 }             }         }         cout << endl;         v.clear();     } }
点赞 回复 分享
发布于 2019-05-09 21:10
#include <iostream> #include <queue>using namespace std; int T, n; void simulate(){ queue<int> q; for(int i=1; i<=n; i++) q.push(i); while(q.size()>1){ cout<<q.front()<<' '; q.pop(); q.push(q.front()); q.pop(); } cout<<q.front()<<endl;} int main(){ cin>>T; for(int t=0; t<T; t++){ cin>>n; simulate(); } return 0;}
点赞 回复 分享
发布于 2019-05-09 20:56
python 用queue做的AC了
点赞 回复 分享
发布于 2019-04-28 19:08
暴力递归了解下,但是我只有45% import java.util.Scanner; public class 纽劢1 {     public static void main(String[] args) throws Exception{         //输入         Scanner sc = new Scanner(System.in);         int t = Integer.parseInt(sc.nextLine());         int[] a = new int[t];         for (int i = 0; i < t; i++) {             a[i] = Integer.parseInt(sc.nextLine());         }         for (int k = 0; k < t; k++) {             int[] m = new int[a[k]];             for (int j = 1; j <= a[k]; j++) {                 m[j - 1] = j;             }             int last = last(m);             System.out.println(last);         }     }     //递归方法     public static int last(int[] m) {         //长度为2时输出,返回         if (m.length == 2) {             System.out.print(m[0] + " ");             return m[1];         }         //否则输出移除的数         System.out.print(m[0] + " ");         int[] s = new int[m.length - 1];         s[s.length - 1] = m[1];         for (int i = 2, j = 0; i <= s.length; i++, j++) {             s[j] = m[i];         }         return last(s);     } }
点赞 回复 分享
发布于 2019-04-14 15:40
C++直接用了数组,在数组里面直接从1到n插入数据,取数据放到新的数组,插入到最后就把当前数据插入到最后位置,然后i++,一直到i+1等于数组长度。
点赞 回复 分享
发布于 2019-04-06 04:22
队列模拟不是On嘛
点赞 回复 分享
发布于 2019-04-05 05:23
import java.util.Scanner; import java.util.ArrayList; public class Main{     public static void main(String[] args){         Scanner sc = new Scanner(System.in);         int len = sc.nextInt();         for(int i = 0; i < len; i++){             int nums = sc.nextInt();             getResult(nums);             System.out.println();         }     }          public static void getResult(int nums){         ArrayList<Integer> temp = new ArrayList<Integer>();         for(int i = 1; i <= nums; i++){             temp.add(i);         }         while(temp.size() >= 3){             System.out.print(temp.get(0) + " ");             temp.add(temp.get(1));             temp.remove(0);             temp.remove(0);         }         for(Integer i : temp){             System.out.print(i + " ");         }     } }
点赞 回复 分享
发布于 2019-04-05 01:45
queue模拟即可
点赞 回复 分享
发布于 2019-04-05 01:01
刚才做完就是简单模拟
点赞 回复 分享
发布于 2019-04-05 01:00
我用队列 没通过 哎
点赞 回复 分享
发布于 2019-04-05 00:43

相关推荐

评论
点赞
2
分享

创作者周榜

更多
牛客网
牛客企业服务