L1-005 座位号(for,未用结构体的写法)

链接:https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805140211482624?type=7&page=0
题目

#include<stdio.h>
int main()
{
    int i,N,b[1000],c[1000],M,j,K[1000],n;
    long a[1000];
    scanf("%d",&N);
    for(i=0;i<N;i++)
    {
       scanf("%ld %d %d",&a[i],&b[i],&c[i]) ;
    }
    scanf("%d",&M);
    for(j=0;j<M;j++)
    {
        scanf("%d",&K[j]);
        for(i=0;i<N;i++)
        {
            if(K[j]==b[i])
            {
                printf("%ld %d\n",a[i],c[i]);
                break;//break有没有无所谓//
            }
        }
    }
   
    return 0;
}
C语言允许用户自己建立由不同类型数据组成的组合型的数据结构,称为结构体。声明结构体类型时必须使用关键字 `struct`,结构体类型的名字由关键字 `struct` 和结构体名组合而成,如 `struct Student`,它可用于定义变量,如 `struct Student stu1, stu2;` [^1]。 以下是结构体定义的一般写法: ```c // 结构体声明 struct 结构体名 { 数据类型 成员1; 数据类型 成员2; // 可以有更多成员 }; // 定义结构体变量 struct 结构体名 变量名; ``` 示例代码如下: ```c #include <stdio.h> // 定义一个结构体表示学生信息 struct Student { char name[50]; int age; float score; }; int main() { // 定义结构体变量 struct Student stu1; // 给结构体成员赋值 sprintf(stu1.name, "John"); stu1.age = 20; stu1.score = 85.5; // 输出结构体成员的值 printf("Name: %s\n", stu1.name); printf("Age: %d\n", stu1.age); printf("Score: %.2f\n", stu1.score); return 0; } ``` 在上述代码中,首先定义了一个 `struct Student` 结构体,包含 `name`、`age` 和 `score` 三个成员。然后在 `main` 函数中定义了 `struct Student` 类型的变量 `stu1`,并给其成员赋值,最后输出成员的值。 此外,还可以使用 `typedef` 来重定义结构体类型,简化结构体变量的定义,示例如下: ```c #include <stdio.h> // 使用typedef重定义结构体类型 typedef struct { char name[50]; int age; float score; } Student; int main() { // 定义结构体变量,无需再写struct关键字 Student stu1; // 给结构体成员赋值 sprintf(stu1.name, "John"); stu1.age = 20; stu1.score = 85.5; // 输出结构体成员的值 printf("Name: %s\n", stu1.name); printf("Age: %d\n", stu1.age); printf("Score: %.2f\n", stu1.score); return 0; } ``` 这里使用 `typedef` 将 `struct {...}` 定义的结构体类型重命名为 `Student`,在定义变量时就无需再写 `struct` 关键字。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

To be Transcendente

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值