第四章-结构体

4.1 scores
这里写图片描述

#include<iostream>  
#include<stdio.h>  
#include<string>  
using namespace std;  

struct student  
{  
    int classnum;  
    int number;  
    int score;  
}stu[2010];  

int main()  
{  

    int i,j;  
    int n;  
    cin >>n;  

    for(i = 0;i<n;i++)  
        cin >> stu[i].classnum >>stu[i].number>>stu[i].score;  

    int max = 0;  
    for(i = 0;i<n;i++)  
    {  
        for(j = i+1;j<n;j++)  
        {  
            if(stu[j].score>stu[i].score)  
            {  
                student t;  
                t = stu[j];  
                stu[j] = stu[i];  
                stu[i] = t;  
            }  
            else if(stu[j].score == stu[i].score && stu[j].classnum<stu[i].classnum)  
            {  
                student t;  
                t = stu[j];  
                stu[j] = stu[i];  
                stu[i] = t;  
            }  
            else if(stu[j].score == stu[i].score && stu[j].classnum == stu[i].classnum  
                && stu[j].number < stu[i].number)  
            {  
                student t;  
                t = stu[j];  
                stu[j] = stu[i];  
                stu[i] = t;  
            }     
        }  
    }     
    for(i = 0;i<n;i++)  
    {  
        cout <<stu[i].classnum<<" "<<stu[i].number<<" "<<stu[i].score<<endl;  
    }  

    return 0;  
}  

4.2 milk
这里写图片描述

#include<iostream>  
#include<stdio.h>  
#include<string>  
using namespace std;  

struct drink  
{  
    string name;  
    double price;  
    int ml;  
    double re;   
}milk[10086];  

int main()  
{  

    int i,j;  
    int n;  
    cin >>n;   
    int t;  
    for(i = 0; i < n; i++)  
    {  
        cin >> milk[i].name >> milk[i].price >> milk[i].ml;  
        t = milk[i].ml/200;  
        if(t < 5)  
            milk[i].re = (double)t/milk[i].price;  
        else  
            milk[i].re = 5.0/milk[i].price;  
    }  

    double max = milk[0].re;  
    int maxnum = 0;  
    for(i = 0;i < n;i++)  
    {  
        if(milk[i].re > max)  
        {  
            max = milk[i].re;  
            maxnum = i;  
        }  
        if(milk[i].re == max)  
        {  
            if(milk[i].ml > milk[maxnum].ml)  
                maxnum = i;  
        }  
    }  

    cout<< milk[maxnum].name<<endl;  
    return 0;     
}  

4.3 circle
这里写图片描述

#include<iostream>  
#include<stdio.h>  
#include<string>  
using namespace std;  

struct people  
{  
    int b; //前一人   
    int bb; //前前一人   
}per[30030];  

int main()  
{  

    int i,j;  
    int n;  
    cin >> n;  
    int num;  
    for(i = 0; i < n; i++)  
    {  
        cin >> num;  
        cin >> per[num].bb;  
    }  

    int p1,p2;  
    p1 = 1;  
    p2 = 1;  

    for(i = 1; i <= (n+1)/2; i++)  
    {  
        p1 = per[p1].bb;  
    }  

    for(i = 1; i <= (n+1)/2; i++)  
    {  
        per[p2].b = p1;  
        per[p1].b = per[p2].bb;  
        p1 = per[p1].bb;  
        p2 = per[p2].bb;  
    }  

    int m;  
    cin >> m;  
    for(i = 0; i < m; i++)  
    {  
        cin >> num;  
        cout << per[num].b << endl;  
    }  

    return 0;  
}  

4.4 races
这里写图片描述

#include<iostream>  
#include<stdio.h>  
#include<string>  
#include<stdlib.h>  
#include<algorithm>  
using namespace std;  

struct race  
{  
    string name;  
    int number;  
    int time;  
}per[1010];  

bool compare(const race &p1,const race &p2)  
{  
    if(p1.number != p2.number)  
        return p1.number > p2.number;  
    else if(p1.time != p2.time)  
        return p1.time < p2.time;  
    else  
        return p1.name < p2.name;  
}  

int main()  
{  

    int i,j;  
    int n,pp;  
    cin >> n >> pp;  
    string s;  
    int num;  
    int num2;  
    for(i = 1; i <= n; i++)  
    {  
        cin >> per[i].name;  
        per[i].number = 0;  
        per[i].time = 0;  

        int p;  
        for(j = 0; j < 5; j++)  
        {  
            cin >> s;  
            p = s.find('/');   

            if(s[p-1] == '-' || s[p+1] == '-')  
                continue;  

            per[i].number++;  
            num = s[0] - '0';  
            for(int k = 1; k < p; k++)  
            {  
                num *= 10;  
                num += s[k] - '0';  
            }//num为提交次数   
        //  cout << " "<<num <<endl;  

            num2 = s[p+1] - '0';  
            for(int m = p+2; m < s.length(); m++)  
            {  
                num2 *= 10;   
                num2 += s[m] - '0';  
            }//num2为ac耗时          
        //  cout << " "<<num2 <<endl;  

            per[i].time += ((num-1) * pp + num2);  
        //  cout <<" "<<per[i].time <<endl;  
        }         
    }  

    sort(per+1,per+n+1,compare);  

    for(i = 1; i <= n; i++)  
    {  
        cout << per[i].name <<" " <<per[i].number <<" "<< per[i].time <<endl;  
    }  

    return 0;  
}  

4.5 out
这里写图片描述

#include<iostream>  
#include<stdio.h>  
#include<string>  
#include<algorithm>  
using namespace std;  

struct person  
{  
    string name;  
    int number;  
    int behind;  
}per[1010];  

int arr[1010];  
string pername[1010];  
int main()  
{  

    int i,j;  
    int n;  
    cin >> n;  
    for(i = 0; i < n; i++)  
        cin >>per[i].name >>per[i].number >>per[i].behind;  

    for(i = 0; i < n; i++)  
        if(per[i].behind == 0)   
        {  
            arr[0] = per[i].number;  
            pername[0] = per[i].name;  
        }  
    //arr为按排队顺序的编号   

    int len = 0;  
    while(len < n-1)  
    {  
        for(i = 0; i < n; i++)  
        {  
            if(per[i].behind == arr[len])   
            {  
                arr[++len] = per[i].number;  
                pername[len] = per[i].name;  
            }  
        }  
    }  
//  for(i = 0; i < n;i++)  
//      cout <<arr[i]<<" ";  

    int m;  
    cin >> m;  
    int num;  
    for(i = 0; i < m;i++)  
    {  
        cin >> num;  
        arr[num-1] = 0;//离队   
        for(j = num-1; j < n; j++)  
        {  
            arr[j] = arr[j+1];  
            pername[j] = pername[j+1];  
        }   
    }  

    for(i = 0; i < n; i++)  
    {  
        if(arr[i])  
        {  
            cout << pername[i]<<endl;  
        }  
    }  

    return 0;  
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值