2025年北京大学计算机考研复试机试真题(附 AC 代码 + 解题思路)
2026/6/18 2:15:44 网站建设 项目流程

2025年北京大学计算机考研复试机试真题

2025年北京大学计算机考研复试上机真题

历年北京大学计算机考研复试上机真题

历年北京大学计算机考研复试机试真题

更多学校题目开源地址:https://gitcode.com/verticallimit1/noobdream

N 诺 DreamJudge 题库:输入 “学校名称” 即可筛选该校历年机试真题,题目均在考纲范围内,按难度自动排序。还可搭配《计算机考研机试攻略》刷题,书中题目可通过题号直接在题库中查找。

整数奇偶排序

题目描述

Time Limit: 1000 ms
Memory Limit: 256 mb

输入10个整数,彼此以空格分隔。重新排序以后输出(也按空格分隔),要求: 1.先输出其中的奇数,并按从大到小排列; 2.然后输出其中的偶数,并按从小到大排列。

输入输出格式
输入描述:

任意排序的10个整数(0~100),彼此以空格分隔。

输出描述:

可能有多组测试数据,对于每组数据,按照要求排序后输出,由空格分隔。 1. 测试数据可能有很多组,请使用while(cin>>a[0]>>a[1]>>...>>a[9])类似的做法来实现; 2. 输入数据随机,有可能相等。

输入输出样例
输入样例#:
4 7 3 13 11 12 0 47 34 98
输出样例#:
47 13 11 7 3 0 4 12 34 98

代码一

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5. int main() {
  6. int a[10];
  7. while (cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4] >> a[5] >> a[6] >> a[7] >> a[8] >> a[9]) {
  8. vector<int> odds, evens;
  9. for (int i = 0; i < 10; ++i) {
  10. if (a[i] % 2 != 0) {
  11. odds.push_back(a[i]);
  12. } else {
  13. evens.push_back(a[i]);
  14. }
  15. }
  16. sort(odds.begin(), odds.end(), greater<int>());
  17. sort(evens.begin(), evens.end());
  18. for (int num : odds) {
  19. cout << num << " ";
  20. }
  21. for (int num : evens) {
  22. cout << num << " ";
  23. }
  24. cout << endl;
  25. }
  26. return 0;
  27. }

代码二

  1. import java.util.*;
  2. public class Main{
  3. public static void main(String[] args){
  4. Scanner sc=new Scanner(System.in);
  5. while(sc.hasNextInt()){
  6. Integer[] a=new Integer[10];
  7. for(int i=0;i<10;i++){
  8. a[i]=sc.nextInt();
  9. }
  10. Arrays.sort(a,new Comparator<Integer>(){
  11. public int compare(Integer a,Integer b){
  12. if((a%2!=0) && (b%2==0)) return -1;
  13. else if((a%2==0) && (b%2!=0)) return 1;
  14. else if((a%2!=0) && (b%2!=0)) return b-a;
  15. else return a-b;
  16. }
  17. });
  18. for(int i=0;i<10;i++){
  19. if(i!=9) System.out.print(a[i]+" ");
  20. else System.out.println(a[i]);
  21. }
  22. }
  23. sc.close();
  24. }
  25. }

代码三

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. bool cmp(int a,int b){
  4. if((a%2)!=(b%2)){
  5. return (a%2)>(b%2);
  6. }
  7. else if(((a%2)==1)&&((b%2)==1)){
  8. return a>b;
  9. }
  10. else if(((a%2)==0)&&((b%2)==0)){
  11. return a<b;
  12. }
  13. }
  14. int main(){
  15. vector<int> a(10);
  16. while(cin>>a[0]>>a[1]>>a[2]>>a[3]>>a[4]>>a[5]>>a[6]>>a[7]>>a[8]>>a[9]){
  17. sort(a.begin(),a.end(),cmp);
  18. for(int i=0;i<a.size();i++){
  19. cout<<a[i]<<" ";
  20. }
  21. }
  22. return 0;
  23. }

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询