1004. 成绩排名 (20)
因为我是用vs2015编译,部分函数会报错。
1、
几个函数的说明
scanf(“%s”,str)
scanf_s(“%s”,str,size)
例如:
char str[50];
scanf(“%s”,str);
scanf_s(“%s”,str,50);
char *s;
s = (char *)malloc(50);
scanf_s(“%s”,s,50);
gets(),遇到第一个换行字符(\n)停止,并在最后面加上(\0)
scanf()从非空白字符遇到下一个空白字符(空格、换行等)停下。
2、数字字符串转整型
char s[10]=”123456”;
int sc = atoi(s);
3、字符串指针之间复制,连接
include string.h
char *strcpy(char *dest, const char *src);
char *strcat(char *dest, const char *src);
strcpy与strcpy_s区别 跟scanf scanf_s一样,得加上大小防止溢出
s->name = (char )malloc(MAXSTR sizeof(char));
strcpy_s(s->name, MAXSTR, name);
4、java c c++指针弄混 对c指针理解要加深
5、仔细读题,特别是输出格式。这题又在输出格式上栽跟头了。
//CODE BY INDERE 2017/6/14
//读入n名学生的姓名、学号、成绩,分别输出成绩最高和成绩最低学生的姓名和学号。
//输出格式:对每个测试用例输出2行,第1行是成绩最高学生的姓名和学号,第2行是成绩最低学生的姓名和学号,字符串间有1空格。
//3
//Joe Math990112 89
//Mike CS991301 100
//Mary EE990830 95
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXSTR 11
#define MAXSIZE 50
typedef struct Stu {
char *name;
char *stuno;
int score;
}Student;
void init(Student *s, char name[], char stuno[], int score);
void sortStu(Student *s, int num);
void getDest(Student *s, int num);
int main() {
int stunum;
scanf("%d", &stunum);
Student *stu = (Student *)malloc(sizeof(Student) * stunum);
for (int i = 0; i < stunum; i++) {
char name[MAXSTR];
char stuno[MAXSTR];
char score_str[MAXSTR];
int score;
scanf("%s %s %s", name,stuno,score_str);
score = atoi(score_str);
init(&stu[i], name, stuno, score);
}
sortStu(stu, stunum);
getDest(stu, stunum);
return 0;
}
void init(Student* s, char *name, char *stuno, int score) {
s->name = (char *)malloc(MAXSTR * sizeof(char));
strcpy(s->name, name);
s->stuno = (char *)malloc(MAXSTR * sizeof(char));
strcpy(s->stuno, stuno);
s->score = score;
}
void sortStu(Student *s, int num) {
//bubbling sort
for (int i = 0; i < num - 1; i++) {
for (int j = i + 1; j < num; j++) {
if (s[i].score > s[j].score) { //exchange
Student temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
}
void getDest(Student *s,int stunum) {
char *topstu,*laststu;
topstu = (char *)malloc(sizeof(char)*MAXSIZE);
laststu = (char *)malloc(sizeof(char)*MAXSIZE);
strcpy(topstu,s[stunum-1].name);
strcat(topstu," ");
strcat(topstu,s[stunum-1].stuno);
strcpy(laststu,s[0].name);
strcat(laststu," ");
strcat(laststu,s[0].stuno);
printf("%s\n", topstu);
printf("%s\n", laststu);
}