2017年5月7日 星期日

[ITSA Basic] 題目27. 猜數字(Java, C++)

[ITSA Basic] 題目27. 猜數字
問題描述 :
製作出一個 4 位數的猜數字系統,若此數的數值和位置跟答案完全相同則出現 A ,若此數的數值跟答案相同但位置不一樣則出現 B , 4A 時代表猜中此數字。 ( 注意 : 此猜數字的字數不能重複 )
假設答案為 :1234
若輸入 :
5621
4321
1324
1234
0000
則會輸出 :
0A2B
0A4B
2A2B
4A0B
輸入說明 :
讀入一連串的 4 位數字,最前面那個為此提猜數字的答案,接著為猜此數字的答案,輸入 0 為結束。
輸出說明 :
將幾 A 幾 B 輸出
解釋


    Java
  1. import java.util.*; 
  2. import java.lang.*; 
  3. import java.io.*; 
  4.  
  5. public class Main { 
  6.  
  7.     public static void main(String[] args) { 
  8.        Scanner sc = new Scanner(System.in); 
  9.        int ans; 
  10.        int num; 
  11.        int A = 0; 
  12.      int B = 0; 
  13.      boolean end = false
  14.        ans = sc.nextInt(); 
  15.  
  16.        int ansarr[] = new int[4]; 
  17.      ansarr[0] = ans /1000; 
  18.      ansarr[1] = (ans % 1000)/100; 
  19.       ansarr[2] = ((ans % 1000)%100)/10; 
  20.      ansarr[3] = ((ans % 1000)%100)%10; 
  21.      /* 
  22.      for(int i =0;i < ansarr.length;i++) 
  23.      { 
  24.           System.out.println(ansarr[i]); 
  25.      } 
  26.       */ 
  27.      int numarr[] = new int[4]; 
  28.      // 
  29.      while(end == false
  30.         {  
  31.          num = sc.nextInt(); 
  32.             if(num !=0000) 
  33.          { 
  34.               for(int j = 0;j < numarr.length;j++) 
  35.                 { 
  36.                   numarr[0] = num /1000; 
  37.                  numarr[1] = (num % 1000)/100; 
  38.                   numarr[2] = ((num % 1000)%100)/10; 
  39.                  numarr[3] = ((num % 1000)%100)%10; 
  40.              } 
  41.                
  42.             
  43.                for(int x = 0;x < ansarr.length;x++) 
  44.                 { 
  45.                   for(int y = 0;y < numarr.length;y++) 
  46.                     { 
  47.                       if(x==y) 
  48.                        { 
  49.                           if(ansarr[x] == numarr[y]) 
  50.                          { 
  51.                               A++; 
  52.                            } 
  53.                       } 
  54.                       else 
  55.                        { 
  56.                           if(ansarr[x] == numarr[y]) 
  57.                          { 
  58.                               B++; 
  59.                            } 
  60.                       } 
  61.                   } 
  62.               } 
  63.               System.out.println(A + "A" + B + "B"); 
  64.              A=0; 
  65.                B=0;     
  66.            } 
  67.           else 
  68.            { 
  69.               end = true
  70.             } 
  71.       } 
  72.   } 
  73. }  

 
   C++

  1. #include <iostream> 
  2. using namespace std; 
  3.  
  4. int main() { 
  5.    // 題目27. 猜數字 
  6.    string ans; 
  7.     string guess; 
  8.   int A = 0, B = 0; 
  9.   cin >> ans; 
  10.   while(cin >> guess) 
  11.   { 
  12.       if(guess != "0000"
  13.         { 
  14.           for(int i = 0;i < 4;i++) 
  15.             { 
  16.               for(int j = 0;j < 4;j++) 
  17.                 { 
  18.                   if(i == j && ans[i] == guess[j]) 
  19.                    { 
  20.                       A++; 
  21.                    } 
  22.                   else if(i != j && ans[i] == guess[j]) 
  23.                   { 
  24.                       B++; 
  25.                    } 
  26.               } 
  27.           } 
  28.           cout << A << "A" << B << "B" << endl; 
  29.             A = 0; 
  30.          B = 0; 
  31.      } 
  32.       else 
  33.        { 
  34.           break
  35.      } 
  36.   } 
  37.   return 0; 
  38. }  


沒有留言:

張貼留言