昨天网易有道java方向赛马网编程1、2题

//两道题都能在本地测试通过,但是一到赛马上就显示测试的所有结果错误,万能的牛友们,能看看程序哪儿出了问题吗?
1、洗牌问题
//每次洗牌开辟一个新的数组来存储洗牌的中间结果
import java.util.Scanner;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
       Scanner cin = new Scanner(System.in);
       int time = cin.nextInt();
       for(int i = 0; i < time; i++) {
       int n = cin.nextInt();
       int k = cin.nextInt();
       int m = n * 2;
       int[] array = new int[m];
       for(int j = 0; j < m; j++) {
       array[j] = cin.nextInt();
       }        
       for(int j = 0; j < k; j++) {
       int[] temp = new int[m];
       for(int l = n - 1,h = m - 1; l >= 0; l--) {
       temp[h--] = array[l + n];
       temp[h--] = array[l];
       }
       array = temp;
       }
       for(int j = 0; j < m - 1; j++) {
       System.out.print(array[j] + " ");
       }
       System.out.println(array[m - 1]);
       }
}
}
2、构造队列
//使用一个双端队列,每次从1-n中从大到小曲一个数放入到双端队列的对头,然后从双端队列的队尾弹出一个数后放到双端队列的对头,这样n次之后,队列中存储的数据的顺序就是原有的所有的数的排列顺序。
import java.util.Scanner;
import java.util.Deque;
import java.util.ArrayDeque;
public class MainThree {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int row = cin.nextInt();
for(int i = 0 ; i < row; i++) {
int n = cin.nextInt();
Deque<Integer> result = new ArrayDeque<>();
for(int j = n; j >= 1; j--) {
result.addFirst(j);
int temp = result.removeLast();
result.addFirst(temp);
}
   Integer[] array = result.toArray(new Integer[result.size()]);
int length = array.length;
for(int j = 0; j < length - 1; j++) {
System.out.print(array[j] + " ");
}
System.out.println(array[length - 1]);
}
}
}

全部评论
不排除oj有问题,昨天我试了一个死循环,也出来wrong answer,应该是tle,而且一个hello world 编译了快一分钟
点赞
送花
回复
分享
发布于 2016-08-18 09:44
时间的限制问题,用java写的同样的代码,通不过,换用C++就可以通过。。时间效率不高啊
点赞
送花
回复
分享
发布于 2016-08-18 15:30
秋招专场
校招火热招聘中
官网直投

相关推荐

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