1. 程式人生 > 其它 >【SVM預測】基於螢火蟲演算法改進SVM演算法實現資料分類matlab原始碼

【SVM預測】基於螢火蟲演算法改進SVM演算法實現資料分類matlab原始碼

1036 Boys vs Girls (25 分)

This time you are asked to tell the difference between the lowest grade of all the male students and the highest grade of all the female students.

Input Specification:

Each input file contains one test case. Each case contains a positive integer N, followed by N lines of student information. Each line contains a student's name

, gender, ID and grade, separated by a space, where name and ID are strings of no more than 10 characters with no space, gender is either F (female) or M (male), and grade is an integer between 0 and 100. It is guaranteed that all the grades are distinct.

Output Specification:

For each test case, output in 3 lines. The first line gives the name and ID of the female student with the highest grade, and the second line gives that of the male student with the lowest grade. The third line gives the difference gradeF

gradeM. If one such kind of student is missing, output Absent in the corresponding line, and output NA in the third line instead.

Sample Input 1:

3
Joe M Math990112 89
Mike M CS991301 100
Mary F EE990830 95

Sample Output 1:

Mary EE990830
Joe Math990112
6

Sample Input 2:

1
Jean M AA980920 60

Sample Output 2:

Absent
Jean AA980920
NA

思路

程式碼

#include<stdio.h>
#include<string.h>

struct Student{
	char name[20];
	char ID[20];
	char gender;
	int grade;
}tmp, highest, lowest;

bool Higher(Student a, Student b){//判斷同性別的學生的成績A成績高時返回true 
	if(a.gender == b.gender) return a.grade > b.grade;
	else return false;
}

void Init(){//初始化highest 和lowest 的相關資料 
	lowest.gender = 'M';
	lowest.grade = 100;
	highest.gender = 'F';
	highest.grade = 0;
}

int main(){
	int n;
	int girl = 0;//boy和girl分別來記錄輸入的男女生的人數,若沒有女生或者沒有男生輸入,後面還要特判
	int boy = 0;
	scanf("%d", &n);
	Init();
	for(int i = 0; i < n; i++){
		scanf("%s %c %s %d", &tmp.name, &tmp.gender, &tmp.ID, &tmp.grade);
		if(tmp.gender == 'M' && Higher(lowest, tmp)) {
			lowest = tmp;
			boy++;
		}
		if(tmp.gender == 'F' && Higher(tmp, highest)){
			highest = tmp;
			girl++;
		} 
	}
	if(boy != 0 && girl != 0){//男孩女孩成績都有
		printf("%s %s\n", highest.name, highest.ID);
		printf("%s %s\n%d", lowest.name, lowest.ID, highest.grade - lowest.grade);
	}else if(boy == 0){//沒有男孩子,特判
		printf("%s %s\n", highest.name, highest.ID);
		printf("Absent\nNA");
	}else{//沒有女孩子,特判
		printf("Absent\n");
		printf("%s %s\nNA", lowest.name, lowest.ID);
	}
	return 0;
}

參考程式碼

  1. 思路與上面的差不多,但是其中的初始化男生的女生的分數時,設定了101和-1分,這樣我們可以通過判斷最後是否存在101和-1來看有沒有男女生的輸入來完成特判,就不需要我上面用到的兩個輔助變數girl和boy了
  2. 成績的比較沒有那麼麻煩(就一句話),可以不用另外寫函式
#include<stdio.h>

struct Student{
	char name[15];
	char ID[15];
	char gender;
	int grade;
}tmp, highest, lowest;

void Init(){//初始化highest 和lowest 的相關資料 
	lowest.grade = 101;
	highest.grade = -1;
}

int main(){
	int n;
	scanf("%d", &n);
	Init();
	for(int i = 0; i < n; i++){
		scanf("%s %c %s %d", &tmp.name, &tmp.gender, &tmp.ID, &tmp.grade);
		if(tmp.gender == 'M' && tmp.grade < lowest.grade) {
			lowest = tmp;
		}
		if(tmp.gender == 'F' && tmp.grade > highest.grade){
			highest = tmp;
		} 
	}
	
	if(highest.grade == -1) printf("Absent\n");
	else printf("%s %s\n", highest.name, highest.ID);
	if(lowest.grade == 101) printf("Absent\n");
	else printf("%s %s\n", lowest.name, lowest.ID);
	if(highest.grade == -1 || lowest.grade == 101) printf("NA\n");
	return 0;
}