zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    NETCore EF 数据库连接正确nuget和MySql错误异常
    JS 对象属性名排序
    NET 在一个数组中查找另一个数组所在起始位置(下标从0开始,未找到返回-1)
    NET 判断是否为回文
    NET/Regex 处理连续空格
    NET 已知excel表格前面26个是a到z,27是aa28是ab,以此类推,N是多少
    Regex 首字母转大写/小写,全大写,全小写
    .NETCore下访问img、js等静态资源404解决办法
    WPF-后台代码使用Behavior
    Socket-服务器端与客户端互相通信
  • 原文地址:https://www.cnblogs.com/anonym/p/8271341.html
Copyright © 2011-2022 走看看