1 import java.util.ArrayList; 2 import java.util.Scanner; 3 4 public class Solution { 5 6 public static void main(String[] args) { 7 8 Scanner scanner = new Scanner(System.in); 9 String nextLine = scanner.nextLine(); 10 11 Integer scoreRet = getScoreRet(nextLine); 12 13 System.out.println(scoreRet); 14 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 } 24 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(); 34 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 } 50 51 if (srcScoreArrLen>1000 || srcScoreArrLen<1){ 52 return -1; 53 } 54 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; 63 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 } 76 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 } 86 87 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); 101 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)){ 109 110 integerArrayList.remove(indexPre); 111 112 continue; 113 } 114 } 115 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; 123 124 integerScore=scoreFactor; 125 126 integerArrayList.add(integerScore); 127 128 continue; 129 } 130 } 131 } 132 133 if (charAt=='+'){ 134 int indexPre=i-1; 135 char charAtPre= srcScoreArr.charAt(indexPre); 136 if (Character.isDigit(charAtPre)){ 137 138 char charAtCurr = srcScoreArr.charAt(i); 139 Integer integerScoreCurr = Integer.valueOf(charAtCurr); 140 141 Integer integerScoreTemp = Integer.valueOf(charAtPre); 142 int scoreFactor=integerScoreCurr+integerScoreTemp; 143 144 integerArrayList.add(scoreFactor); 145 146 continue; 147 } 148 } 149 150 } 151 } 152 } 153 154 155 int scoreRet=0; 156 for (Integer integer : integerArrayList) { 157 scoreRet+=integer; 158 } 159 160 return scoreRet; 161 } 162 }
标签:篮球赛,charAt,charAtPre,int,srcScoreArr,continue,统计算法,Integer,NBA From: https://www.cnblogs.com/liaowanzhong/p/18401537