题目描述
2048星球要举行一次选举,以选出他们的新一任国王。请你帮忙写一道小程序来帮助他们确定谁是新的国王。
#include<stdio.h>
#include<string.h>
struct GUOWANG {
char b[10];
int n;
};
int main() {
struct GUOWANG temp, a[3] = { "Bob",0,"zhengyang",0,"yangguang",0 };
char num[10];
int i, j,k;
for (i = 0; i < 10; i++) {
scanf("%s", num);
for (j = 0; j < 3; j++) {
if (strcmp(num, a[j].b) == 0) {
a[j].n++;
}
}
}
for (i = 0; i < 2; i++) {
k = i;
for (j = i + 1; j < 3; j++) {
if (a[j].n > a[k].n) {
k = j;
temp = a[k];
a[k] = a[j];
a[j] = temp;
}
}
}
i=0;
printf("%s\n", a[i].b);
return 0;
}
本题要求编写程序,用结构体来存储二维平面向量坐标并计算两个二维平面向量的差向量。
#include<stdio.h>
#include <stdlib.h>
struct pos {
double x;
double y;
}a[2];
int main(){
int i;
double n, m;
for (i = 0; i < 2; i++) {
scanf("%lf %lf", &a[i].x, &a[i].y);
}
n = a[0].x - a[1].x;
m = a[0].y - a[1].y;
printf("(%.1lf, %.1lf)", n, m);
return 0;
}
本题要求用结构体来存储三个学生的成绩,每个学生有3门课的成绩(成绩为小数)
#include<stdio.h>
#include <stdlib.h>
struct mingdan {
char x[20];
double yuwen;
double shuxue;
double yingyu;
double b;
}a[3],temp;
int main(){
int i,j,k;
for (i = 0; i < 3; i++) {
scanf("%s %lf %lf %lf",&a[i].x ,&a[i].shuxue, &a[i].yingyu, &a[i].yuwen);
a[i].b=(a[i].shuxue+a[i].yingyu+a[i].yuwen)/3;
}
for (i = 0; i < 2; i++) {
for (j = i + 1; j < 3; j++) {
if (a[j].b > a[i].b) {
k = i;
temp = a[k];
a[k] = a[j];
a[j] = temp;
}
}
}
printf("%s %.2lf ", a[0].x,a[0].b );
return 0;
}
输入多个学生的学号、姓名和成绩,然后输出成绩最高的哪个学生的姓名和学号
#include<stdio.h>
struct mingdan {
int xuehao;
char xingming[20];
int chengji;
};
int main() {
struct mingdan a[10],temp;
int n,i,j,m;
while (scanf("%d", &n)!= EOF) {
if(n==0)break;
for (i = 0; i < n; i++) {
scanf("%d %s %d", &a[i].xuehao, a[i].xingming, &a[i].chengji);
}
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if(a[j].chengji>a[i].chengji){
m = j;
temp = a[i];
a[i] = a[j];
a[j] = temp;}
}
}
printf("%d %s\n", a[0].xuehao,a[0].xingming);
}
return 0;
}
给定几组日期,要求按照时间从前往后顺序输出
#include<stdio.h>
struct date {
int month;
int day;
};
int main() {
struct date date1[10], temp;
int i, j, k, m;
for (i = 0; i < 10; i++) {
scanf("%d %d", &date1[i].month, &date1[i].day);
if (date1[i].month == 0 && date1[i].day == 0)
break;
}k = i - 1;
for (j = 0; j < k ; j++) {
for (i =j+1; i <=k; i++) {
if (date1[i].month < date1[j].month ) {
m = i;
temp = date1[m];
date1[m] = date1[j];
date1[j] = temp;
}
else if (date1[i].month == date1[j].month && date1[i].day < date1[j].day) {
m = i;
temp = date1[m];
date1[m] = date1[j];
date1[j] = temp;
}
}
}for (i = 0; i <= k; i++) {
printf("%d %d\n", date1[i].month, date1[i].day);
}
return 0;
}