有一个用分号间隔的字符串,如“A;AA;AAA;AAAA;AA;AA”,求计算权重,有一个AAAA权重为4,AAA权重为3,AA权重为2,A权重为1,最后的权重计算为最大权重乘以个数,
例如 AA;AA权重为2*2
AA权重为2*1
求如何用代码实现
package test2;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
// System.out.println(str);
String[] ss = str.split(";");
int x = js(ss);
System.out.println(x);
}
public static int js(String[] str) {
//权重
int qz = 0;
int max = 0;
int[] test = new int[4];
for (int i = 0; i < str.length; i++) {
if (str[i].equals("A")) {
test[0]++;
} else if (str[i].equals("AA")) {
test[1]++;
} else if (str[i].equals("AAA")) {
test[2]++;
} else if (str[i].equals("AAAA")) {
test[3]++;
}
}
for (int i = 0; i < test.length; i++) {
if (test[i] > max){
qz = i+1;
max = test[i];}
}
//System.out.println("qz:" + qz+",maxNum:"+max);
return max * qz;
}
}
'A;AA;AAA;AAAA;AA;AA'.split(';').sort().reverse()[0].length * 'A;AA;AAA;AAAA;AA;AA'.split(';').length
int n="A;AA;AAA;AAAA;AA;AA".Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries).Max().Length *"A;AA;AAA;AAAA;AA;AA".Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries).Count();
//别告诉我我是对的!
.net?sql?