美文网首页
A1055 The World's Richest (25分)

A1055 The World's Richest (25分)

作者: km15 | 来源:发表于2020-01-28 16:37 被阅读0次

// A1055 The World's Richest (25分).cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
题意:
排序+超时排序

learn && wrong;
1、string的比较
2、编译错误,是strcmp(a.id,b.id)没有加上 《 0
3、有一组数据,四个人数,但是符合要求的只有三个,所以就导致输出了四个,所以每次都要重新初始化,需要写一个初试函数
4、有一组数据超时了,每个年龄中财富在前100名以内的人全都存到另一个数组中,后面所有操作均在这个数组中进行,这个操作是的后面K次均不会超时

题意:
输入:
1、人数(10的5次)+查询次数(10的3次)
2、名字(不超过8位+年龄(0到200+资产(10的6次方,正负区间
3、最后是,M个人数,最小,最大,闭区间

输出:
要求输出年龄区间内的最富有,

case #x;
首先资产非递增,相同资产则年龄非递减,再相同则名字非递减

名字+年龄+资产

编程思想:
1、輸入结构体,名字,年龄,资产
2、如何摘除年龄区间里的人呢,遍历,相同则复制,或者说,可以以什么排序先吗,年龄,或者说资产排序,然后输出,都不可行,如果是年龄,你怎么确定第几个到第几个,资产也是啊
3、摘出来后排序,然后输出

*/

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;

struct person {
    char id[10]; //名字
    int age; 
    int weight; //资产
}per[100010],per2[100010];


bool cmp(person a, person b) {
    if (a.weight != b.weight) return a.weight > b.weight;
    else if (a.age != b.age) return a.age < b.age;
    else return strcmp(a.id,b.id) < 0;
}

bool init();

int main()
{
    //————————————————————
    int total_person, num_times; //查询人数,查询次数;
    cin >> total_person >> num_times;
    

    //————————————————————
    for (int i = 0;i < total_person;++i) {  //录入人数
        cin >> per[i].id >> per[i].age >> per[i].weight;//(scanf!!!)没有引用
    }
    

    //————————————————————
    int meigequjian_duoshaoren, amin, amax;//查询的人数,以及年龄区间
    

     //————————————————————
    int times = 1; //输出第几次

    for (int j = 0; j < num_times;++j) {
        int xiabiao = 0; //录入到另一个数组的下标(!!!)每次都要覆盖掉
        
        cin >> meigequjian_duoshaoren >> amin >> amax;
        for (int k = 0;k < total_person;++k) { //录入到另一个结构体数组 (!!!)这里出现了一堆0?所有上界都弄混了
            if (per[k].age >= amin && per[k].age <= amax) {
                per2[xiabiao++] = per[k];
            }
        }

         
        //————————————————————
        sort(per2, per2 + xiabiao,cmp);  //排序輸出

        cout << "Case #" << times++ << ":" << endl;
        if (xiabiao != 0) {
            for (int j = 0;j < meigequjian_duoshaoren;++j) { //输出要求
                printf("%s %d %d\n", per2[j].id, per2[j].age, per2[j].weight); //(!!!)printf输出string用什么格式
            }
        }
        else {
                cout << "None" << endl;
            }
    }
    return 0;
}

相关文章

网友评论

      本文标题:A1055 The World's Richest (25分)

      本文链接:https://www.haomeiwen.com/subject/qlbrthtx.html