欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

软件构造习题课6-6心得

程序员文章站 2024-02-09 16:03:16
...

1 给出一个Java题目
Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher’s h-index. According to the definition of h-index on Wikipedia: “A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each.”
Example:Input: citations = [3,0,6,1,5]
Output: 3
Explanation: [3,0,6,1,5] means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 citations respectively.
Since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, her h-index is 3.
Note: If there are several possible values for h, the maximum one is taken as the h-index.
我们稍微分析一下,这道题的意思就是,我们先对这个数组排个序(降序),然后找到所在位置的数值小于等于位置编号(如果从1开始编)的位置,然后返回就好了。
所以我们就有了第一次的代码:常规的scanner+冒泡排序+for循环搜索

// 从控制台读入
var foo = 'bar';
Scanner scanner = new Scanner(System.in);
int[] citations = new int[100];
String[] strs;
System.out.println("Please input the citation numbers:");
String line = scanner.nextLine();
strs = line.split(",");
for (int i = 0; i < strs.length; i++) 
	citations[i] = Integer.parseInt(strs[i]);
//冒泡排序
for (int i = 0; i < number - 1; i++) {	
	for (int j = 0; j < number - 1; j++) {		
		if (citations[j] < citations[j + 1]) {			
			int temp = citations[j + 1];			
			citations[j + 1] = citations[j];			
			citations[j] = temp;		
		}	
	}
}
//计算Hindex
int hindex = 0;
for (int j = 0; j < number; j++) {
	if (citations[j] >= j + 1)
			hindex = j + 1;
	else
			break;
}
System.out.println("The h-index is: " + hindex);

这次的代码都是在main函数里面写出来的,模块化不强而且不方便后边对代码的改进,所以我们就有了新的想法,就是把这几步分别用方法封装一下,然后我们想改进的时候只需要改方法内部就好了,而几乎不用改main函数。

然后我们对于一些边界值进行测试,发现都挂了,比如:空数组,负值,特殊值(符号等等)。
其实对于空数组我们很好解决,只要在最开始的时候用line.length()判断一下就好了。

String line = new String();
line = scanner.nextLine();
while(line.length() == 0) {
	System.out.println("Input empty, please re-input:");
	line = scanner.nextLine();
}

然后我们就开始对负值和小数还有其他特殊值进行判断,在实验一中,我们当时也有一个这样的判断,目的是为了判断MagicSquare是否每一个数都是一个正整数,当时我是这样判断的:

for(i=0;i<SingleWord.length;i++) {
     for(j=0;j<SingleWord[i].length();j++) {
      //判断是否是正整数
      if(!Character.isDigit(SingleWord[i].charAt(j))){
       System.out.println(fileName+k+","+i+"中含有不是正整数的元素"+SingleWord[i]);
       return false;
      }
     }
     //如果符合 就将矩阵中的数字赋值
     matrix[k][i]= Integer.valueOf(SingleWord[i]);
}

我这个方法比较笨,上课的时候老师讲了一种比较好的方法,就是使用正则表达式,这是我第一次接触这种东西,现在还不咋会写,就先套用一下老师的:

boolean legalNumbers = true;
strs = line.split(",");
for (int i = 0; i < strs.length; i++) {
	//if not, stop checking others and let user re-input
	if(! strs[i].matches("[0-9]+")) {
	System.out.println(strs[i] + " is illegal: ");
	legalNumbers = false;
	break;		
	}		
//otherwise, store the integer into array		
citations[i] = Integer.parseInt(strs[i]); 	
}

这种方式感觉能够节省一个循环。
然后我们发现,在老师课上给出的例子中,我们就定义了一个长度为100的数组,那么如果输入的数组长度大于100,那么就会出现一个异常,所以我们选择使用一个try catch进行或者直接将数组改成list这样既能够完成排序,又不用担心溢出的错误

List<Integer> citations = new ArrayList<>();

String[] strs = input.split(",");

for (int i = 0; i < strs.length; i++) {
   if(! strs[i].matches("[0-9]+")) 
      throw new IllegalArgumentException(strs[i] + " is illegal");

   citations.add(Integer.parseInt(strs[i]));
}

return citations;
}

最后我们还可以设计paper类和author类然后用每个类存储相应的数据。