3.2 查找元素

題號 題目 分數
B1041 考試座位號 15
B1004 成績排名 20
B1028 人口普查 20
B1032 挖掘機技術哪家強 20
A1011 World Cup Betting 20
A1006 Sign In and Sign Out 25
A1036 Boys vs Girls 25
B1041. 考試座位號
#include <cstdio>
const int MAXN = 1010;

typedef struct Student
{
    long long id;
    int examSeat;
}Student;

int main()
{
    int n, seat, examSeat;
    long long id;
    Student testSeat[MAXN];

    scanf("%d", &n);
    for(int i = 0; i < n; ++i)
    {
        scanf("%lld %d %d", &id, &seat, &examSeat);
        testSeat[seat].id = id;
        testSeat[seat].examSeat = examSeat;
    }

    int m;
    scanf("%d", &m);
    for(int i = 0; i < m; ++i)
    {
        scanf("%d", &seat);
        printf("%lld %d\n", testSeat[seat].id, testSeat[seat].examSeat);
    }

    return 0;
}
B1004. 成績排名
#include <cstdio>

struct Student
{
    char name[15];
    char id[15];
    int score;
}tmp, ansMax, ansMin;

int main()
{
    int n;
    ansMax.score = -1;
    ansMin.score = 101;
    scanf("%d", &n);
    for(int i = 0; i < n; ++i)
    {
        scanf("%s %s %d", tmp.name, tmp.id, &tmp.score);
        if(tmp.score > ansMax.score)
            ansMax = tmp;
        if(tmp.score < ansMin.score)
            ansMin = tmp;
    }
    printf("%s %s\n", ansMax.name, ansMax.id);
    printf("%s %s\n", ansMin.name, ansMin.id);
    return 0;
}
B1028. 人口普查
注意點:寫代碼一定要認真!!!oldest和youngest不要搞混,oldest的出生日期更小,youngest的出生日期更大。
有可能存在所有人的日期都不在合法區間內的情況,這時必須特判輸出爲0,否則會返回“格式錯誤”.
#include <cstdio>
struct Person
{
    char name[10];
    int yy, mm, dd;
}left, right, youngest, oldest;

void init()
{
    youngest.yy = left.yy = 1814;
    oldest.yy = right.yy = 2014;
    oldest.mm = youngest.mm = left.mm = right.mm = 9;
    oldest.dd = youngest.dd = left.dd = right.dd = 6;
}

bool lessEqu(Person a, Person b)
{
    if(a.yy != b.yy)
        return a.yy <= b.yy;
    else if(a.mm != b.mm)
        return a.mm <= b.mm;
    else
        return a.dd <= b.dd;
}

bool moreEqu(Person a, Person b)
{
    if(a.yy != b.yy)
        return a.yy >= b.yy;
    else if(a.mm != b.mm)
        return a.mm >= b.mm;
    else
        return a.dd >= b.dd;
}



int main()
{
    init();
    int n;
    scanf("%d", &n);

    Person tmp;
    int num = 0;
    for(int i = 0; i < n; ++i)
    {
        scanf("%s", tmp.name);
        scanf("%d/%d/%d", &tmp.yy, &tmp.mm, &tmp.dd);
        if(lessEqu(tmp, right) && moreEqu(tmp, left))
        {
            num += 1;
            if(lessEqu(tmp, oldest))
                oldest = tmp;
            if(moreEqu(tmp, youngest))
                youngest = tmp;
        }
    }

    if(0 == num)
        printf("0\n");
    else
        printf("%d %s %s", num, oldest.name, youngest.name);
    return 0;
}
B1032. 挖掘機技術哪家強
#include <cstdio>

const int MAXN = 100010;
int school[MAXN];

int main()
{
    int n;
    scanf("%d", &n);

    int id, score;
    for(int i = 0; i < n; ++i)
    {
        scanf("%d %d", &id, &score);
        school[id] += score;
    }

    int maxScore = -1;
    int k;
    for(int i = 1; i < MAXN; ++i)
    {
        if(school[i] > maxScore)
        {
            k = i;
            maxScore = school[i];
        }
    }

    printf("%d %d", k, maxScore);
    return 0;
}
A1011. World Cup Betting
注意:用char s = {‘W’, ‘T’, ‘L’}數組表示比賽結果比利用if/else來輸出W/T/L更方便。
#include <cstdio>

char s[3] = {'W', 'T', 'L'};

int main()
{
    double ans = 1.0, max1;
    int maxID;
    double tmp;

    for(int i = 0; i < 3; ++i)
    {
        max1 = -1;
        for(int j = 0; j < 3; ++j)
        {
            scanf("%lf", &tmp);
            if(tmp > max1)
            {
                max1 = tmp;
                maxID = j;
            }
        }
        printf("%c ", s[maxID]);
        ans *= max1;
    }
    printf("%.2f", (ans * 0.65 - 1) * 2);
    return 0;
}
A1006. Sign In and Sign Out
#include <cstdio>

struct Student
{
    char id[16];
    int hh, mm, ss;
}signIn, signOut, tmp;

void init()
{
    signIn.hh = 24;
    signIn.mm = signIn.ss = 60;
    signOut.hh = signOut.mm = signOut.ss = 0;
}

bool great(Student a, Student b)
{
    if(a.hh != b.hh)
        return a.hh >= b.hh;
    else if(a.mm != b.mm)
        return a.mm >= b.mm;
    return a.ss >= b.ss;
}

int main()
{
    init();
    int n;
    scanf("%d", &n);

    for(int i = 0; i < n; ++i)
    {
        scanf("%s %d:%d:%d", tmp.id, &tmp.hh, &tmp.mm, &tmp.ss);
        if(great(signIn, tmp))
            signIn = tmp;

        scanf("%d:%d:%d", &tmp.hh, &tmp.mm, &tmp.ss);
        if(great(tmp, signOut))
            signOut = tmp;
    }
    printf("%s %s", signIn.id, signOut.id);
    return 0;
}
A1036. Boys vs Girls
#include <cstdio>

struct Student
{
    char name[11];
    char gender;
    char id[11];
    int grade;
}M, F, tmp;

void init()
{
    M.grade = 101;
    M.gender = 'M';
    F.gender = 'F';
    F.grade = -1;
}

int main()
{
    init();
    int n;
    scanf("%d", &n);

    for(int i = 0; i < n; ++i)
    {
        scanf("%s %c %s %d", tmp.name, &tmp.gender, tmp.id, &tmp.grade);
        if('M' == tmp.gender && tmp.grade < M.grade)
            M = tmp;
        if('F' == tmp.gender && tmp.grade > F.grade)
            F = tmp;
    }

    if(F.grade == -1)
        printf("Absent\n");
    else
        printf("%s %s\n", F.name, F.id);

    if(M.grade == 101)
        printf("Absent\n");
    else
        printf("%s %s\n", M.name, M.id);

    if(F.grade != -1 && M.grade != 101)
        printf("%d", F.grade - M.grade);
    else
        printf("NA");
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章