SDUT 《程序设计基础(B)II》实验1--结构体、共用体和枚举

《程序设计基础(B)II》实验1–结构体、共用体和枚举

https://acm.sdut.edu.cn/onlinejudge3/contests/3217/overview

A - 检查宿舍卫生

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int s[110];
    int n,i,a,b,c,d,e;
    while(~scanf("%d",&n))
    {
        for(i=0; i<n; i++)
        {
            scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
            s[i]=a+b+c+d+e;
        }
        int f=0,max=0;
        for(i=0; i<n; i++)
        {
            if(s[i]<85)
                f++;
            if(max<s[i])
                max=s[i];
        }
        if(max<85)
            printf("%d No\n",f);
        else
            printf("%d %d\n",f,max);
    }
    return 0;
}

D - 小 I 选宾馆

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int p,w,k;
} a[5050],t;
int main()
{
    int n,i,j;
    while(~scanf("%d",&n))
    {
        for(i=0; i<n; i++)
        {
            scanf("%d %d",&a[i].p,&a[i].w);
            a[i].k=i;
        }
        for(i=0; i<n; i++)
        {
            for(j=i; j<n; j++)
            {
                if(a[i].p<a[j].p)
                {
                    t=a[i];
                    a[i]=a[j];
                    a[j]=t;
                }
                else if(a[i].p==a[j].p&&a[i].w<a[j].w)
                {
                    t=a[i];
                    a[i]=a[j];
                    a[j]=t;
                }
            }
        }
        printf("%d\n",a[0].k+1);
    }
    return 0;
}

I - 共用体练习

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
union
{
    int b;
    double c;
    char d[20];
} s[100010];
int main()
{
    char a[100010][20];
    int n,m,i,t;
    scanf("%d %d",&n,&m);
    for(i=0; i<n; i++)
    {
        scanf("%s",a[i]);
        if(strcmp(a[i],"INT")==0)
            scanf("%d",&s[i].b);
        else if(strcmp(a[i],"DOUBLE")==0)
            scanf("%lf",&s[i].c);
        else if(strcmp(a[i],"STRING")==0)
            scanf("%s",s[i].d);

    }
    for(i=0; i<m; i++)
    {
        scanf("%d",&t);
        if(strcmp(a[t],"INT")==0)
            printf("%d\n",s[t].b);
        else if(strcmp(a[t],"DOUBLE")==0)
            printf("%.2f\n",s[t].c);
        else if(strcmp(a[t],"STRING")==0)
            printf("%s\n",s[t].d);

    }
    return 0;
}

J - 简单枚举类型——植物与颜色

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum
{red, orange, yellow, green, blue, violet,no}
a;
int main()
{
    char s[30];
    while(~scanf("%s",s))
    {
        if(strcmp(s,"red")==0)
            a=red;
        else if(strcmp(s,"orange")==0)
            a=orange;
        else if(strcmp(s,"yellow")==0)
            a=yellow;
        else if(strcmp(s,"green")==0)
            a=green;
        else if(strcmp(s,"blue")==0)
            a=blue;
        else if(strcmp(s,"violet")==0)
            a=violet;
        else
            a=no;
        switch(a)
        {
        case 0:
            printf("Rose are red.\n");
            break;
        case 1:
            printf("Poppies are orange.\n");
            break;
        case 2:
            printf("Sunflower are yellow.\n");
            break;
        case 3:
            printf("Grass are green.\n");
            break;
        case 4:
            printf("Bluebells are blue.\n");
            break;
        case 5:
            printf("Violets are violet.\n");
            break;
        default:
            printf("I don't know about the color %s.\n",s);
        }
    }
    return 0;
}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值