close
題目概要:
給2字串,並將字串換成ASCII CODE,再將轉換後的數字相加(Ex 123=1+2+3=(6))直到個位數為止,計算2字串的出百分比。
解題方向:
依據題目需求去寫即可。字串中可能會有特殊自元,特殊字元可忽略,大小寫所代表的數字都一樣。
程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Java | |
import java.util.Scanner; | |
class uva10424{ | |
public static void main(String args[]){ | |
Scanner sc=new Scanner(System.in); | |
while(sc.hasNextLine()){ | |
double val1=change(sc.nextLine().toLowerCase()); //讀取第一個字串,並轉換成數字。 | |
double val2=change(sc.nextLine().toLowerCase()); //讀取第二個字串,並轉換成數字。 | |
//Output | |
if(Math.max(val1,val2)!=0)System.out.printf("%.2f %%",(Math.min(val1,val2)/Math.max(val1,val2))*100); | |
else System.out.print("-1.#J %"); | |
System.out.println(""); | |
} | |
} | |
public static double change(String st){ | |
//將每個英文字母轉換ASCII CODE,再將每個數字相加,直到數字為個位數為止。EX (2+3+26) = 31 = (3+1) = 4。 | |
double val=0; | |
for(int i=0;i<st.length();i++){ | |
if(st.charAt(i)>=97 && st.charAt(i)<=122) val=val+st.charAt(i)-96; | |
} | |
while((int)val>=10){ | |
val=(int)val/10+(int)val%10; | |
} | |
return val; | |
} | |
} |
文章標籤
全站熱搜