Summary: The use of several functions
(1) int num=Integer.parseint (str[0]); Converts the first string to an integer representing the number of names
(2) String string1=str[i].tolowercase (); Variable lowercase are
(3) Char ch[]=string1.tochararray (); Converts each string into a printed character array
(4) String st=integer.tostring(num1[p]); convert int to String type
(5) algorithm level: Calculate the number of each name letter appear, after sorting, directly according to the sort of counting to calculate on the line, the count is 26, a decrease on the line
Describe |
Give a name that consists of 26 strings, which defines the "beauty" of the string as the sum of all its letters "beauty". Each letter has a "pretty degree", ranging from 1 to 26. No two letters have the same "beauty degree". Letters Ignore case. Give multiple names to calculate the maximum possible "beauty" for each name. |
Knowledge points |
String |
Run time limit |
0M |
Memory limit |
0 |
Input |
An integer n, followed by n names n A string, each representing a name |
Output |
Each name may be the most beautiful degree |
Sample input |
2 Zhangsan Lisi |
Sample output |
192 101 |
PC Test version:
PackageHuawei4;ImportJava.util.Scanner; Public classBeautiful { Public Static voidMain (string[] args) {System.out.println ("Please enter the sample:"); Scanner Scstr=NewScanner (system.in); String in=Scstr.nextline (); String str[]=in.split ("");//Save up, space split; intNum=integer.parseint (Str[0]);//converts the first string to an integer representing the number of names intnum1[]=New int[Num];//to store the beauty of every name for(inti=1;i<=num;i++) { inttmp[]=New int[26];//the number of 26 letters each name appearsString string1=str[i].tolowercase ();//variable lowercase are CharCh[]=string1.tochararray ();//converts each string into a printed character array for(intJ=0;j<str[i].length (); j + +) {//Number of statistics intk=ch[j]-' a '; if((k>=0) && (k<=26) ) {Tmp[k]++;//If you add 1 to this range; } } //bubbling to TMP intTMP2; for(intp=tmp.length-1;p>0;p--){ for(intq=0;q<p;q++){ if(tmp[q+1]<Tmp[q]) {TMP2=Tmp[q]; TMP[Q]=tmp[q+1]; Tmp[q+1]=TMP2; } } } //no need to calculate each letter, directly traverse the number of letters for(intk=0;k<tmp.length;k++) {Num1[i-1]=num1[i-1]+tmp[k]* (26+k-25);//If you add 1 to this range;}} StringBuffer tt=NewStringBuffer (); for(intp=0;p<num;p++) {String St=integer.tostring (num1[p]);//convert int to String type tt.append (ST); if(p!=num-1) Tt.append (" ");//a space, if not the last name} System.out.print (TT);//for (int p=0;p<num;p++) {//System.out.println (num1[p]);// } } }
Huawei Online OJ Version:
View Plaincopy to Clipboardprint?ImportJava.util.Scanner; Public classmain{ Public Static voidMain (string[] args) {Scanner scstr=NewScanner (system.in); String in=Scstr.nextline (); String str[]=in.split ("");//Save up, space split; intNum=integer.parseint (Str[0]);//converts the first string to an integer representing the number of names intnum1[]=New int[Num];//to store the beauty of every name for(inti=1;i<=num;i++) { inttmp[]=New int[26];//the number of 26 letters each name appearsString string1=str[i].tolowercase ();//variable lowercase are CharCh[]=string1.tochararray ();//converts each string into a printed character array for(intJ=0;j<str[i].length (); j + +) {//Number of statistics intk=ch[j]-' a '; if((k>=0) && (k<=26) ) {Tmp[k]++;//If you add 1 to this range; } } //bubbling to TMP intTMP2; for(intp=tmp.length-1;p>0;p--){ for(intq=0;q<p;q++){ if(tmp[q+1]<Tmp[q]) {TMP2=Tmp[q]; TMP[Q]=tmp[q+1]; Tmp[q+1]=TMP2; } } } //no need to calculate each letter, directly traverse the number of letters for(intk=0;k<tmp.length;k++) {Num1[i-1]=num1[i-1]+tmp[k]* (26+k-25);//If you add 1 to this range;}} StringBuffer tt=NewStringBuffer (); for(intp=0;p<num;p++) {String St=integer.tostring (num1[p]); Tt.append (ST); if(p!=num-1) Tt.append (" ");//a space, if not the last name} System.out.print (TT); } }
hw--Beauty degree 2