• C_结构体_笔记


    定义结构体:

    1 struct Node { 
    2     int num;                            
    3     char name[20];                      
    4     double score[3];
    5     double average;
    6 }

    struct Node { char name[10];int num;}N[10];

      等价于

    struct Node { char name[10];int num;};
    struct Node N[10];    //C
    Node N[10];            //C++

    题目1  10个学生 学号姓名三门课程成绩 输入10个学生数据(改为文件输入;输出学号姓名三门课程平均成绩最高的学生信息

    代码:(编译环境为Visual Studio C++)

     1 #include <cstdio>
     2 #include <algorithm>
     3 
     4 using namespace std;
     5 
     6 struct Node {                           //typedef struct node{}N;
     7     int num;                            //typedef 是将struct封装成N   (C)
     8     char name[20];                      //typedef long long ll;使得ll等价于long long
     9     double cj[3];
    10     double pj;
    11 }xx[10];                 //直接声明结构体,为了避免指针,可以定义成全局变量
    12 
    13 bool cmp(Node p, Node q) {              //写成Nodw& p会比较好
    14     if (p.pj == q.pj)
    15 //        if()return strcmp
    16         return p.num < q.num;           //跳出函数返回bool
    17     return p.pj > q.pj;
    18 }
    19 
    20 int main() {
    21 //    struct Node xx[10];
    22     int i;
    23 //    b.name = "tom";          //.的优先级最高
    24 //    e.age++;                 //age=17
    25     freopen("in.txt", "r", stdin);
    26     freopen("out.txt", "w", stdout);
    27     for (i = 0; i < 10; i++) {
    28         scanf_s("%d %s", &xx[i].num, xx[i].name,sizeof(xx[i].name));  //(?没有为格式字符串传递足够的参数)VSC++要求scanf_s时在使用%c和%s读入字符或字符串时,应在地址参数后附加一个缓冲区边界值。
    29         for (int j = 0; j < 3; j++) {
    30             scanf_s("%lf", &xx[i].cj[j]);
    31         }
    32         xx[i].pj = (xx[i].cj[1] + xx[i].cj[2] + xx[i].cj[3]) / 3;
    33     }
    34     sort(xx, xx + 10, cmp);                   
    35     printf("%d %s %lf %lf %lf\n %lf\n", xx[0].num, xx[0].name, xx[0].cj[1], xx[0].cj[2], xx[0].cj[3], xx[0].pj);
    36 
    37     return 0;
    38 }

     

    题目2(CF456 Div.2 A)Laptops

      One day Dima and Alex had an argument about the price and quality of laptops. Dima thinks that the more expensive a laptop is, the better it is. Alex disagrees. Alex thinks that there are two laptops, such that the price of the first laptop is less (strictly smaller) than the price of the second laptop but the quality of the first laptop is higher (strictly greater) than the quality of the second laptop.

      Please, check the guess of Alex. You are given descriptions of n laptops. Determine whether two described above laptops exist.

      Input

        The first line contains an integer n (1 ≤ n ≤ 105) — the number of laptops.

        Next n lines contain two integers each, ai and bi (1 ≤ ai, bi ≤ n), where ai is the price of the i-th laptop, and bi is the number that represents the quality of the i-th laptop (the larger the number is, the higher is the quality).

        All ai are distinct. All bi are distinct.

      Output

        If Alex is correct, print "Happy Alex", otherwise print "Poor Alex" (without the quotes).

    思路  先按价格对结构体进行排序,然后比较排序后电脑的配置参数如果按递增排列说明Alex的假设不存在,如果配置参数不是递增则说明假设存在。

    AC代码:

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    
    using namespace std;
    
    struct Computer{
        int price;
        int num;
    }c[100000];
    
    bool cmp(Computer a,Computer b){
        return a.price<b.price;
    }
    
    int main(){
        int n,i;
    
        scanf("%d",&n);
        for(i=0;i<n;i++)
            scanf("%d %d",&c[i].price,&c[i].num);
        sort(c,c+n,cmp);
        for(i=0;i<n-1;i++){
            if(c[i].num>c[i+1].num){
                printf("Happy Alex\n");
                break;
            }
        }
        if(i==n-1)printf("Poor Alex\n");
        return 0;
    }

    其他:

    #define fi(x) for(int i=0;i<x;i++)    //不建议用的偷懒办法233
    
    //memset不能填1...是按四位来改的...可以改成-1...ORZ
    // inf=0x3f3f3f3f          32位,inf+inf=inf
  • 相关阅读:
    CF1439E
    CF1446
    CSP2020 游记
    CF1442
    CF1444E
    CF1444
    CF850F Rainbow Balls
    A
    uoj266[清华集训2016]Alice和Bob又在玩游戏(SG函数)
    loj536「LibreOJ Round #6」花札(二分图博弈)
  • 原文地址:https://www.cnblogs.com/anonym/p/8271341.html
Copyright © 2020-2023  润新知