C语言-结构体

这些代码片段展示了使用C语言处理不同数据结构,如结构体,进行数据排序和比较的场景。包括检查宿舍卫生评分、计算个人综合评分、选票统计、选宾馆、排名次、最终排名、选择配偶条件筛选以及基于质价比和枚举类型的植物颜色匹配等算法应用。
摘要由CSDN通过智能技术生成

7-1 检查宿舍卫生

 代码:

#include <stdio.h>
struct score
{
    int a[5];
};
int main()
{
    int n,sum,count=0,max=-1,i,j;
    scanf("%d\n",&n);
    struct score b[100];
    for(j=0;j<n;j++)
    {
        for(i=0;i<n;i++)
        {
            int x;
            scanf("%d",&b[j].a[i]);
            sum=b[j].a[0]+b[j].a[1]+b[j].a[2]+b[j].a[3]+b[j].a[4];
        }
        if(sum<85)
            count++;
        else
            if(sum>max)
            max=sum;
                sum=0;
    }
    if(count==n)
        printf("%d No\n",count);
    else
        printf("%d %d\n",count,max);
    return 0;
}

7-2 小 I 的小姐姐

代码:

#include <stdio.h>
struct score
{
    int w1,w2,w3;
}a[1010];
int main()
{
    int n,j,flag;
    double max=-1,sum=0;
    scanf("%d",&n);
    struct score b[1010];
    for(j=0;j<n;j++)
    {
        scanf("%d%d%d",&b[j].w1,&b[j].w2,&b[j].w3);
        sum=0.7*b[j].w1+0.2*b[j].w2+0.1*b[j].w3;
        if(sum>max)
        {
            max=sum;
            flag=j;
        }
    }
    printf("%d",flag);
    return 0;
}

7-3 选票统计

代码:

#include <stdio.h>
#include <stdlib.h>
struct
{
    int piao;
}a[1001];
int main()
{
    int i,m,n,x;
    scanf("%d %d",&m,&n);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&x);
        a[x].piao++;
    }
    int k,max=0;
    for(i=1;i<=m;i++)
    {
        if(a[i].piao>max)
        {
            max=a[i].piao;
            k=i;
        }
    }
    printf("%d\n%d\n",k,max);
    return 0;
}

7-4 小 I 选宾馆

代码:

 #include <stdio.h>
#include <stdlib.h>
struct binguan
{
    int a;
    int b;
    int c;
} st[5001],t;
int main()
{
    int n,i,j;
    while(scanf("%d",&n)!=EOF)
    {
        for(i=1; i<=n; i++)
        {
            scanf("%d %d",&st[i].a,&st[i].b);
            st[i].c=i;
        }
        for(i=1; i<=n-1; i++)
        {
            for(j=1; j<=n-i; j++)
            {
                if(st[j].a<st[j+1].a)
                {
                    t=st[j];
                    st[j]=st[j+1];
                    st[j+1]=t;
                }
                if(st[j].a==st[j+1].a)
                {
                    if(st[j].b<st[j+1].b)
                    {
                        t=st[j];
                        st[j]=st[j+1];
                        st[j+1]=t;
                    }

                }
            }
        }
        printf("%d\n",st[1].c);
    }
    return 0;
}

7-5 小鑫の日常系列故事(十)——排名次

代码:

#include<stdio.h>  
#include<string.h>  
int main()  
{  
    int n,i,j,gra[51],t;   
    char a[51][51],b[51];  
    scanf("%d",&n);  
    for(i=0;i<n;i++)  
    {  
        scanf("%s",a[i]);  
        scanf("%d",&gra[i]);  
    }  
    for(i=0;i<n;i++)  
    {  
       for(j=0;j<n-1-i;j++)  
       {  
           if(gra[j]<gra[j+1])  
           {  
               t=gra[j],gra[j]=gra[j+1],gra[j+1]=t;  
               strcpy(b,a[j]);  
               strcpy(a[j],a[j+1]);  
               strcpy(a[j+1],b);  
           }  
       }  
    }  
    for(i=0;i<n;i++)  
    {  
        printf("%s %d\n",a[i],gra[i]);  
    }  
    return 0;  
} 

7-6 最终排名

代码:

#include<stdio.h>
#include<stdlib.h>
struct 
{
    int id;
    int num;
}a[100001];
int main()
{
    int  n,i,j,temp,t;
    while(scanf("%d",&n)!=EOF)
    {
        for(i=0;i<n;i++)
        {
            scanf("%d %d",&a[i].id,&a[i].num);
        }
        for(i=0;i<n-1;i++)
        {
            for(j=0;j<n-1-i;j++)
            {
                if(a[j].num<a[j+1].num)
                {
                    temp =a[j].num;
                    a[j].num=a[j+1].num;
                    a[j+1].num=temp;
                    t=a[j].id;
                    a[j].id=a[j+1].id;
                    a[j+1].id=t;
                    
                        
                }
               
            }
        }
    }for(i=0;i<n;i++)
    {
        printf("%d %d\n",a[i].id,a[i].num);
    }return 0;
        
}

7-7 选夫婿1

代码:

#include<stdio.h>
 
struct men
 
{
 
    char name[20];
 
    int h,w;
 
}s[1000],t;
 
int main()
 
{
 
    int n,i,j,a,b,c,d,z=1;
 
    scanf("%d",&n);
 
    for(i=0;i<n;i++)
 
    {
 
        scanf("%s%d%d",s[i].name,&s[i].h,&s[i].w);
 
    }
 
    scanf("%d%d%d%d",&a,&b,&c,&d);
 
    for(i=0;i<n-1;i++)
 
    {
 
        for(j=0;j<n-1-i;j++)
 
        {
 
            if(s[j].h>s[j+1].h)
 
            {
 
                t=s[j+1];
 
                s[j+1]=s[j];
 
                s[j]=t;
 
            }
 
            else if(s[j].h==s[j+1].h)
 
            {
 
                if(s[j].w>s[j+1].w)
 
                {
 
                t=s[j+1];
 
                s[j+1]=s[j];
 
                s[j]=t;
 
                }
 
            }
 
        }
 
    }
 
    for(i=0;i<n;i++)
 
    {
 
        if(s[i].h>=a&&s[i].h<=b&&s[i].w>=c&&s[i].w<=d)
 
        {
 
            printf("%s %d %d\n",s[i].name,s[i].h,s[i].w);z=0;
 
        }
 
    }
 
    if(z==1)printf("No\n");
 
    return 0;
 
}

7-8 老--质价比

代码:

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

7-9 共用体练习

代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
union
{
    int a;
    double b;
    char c[20];
}s[100000],t[100000];
int main()
{
    int n,m,i,x;
    scanf("%d%d",&n,&m);
    for(i=0;i<n;i++)
    {
        scanf("%s",s[i].c);
        {
            if(s[i].c[0]=='I')
                scanf("%d",&t[i].a);
            else if(s[i].c[0]=='D')
                scanf("%lf",&t[i].b);
            else if(s[i].c[0]=='S')
                scanf("%s",t[i].c);
        }
    }
    for(i=0;i<m;i++)
    {
        scanf("%d",&x);
        if(s[x].c[0]=='I')
            printf("%d\n",t[x].a);
        else if(s[x].c[0]=='D')
            printf("%.2lf\n",t[x].b);
        else if(s[x].c[0]=='S')
            printf("%s\n",t[x].c);
    }
    return 0;
}

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

代码:

#include <stdio.h>
#include <string.h>
    int main()
    {
        enum color{red, orange, yellow, green, blue, violet} flower;
        int n;
        scanf("%d",&n);
        int i;
        char s[n][30];
        for (i=0;i<n;i++)
        {
            scanf("%s",s[i]);
		}
 
		for (i=0;i<n;i++)
		{
        if(strcmp(s[i],"red")==0) flower=red;
        else if(strcmp(s[i],"orange")==0) flower=orange;
        else if(strcmp(s[i],"yellow")==0) flower=yellow;
        else if(strcmp(s[i],"green")==0) flower=green;
        else if(strcmp(s[i],"blue")==0) flower=blue;
        else if(strcmp(s[i],"violet")==0) flower=violet;
        else {printf("I don't know about the color %s.\n",s[i]);continue;}
        switch(flower)
        {
           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;
 
		}
}
 
                return 0;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

尘心不灭

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

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

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

打赏作者

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

抵扣说明:

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

余额充值