码迷,mamicode.com
首页 > 编程语言 > 详细

Openjudge 1.10 简单排序

时间:2019-12-05 20:25:15      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:algo   数组   pre   namespace   lse   return   for   老年   编写   

1.10.6 奇偶排列

我的读入一行方式很奇怪,应该只能用于这种给定长度的数组了(急需补习...)

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio> 
#include <cmath>
using namespace std;
int c[15];
bool cmp(int x,int y){
    if(x%2==0&&y%2==0) return x<y;
    if(x%2!=0&&y%2!=0) return x>y;
    if(x%2!=0&&y%2==0) return 1;
    if(x%2==0&&y%2!=0) return 0;
}
int main( ){
    cin>>c[1]>>c[2]>>c[3]>>c[4]>>c[5]>>c[6]>>c[7]>>c[8];
    cin>>c[9]>>c[10];
    sort(c+1,c+11,cmp);
    for(int i=1;i<=10;i++){
        printf("%d ",c[i]);
    }
    return 0;
}

1.10.7 合照效果

cmp作用好大

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
struct m{
    char s[7];
    int num;
    double tall;
    bool wet;//1 male 0 female
}pep[41];
struct ma{
    double tal;
    bool we;
}man[41],woman[41];
bool cmp(ma x,ma y){
    if(x.we==1&&y.we==1) return x.tal<y.tal;
    if(x.we==0&&y.we==0) return x.tal>y.tal;
}
int main( ){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%s",pep[i].s);
        scanf("%lf",&pep[i].tall);
        pep[i].num=i;
        if(pep[i].s[0]=='m') pep[i].wet=1;
        else pep[i].wet=0;
    }
    int pp=1,qq=1;
    for(int i=1;i<=n;i++){
        if(pep[i].wet==1) {
            man[pp].tal=pep[i].tall;
            man[pp].we=1;
            //printf("%.2lf %d %d\n",man[pp].tal,pp,man[pp].we);
            pp++;
        }
        else if(pep[i].wet==0) {
            woman[qq].tal=pep[i].tall;
            woman[qq].we=0;
            //printf("%.2lf %d %d\n",woman[qq].tal,qq,woman[qq].we);
            qq++;
        }
    }
    pp--;qq--;
    //printf("pp=%d qq=%d\n",pp,qq);
    sort(man+1,man+pp+1,cmp);
    sort(woman+1,woman+qq+1,cmp);
    
    //printf("tot=%d sum=%d r=%d\n",tot,sum,r);
    for(int i=1;i<=pp;i++){
        printf("%.2lf ",man[i].tal);
    }
    for(int i=1;i<=qq;i++){
        printf("%.2lf ",woman[i].tal);
    }
    return 0;
} 

1.10.8 病人排队

cmp写的不好,一开始4分

病人登记看病,编写一个程序,将登记的病人按照以下原则排出看病的先后顺序:

  1. 老年人(年龄 >= 60岁)比非老年人优先看病。

  2. 老年人按年龄从大到小的顺序看病,年龄相同的按登记的先后顺序排序。

\(2\)条没写好

  1. 非老年人按登记的先后顺序看病。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
struct c{
    char s[15];
    int age;
    int num;
}pep[105];
bool cmp(c x,c y){
    if(x.age<60&&y.age<60) return x.num<y.num;
    else if(x.age>=60&&y.age>=60&&x.age==y.age) return x.num<y.num;
    return x.age>y.age;
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%s",pep[i].s);
        scanf("%d",&pep[i].age);
        pep[i].num=i;
    }
    sort(pep+1,pep+n+1,cmp);
    for(int i=1;i<=n;i++){
        printf("%s\n",pep[i].s);
    }
    return 0;
}

1.10.9 随机数

WA 9分 我也不知道为什么

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> 
using namespace std;
int a[105];
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++){
        for(int j=2;j<=n;j++){
            if(a[j]<=a[j-1]) swap(a[j],a[j-1]);
            if(a[j]==a[j-1]){
                a[j]=0;
                for(int l=j;l<n;l++){
                    a[l]=a[l+1];
                    a[l+1]=0;
                }
                n--;
            }
        }
    }
    printf("%d \n",n);
    for(int i=1;i<=n;i++){
        printf("%d ",a[i]);
    }
    return 0;
}

Openjudge 1.10 简单排序

标签:algo   数组   pre   namespace   lse   return   for   老年   编写   

原文地址:https://www.cnblogs.com/liuziwen0224/p/11991540.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!