Please write a JAVA program for the following tasks.
Input : two number R and S of 3 non-duplicated digits.
purposes :

1. show the relationship ( = , < , > ) of the two numbers.

2. show how many digits shared by R and S.

→123 , 456 : 0
→123 , 302 : 2

3. show the similarity of R and S using xAyB format.

→123 , 456 : 0A0B
→123 , 302 : 0A2B
→123 , 123 : 3A0B


不過針對數字有重複的情況下沒做任何例外處理XD
--------------------------------------------------------------
import java.io.*;

public class homework_1{
public static void main(String[] aqwe)
throws IOException{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("本程式使用規則:分別輸入兩個三位數,位數之間的數字不得重複");
System.out.println("請輸入三位數r:");
String str = br.readLine();
int r = Integer.parseInt(str);
System.out.println("請輸入三位數s:");
String str2 = br.readLine();
int s = Integer.parseInt(str2);
int same = 0;
int A = 0;
int B = 0;

int i = r - s;
if(i == 0)
System.out.println("r = s");
if(i < 0)
System.out.println("r < s");
if(i > 0)
System.out.println("r > s");

int[] rr = new int[3];
int[] ss = new int[3];

for(int q=0;q<=2;q++){
rr[q] = r%10;
r = r/10;
ss[q] = s%10;
s = s/10;
}

for(int x=0;x<=2;x++){
for(int y=0;y<=2;y++){
if(rr[y] == ss[x]){
same++;
if(x == y){
A++;
}
else{
B++;
}
}
}
}

System.out.println(same);
System.out.println(A + "A" + B + "B");


}
}
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 steven70101 的頭像
    steven70101

    老人家的舊書房

    steven70101 發表在 痞客邦 留言(0) 人氣()