NBA 篮球赛的计分统计算法
1 import java.util.ArrayList; 2 import java.util.Scanner; 3 4 public class Solution { 5 6 public static void main(String[] args) { 8 Scanner scanner = new Scanner(System.in); 9 String nextLine = scanner.nextLine(); 11 Integer scoreRet = getScoreRet(nextLine); 13 System.out.println(scoreRet); 15 } 16 17 public static Integer getScoreRet(String srcScoreArrStr){ 18 if (srcScoreArrStr==null){ 19 return -1; 20 } 21 if (srcScoreArrStr.isEmpty()){ 22 return -1; 23 } 25 String trimSrcScoreTempArrStr = srcScoreArrStr.trim(); 26 String[] splitSrcScoreTempArrStr = trimSrcScoreTempArrStr.split("\\s"); 27 int length = splitSrcScoreTempArrStr.length; 28 StringBuilder stringBuilder = new StringBuilder(); 29 for (int i = 0; i < length; i++) { 30 String scoreStrTemp = splitSrcScoreTempArrStr[i]; 31 stringBuilder.append(scoreStrTemp); 32 } 33 String srcScoreArr = stringBuilder.toString(); 35 int srcScoreArrLen=srcScoreArr.length(); 36 for (int i = 0; i < srcScoreArrLen; i++) { 37 char charAt = srcScoreArr.charAt(i); 38 if (Character.isDigit(charAt)){ 39 continue; 40 }else if (charAt=='+'){ 41 continue; 42 }else if (charAt=='C'){ 43 continue; 44 }else if (charAt=='D'){ 45 continue; 46 }else{ 47 return -1; 48 } 49 } 51 if (srcScoreArrLen>1000 || srcScoreArrLen<1){ 52 return -1; 53 } 55 int multiPlyFac=10; 56 for (int i = 0; i < 4; i++) { 57 multiPlyFac*=10; 58 } 59 int lowDeadLineNum=-3; 60 lowDeadLineNum*=multiPlyFac; 61 int highRestricNum=3; 62 highRestricNum*=multiPlyFac; 64 for (int i = 0; i < srcScoreArrLen; i++) { 65 char charAt = srcScoreArr.charAt(i); 66 Integer integerScore = Integer.valueOf(charAt); 67 if (Character.isDigit(integerScore)){ 68 if (integerScore<lowDeadLineNum){ 69 return -1; 70 } 71 if (integerScore>highRestricNum){ 72 return -1; 73 } 74 } 75 } 77 if (srcScoreArr.length()==1){ 78 char srcScoreArr1 = srcScoreArr.charAt(0); 79 if (Character.isDigit(srcScoreArr1)){ 80 return Integer.valueOf(srcScoreArr1); 81 } 82 if (srcScoreArr1=='+'){ 83 return -1; 84 } 85 } 88 ArrayList<Integer> integerArrayList = new ArrayList<Integer>(); 89 for (int i = 0; i < srcScoreArrLen; i++) { 90 char charAt = srcScoreArr.charAt(i); 91 Integer integerScore=0; 92 if (Character.isDigit(charAt)) { 93 integerScore = Integer.valueOf(charAt); 94 integerArrayList.add(integerScore); 95 continue; 96 }else { 97 char charAt1 = srcScoreArr.charAt(0); 98 if (charAt1=='+'){ 99 integerScore=-1; 100 integerArrayList.add(integerScore); 102 continue; 103 }else { 104 if (charAt=='C'){ 105 int indexPre=i-1; 106 char charAtPre = srcScoreArr.charAt(indexPre); 107 Integer integerScoreFactor = Integer.valueOf(charAtPre); 108 if (Character.isDigit(integerScoreFactor)){ 110 integerArrayList.remove(indexPre); 112 continue; 113 } 114 } 116 if (charAt=='D'){ 117 int indexPre=i-1; 118 char charAtPre = srcScoreArr.charAt(indexPre); 119 if (Character.isDigit(charAtPre)){ 120 if (Character.isDigit(charAtPre)){ 121 Integer integerScoreTemp = Integer.valueOf(charAtPre); 122 int scoreFactor=integerScoreTemp*2; 124 integerScore=scoreFactor; 126 integerArrayList.add(integerScore); 128 continue; 129 } 130 } 131 } 133 if (charAt=='+'){ 134 int indexPre=i-1; 135 char charAtPre= srcScoreArr.charAt(indexPre); 136 if (Character.isDigit(charAtPre)){ 138 char charAtCurr = srcScoreArr.charAt(i); 139 Integer integerScoreCurr = Integer.valueOf(charAtCurr); 141 Integer integerScoreTemp = Integer.valueOf(charAtPre); 142 int scoreFactor=integerScoreCurr+integerScoreTemp; 144 integerArrayList.add(scoreFactor); 146 continue; 147 } 148 } 150 } 151 } 152 } 155 int scoreRet=0; 156 for (Integer integer : integerArrayList) { 157 scoreRet+=integer; 158 } 160 return scoreRet; 161 } 162 }