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;
}