作业代码《算法笔记》2.8小节—C/C++快速入门->结构体(struct)的使用

Problem A: C语言11.1

#include<cstdio>
#include<cstring>
struct person {
    char name[20];
    int count;
}leader[3] = {"Li", 0, "Zhang", 0, "Fun", 0};
int main()
{
   int n,i=0;
   char str[20];
   scanf("%d",&n);
   while(i<n){
       scanf("%s",str);
       if(strcmp(str,"Li")==0){leader[0].count++;}
       if(strcmp(str,"Zhang")==0){leader[1].count++;}
       if(strcmp(str,"Fun")==0){leader[2].count++;}
       i++;
   }
   for(i=0;i<3;i++){
       printf("%s:%d\n",leader[i].name,leader[i].count);
   }
    return 0;
}

Problem A: C语言11.2

#include <cstdio>

struct student {
    int num;
    char name[20];
    char sex;
    int age;
}stu[20];
int main() {
    int n;
    scanf(" %d", &n);
    for(int i=0;i<n;i++){
        scanf("%d %s %c %d", &stu[i].num, stu[i].name, &stu[i].sex, &stu[i].age);
    }
    for (int i = 0; i < n; i++) {
        printf("%d %s %c %d\n", stu[i].num, stu[i].name, stu[i].sex, stu[i].age);
    }
    
}

Problem A: C语言11.4

#include <cstdio>

struct a{
    int num;
    char name[10];
    char sex;
    char job;
    union {
        int clas;
        char position[10];
    }category;
};

int main() {
    int n;
    scanf("%d", &n);
    a cat[100];
    for (int i = 0; i < n; i++) {
        scanf("%d %s %c %c", &cat[i].num, cat[i].name, &cat[i].sex, &cat[i].job);
        if (cat[i].job == 's')scanf("%d", &cat[i].category.clas);
        else scanf("%s", cat[i].category.position);
    }
    for (int i = 0; i < n; i++) {
        printf("%d %s %c %c", cat[i].num, cat[i].name, cat[i].sex, cat[i].job);
        if (cat[i].job == 's')printf(" %d\n", cat[i].category.clas);
        else printf(" %s\n", cat[i].category.position);
    }
    return 0;
    
}

*Problem A: C语言11.7

#include <cstdio>

struct student {
    int num;
    char name[20];
    int a,b,c;
}stu[5];
void input(student &stu) {
    scanf("%d %s %d %d %d", &stu.num, stu.name, &stu.a, &stu.b, &stu.c);
}
void print(student &stu) {
    printf("%d %s %d %d %d\n", stu.num, stu.name, stu.a, stu.b, stu.c);
}
int main() {
    for (int i = 0; i < 5; i++) {
        input(stu[i]);
   }
    for (int i = 0; i < 5; i++) {
        print(stu[i]);
    }
    return 0;
    
}

*Problem A: C语言11.8

#include <cstdio>

struct student {
    int num;
    char name[20];
    int a,b,c;
}stu[10];
void input(student &stu) {
    scanf("%d %s %d %d %d", &stu.num, stu.name, &stu.a, &stu.b, &stu.c);
}
void print(student &stu) {
    printf("%d %s %d %d %d\n", stu.num, stu.name, stu.a, stu.b, stu.c);
}
int main() {
    int x=0,y=0,z=0,score = 0,m = 0;
        
    for (int i = 0; i < 10; i++) {
        input(stu[i]);
   }
    for (int i = 0; i < 10; i++) {
        x = x + stu[i].a;
        y = y + stu[i].b;
        z = z + stu[i].c;
        if (score < (stu[i].a + stu[i].b + stu[i].c)) {
            score = stu[i].a + stu[i].b + stu[i].c;
            m = i;
        }
    }
    printf("%.2f %.2f %.2f\n", (double)x / 10.0, (double)y / 10.0, (double)z / 10.0);
    print(stu[m]);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值