close

題目概要:

紀錄每個按鈕按了幾次。

解題方向:

1. 使用一個字元陣列紀錄每個英文字母指法。

2. 目前的按法跟上一個按法做比較,當是0→1時就紀錄按一次。

程式碼:

//Java
import java.util.Scanner;
class uva10415{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int cases=Integer.parseInt(sc.nextLine());
for(int i=0;i<cases;i++){
//紀錄每個英文字母所會按到的鍵。
String finger[]={"c0111001111","d0111001110","e0111001100","f0111001000","g0111000000","a0110000000","b0100000000","C0010000000","D1111001110","E1111001100","F1111001000","G1111000000","A1110000000","B1100000000"};
int count[]=new int[10]; //紀錄每個鍵所按的次數。
String st=sc.nextLine();
String current="0000000000"; //目前手指所按的鍵。
for(int j=0;j<st.length();j++){
String temp="";
//找出目前的英文字母所需要案的鍵
for(int k=0;k<finger.length;k++){
if(st.charAt(j)==finger[k].charAt(0)){
temp=finger[k].toString();
break;
}
}
//判斷次數是否++。
StringBuilder stB=new StringBuilder(temp);
temp=stB.substring(1);
for(int k=0;k<10;k++){
if(current.charAt(k)!=temp.charAt(k) && temp.charAt(k)=='1') count[k]++;
}
current=temp;
}
//Output
for(int j=0;j<10;j++){
if(j!=9) System.out.print(count[j]+" ");
else System.out.print(count[j]);
}
System.out.println("");
}
}
}
view raw uva10415.java hosted with ❤ by GitHub

arrow
arrow
    文章標籤
    Java
    全站熱搜
    創作者介紹
    創作者 a7069810 的頭像
    a7069810

    紀錄自己的程式人生

    a7069810 發表在 痞客邦 留言(3) 人氣()