美文网首页
student id 选择排序

student id 选择排序

作者: 日光降临 | 来源:发表于2019-03-04 18:17 被阅读0次

实现一个名为Class的类,包含如下的一些属性和方法:

一个public的students属性,是一个Student类的数组。
一个构造函数,接受一个参数n,代表班级里有n个学生。构造函数需要创建n个学生的实例对象并放在students这个成员中,每个学生按照创建的顺序,学号需要依次标记为0, 1, 2 ... n-1。

class Student{
   public int id;
   public Student(int id){
      this.id = id;
   }
}

class Class{
   public Student[] students;
   Class(int n){
      this.student = Student[n];
      for(int i=0; i<n; i++)
         this.students[i] = new Student(i);
   }
}

判断是否闰年

public class Solution {
    public boolean isLeapYear(int n) {
        // write your code here
        return (n%400 == 0 || (n%4 == 0 && n%100 != 0));
    }
}

实现一个学生的类 Student. 包含如下的成员函数和方法:

两个公有成员 name 和 score。分别是字符串类型和整数类型。
一个构造函数,接受一个参数 name。
一个公有成员函数 getLevel() 返回学生的成绩等级(一个字符)。
等级表如下:

A: score >= 90
B: score >= 80 and < 90
C: score >= 60 and < 80
D: score < 60

class Student{
   public String name;
   public int score;
   Student(String name){
      this.name = name;
   }
   public char getLevel(){
      if(score >= 90)return ‘A’;
      if(score >= 80)return ‘B’;
      if(score >= 60)return ‘C’;
      return ‘D’;
}

选择排序:用min记住最小数的下标,这样不必每次比较都交换。然后在内层循环结束后,判断i和min是否相等,如果不等交换两者的值。

public class main{
   public static int main(){
      int[] num = {10,2,5,-2,8,1,9};
      int i,j,min;
      int len = num.length;
      for(i=0; i<len-1; i++){
         min=i;
         for(j=i+1; j<len; j++){
            if(num[min] > num[j])
               min = j;
         }
         if(i != min){
            int tmp = num[i];
            num[i] = num[min];
            num[min] = tmp;
         }
      }
      for(i=0; i<len; ++i){
         System.out.print(num[i]+’ ‘);
      }
      System.out.println();
   }
}

相关文章

网友评论

      本文标题:student id 选择排序

      本文链接:https://www.haomeiwen.com/subject/kbtwuqtx.html