注册 登录
编程论坛 C++教室

编译错误

根根本根 发布于 2018-08-31 12:17, 1744 次点击
http://noi.

06:整数奇偶排序
总时间限制: 1000ms 内存限制: 65536kB
描述
给定10个整数的序列,要求对其重新排序。排序要求:

1.奇数在前,偶数在后;

2.奇数按从大到小排序;

3.偶数按从小到大排序。

输入
输入一行,包含10个整数,彼此以一个空格分开,每个整数的范围是大于等于0,小于等于100。
输出
按照要求排序后输出一行,包含排序后的10个整数,数与数之间以一个空格分开。
样例输入
4 7 3 13 11 12 0 47 34 98
样例输出
47 13 11 7 3 0 4 12 34 98
来源
1873

#include <bits/stdc++.h>
using namespace std;
bool cmp1(int a,int b) {
    if(a>b) {
        return 1;
    }
    return 0;
}
int main() {
    int a[10]= {0},b[10]= {0},n,m;
    for(int i=0; i<10; i++) {
        int t;
        cin>>t;
        if(t%2==0) {
            b[m]=i;
            m++;
        } else {
            a[n]=i;
            n++;
        }
    }
    sort(a,&a[n]);
    sort(b,&b[m],a);
    for(int i=0; i<n; i++) {
        cout<<a[i]<<" ";
    }
    cout<<endl;
    for(int i=0; i<m; i++) {
        cout<<b[i]<<" ";
    }
    return 0;
}
6 回复
#2
根根本根2018-08-31 12:27
找到问题了
 但是这样的代码还是2分
#include <bits/stdc++.h>
using namespace std;
bool cmp1(int a,int b) {
    if(a>b) {
        return 1;
    }
    return 0;
}
int main() {
    int a[10]= {0},b[10]= {0},n=0,m=0;
    for(int i=0; i<10; i++) {
        int t;
        cin>>t;
        if(t%2==0) {
            b[m]=t;
            m++;
        } else {
            a[n]=t;
            n++;
        }
    }
    sort(a,&a[n]);
    sort(b,&b[m],cmp1);
    for(int i=0; i<n; i++) {
        cout<<a[i]<<" ";
    }
    for(int i=0; i<m; i++) {
        cout<<b[i]<<" ";
    }
    return 0;
}
#3
rjsp2018-08-31 13:49
如果你时间不够,就别学编程
等你有时间了,将题目中的“样例输入”输入你的程序中试试,看看是不是和“样例输出”一致。
#4
rjsp2018-08-31 15:06
如果不考虑运行效率的话,就按照题目要求写个比较函数就行了
程序代码:
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;

inline bool cmp( unsigned a, unsigned b )
{
    return a%2==0 ? (b%2==0 ? a<b : false) : (b%2==0 ? true : a>b);
}

int main( void )
{
    unsigned buf[10];
    copy_n( istream_iterator<unsigned>(cin), 10, buf );
    sort( buf, buf+10, &cmp );
    copy( buf, buf+10, ostream_iterator<unsigned>(cout," ") );
}

如果考虑运行效率的话,……
#5
根根本根2018-08-31 15:25
#include <bits/stdc++.h>
using namespace std;
bool cmp1(int a,int b) {
    if(a>b) {
        return 1;
    }
    return 0;
}
int main() {
    int a[10]= {0},b[10]= {0},n=0,m=0;
    for(int i=0; i<10; i++) {
        int t;
        cin>>t;
        if(t%2==0) {
            b[m]=t;
            m++;
        } else {
            a[n]=t;
            n++;
        }
    }
    sort(b,&b[m]);
    sort(a,&a[n],cmp1);
    for(int i=0; i<n; i++) {
        cout<<a[i]<<" ";
    }
    for(int i=0; i<m; i++) {
        cout<<b[i]<<" ";
    }
    return 0;
}
#6
根根本根2018-08-31 15:26
  ×
 ×*×
* * *       通过的代码
  *
    *
  *
#7
汉家萌妹子2018-09-03 21:26
编译错误请把错误信息也贴出来
1