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
代码一
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- int main() {
- int a[10];
- while (cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4] >> a[5] >> a[6] >> a[7] >> a[8] >> a[9]) {
- vector<int> odds, evens;
- for (int i = 0; i < 10; ++i) {
- if (a[i] % 2 != 0) {
- odds.push_back(a[i]);
- } else {
- evens.push_back(a[i]);
- }
- }
- sort(odds.begin(), odds.end(), greater<int>());
- sort(evens.begin(), evens.end());
- for (int num : odds) {
- cout << num << " ";
- }
- for (int num : evens) {
- cout << num << " ";
- }
- cout << endl;
- }
- return 0;
- }
代码二
- import java.util.*;
- public class Main{
- public static void main(String[] args){
- Scanner sc=new Scanner(System.in);
- while(sc.hasNextInt()){
- Integer[] a=new Integer[10];
- for(int i=0;i<10;i++){
- a[i]=sc.nextInt();
- }
- Arrays.sort(a,new Comparator<Integer>(){
- public int compare(Integer a,Integer b){
- if((a%2!=0) && (b%2==0)) return -1;
- else if((a%2==0) && (b%2!=0)) return 1;
- else if((a%2!=0) && (b%2!=0)) return b-a;
- else return a-b;
- }
- });
- for(int i=0;i<10;i++){
- if(i!=9) System.out.print(a[i]+" ");
- else System.out.println(a[i]);
- }
- }
- sc.close();
- }
- }
代码三
- #include<bits/stdc++.h>
- using namespace std;
- bool cmp(int a,int b){
- if((a%2)!=(b%2)){
- return (a%2)>(b%2);
- }
- else if(((a%2)==1)&&((b%2)==1)){
- return a>b;
- }
- else if(((a%2)==0)&&((b%2)==0)){
- return a<b;
- }
- }
- int main(){
- vector<int> a(10);
- while(cin>>a[0]>>a[1]>>a[2]>>a[3]>>a[4]>>a[5]>>a[6]>>a[7]>>a[8]>>a[9]){
- sort(a.begin(),a.end(),cmp);
- for(int i=0;i<a.size();i++){
- cout<<a[i]<<" ";
- }
- }
- return 0;
- }