1.投票选国王
#include<stdio.h>
#include<string.h>
typedef struct candidate
{
char name[100];
int votes;
} candidate;
int main(void)
{
candidate n1;
candidate n2;
candidate n3;
candidate k;
char vote[50];
scanf("%s",n1.name);
scanf("%s",n2.name);
scanf("%s",n3.name);
n1.votes = 0;
n2.votes = 0;
n3.votes = 0;
while (scanf("%s",vote) != EOF)
{
setbuf(stdin,NULL);
if (strcmp(n1.name,vote) == 0)
{
n1.votes++;
}
if (strcmp(n2.name,vote) == 0)
{
n2.votes++;
}
if (strcmp(n3.name,vote) == 0)
{
n3.votes++;
}
}
k = n2;
if (n2.votes > n1.votes)
{
k = n2;
if (k.votes < n3.votes)
{
k = n3;
}
}
printf("%s",k.name);
}
2.用结构体储存二维平面向量坐标并计算两个二维平面向量的差向量
#include<stdio.h>
typedef struct t2
{
float m;
float n;
}t2;
int main(void)
{
t2 ve1;
t2 ve2;
t2 ve3;
scanf("%f %f %f %f",&ve1.m,&ve1.n,&ve2.m,&ve2.n);
ve3.m = ve1.m - ve2.m;
ve3.n = ve1.n - ve2.n;
printf("(%.1f, %.1f)",ve3.m,ve3.n);
}
3.用结构体储存3个学生成绩
#include<stdio.h>
typedef struct student
{
char name[20];
int sc1;
int sc2;
int sc3
;
float ave;
}stu;
int main(void)
{
stu s1;
stu s2;
stu s3;
scanf("%s %d %d %d",s1.name,&s1.sc1,&s1.sc2,&s1.sc3);
scanf("%s %d %d %d",s2.name,&s2.sc1,&s2.sc2,&s2.sc3);
scanf("%s %d %d %d",s3.name,&s3.sc1,&s3.sc2,&s3.sc3);
s1.ave = (s1.sc1+s1.sc2+s1.sc3)/3;
s2.ave = (s2.sc1+s2.sc2+s2.sc3)/3;
s3.ave = (s3.sc1+s3.sc2+s3.sc3)/3;
int bo = 0;
while (bo == 0)
{
stu t;
bo = 1;
if (s1.ave<s2.ave)
{
t = s1;
s1 = s2;
s2 = t;
bo = 0;
}
if (s2.ave<s3.ave)
{
t = s2;
s2 = s3;
s3 = t;
bo = 0;
}
}
printf("%s %.2f\n",s1.name,s1.ave);
}
4.