英雄会挑战失败求原因
生活随笔
收集整理的這篇文章主要介紹了
英雄会挑战失败求原因
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目詳情
public? class?Main?{
???? public? static? int?perfect(String?s)?{
????????String?lowCaseString?=?s.toLowerCase();
???????? char[]?chars?=?lowCaseString.toCharArray();
???????? int[]?weights?=? new? int[26];
???????? for( char?c?:?chars){
???????????? int?index?=?( int)(c?-? 'a');
???????????? //如果該字符與'a'的差值大于25,說明不是英文字母,不處理
???????????? if(index?>?25){
???????????????? continue;
????????????}
????????????weights[index]?+=?1;
????????}
????????
????????Arrays.sort(weights);
???????? int?perfectValue?=?0;
???????? for( int?i?=?weights.length?-?1?;?i?>?0?;?i--){
???????????? if(weights[i]?==?0){
???????????????? break;
????????????}
????????????perfectValue?+=?(weights[i]?*?(i?+?1));
????????}
???????? return?perfectValue;
????}
????
???? //start?提示:自動閱卷起始唯一標識,請勿刪除或增加。?
???? public? static? void?main(String?args[])?
????{?
????????System.out.println(perfect( "dad"));
????}?
???? //end?//提示:自動閱卷結束唯一標識,請勿刪除或增加。
} ? ? ? ?
我們要給每個字母配一個1-26之間的整數,具體怎么分配由你決定,但不同字母的完美度不同,
而一個字符串的完美度等于它里面所有字母的完美度之和,且不在乎字母大小寫,也就是說字母F和f的完美度是一樣的。
現在給定一個字符串,輸出它的最大可能的完美度。
例如:dad,你可以將26分配給d,25分配給a,這樣整個字符串最大可能的完美度為77。
函數頭部
C
int perfect(const char *s);
C++
int perfect(const string &s);
java
public static int perfect(String s);
答題說明 main函數用于方便自行編譯,可不完成。 我提交的代碼 import?java.util.*;public? class?Main?{
???? public? static? int?perfect(String?s)?{
????????String?lowCaseString?=?s.toLowerCase();
???????? char[]?chars?=?lowCaseString.toCharArray();
???????? int[]?weights?=? new? int[26];
???????? for( char?c?:?chars){
???????????? int?index?=?( int)(c?-? 'a');
???????????? //如果該字符與'a'的差值大于25,說明不是英文字母,不處理
???????????? if(index?>?25){
???????????????? continue;
????????????}
????????????weights[index]?+=?1;
????????}
????????
????????Arrays.sort(weights);
???????? int?perfectValue?=?0;
???????? for( int?i?=?weights.length?-?1?;?i?>?0?;?i--){
???????????? if(weights[i]?==?0){
???????????????? break;
????????????}
????????????perfectValue?+=?(weights[i]?*?(i?+?1));
????????}
???????? return?perfectValue;
????}
????
???? //start?提示:自動閱卷起始唯一標識,請勿刪除或增加。?
???? public? static? void?main(String?args[])?
????{?
????????System.out.println(perfect( "dad"));
????}?
???? //end?//提示:自動閱卷結束唯一標識,請勿刪除或增加。
} ? ? ? ?
?
轉載于:https://www.cnblogs.com/jiangu66/p/3230807.html
總結
以上是生活随笔為你收集整理的英雄会挑战失败求原因的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1,windows java环境搭建,e
- 下一篇: rocket mq 监听端口_MQ消息最