2017年5月25日 星期四

[Uva]1225 - Digit Counting

[Uva]1225 - Digit Counting

這也是2017/5/23的CPE第一題考題

中文大意
第一行輸入一個測試次數 time
給一個整數N(範圍:1< N <10000)
假設N = 13
請計算出0 ~ 9出現的次數
N = 13 => 12345678910111213
要統計的數字    0   1   2   3   4   5   6   7   8   9
出現次數            1   6   2   2   1   1   1   1   1   1

N = 24  => 123456789101112131415161718192021222324
要統計的數字    0   1   2   3   4   5   6   7   8   9
出現次數            2  13  8   3   3   2   2   2   2   2

為了計算數量所以宣告一個 arrcount 的陣列
每統計完一個N後要記得歸零陣列,不然數字會一直往上加喔

程式碼如下
import java.util.*;
import java.lang.*;

public class DigitCounting {

public static void main(String[] args) {
int N ; //待測數字
int time;  //測試次數
long arrcount[] = new long[10];  //統計數量的陣列

Scanner sc = new Scanner(System.in);
time = sc.nextInt();  //輸入次數
for(int i = 0;i < time;i++)
{
N = sc.nextInt(); //輸入待測數字
if(N > 1 && N < 10000)  //判斷N是否在範圍內
{
for(int j = 1;j <= N;j++)
                                //開始計算數量,從1開始,每個數的個位、十位、百位...分開統計
{
if(j < 10)
{
arrcount[j]++;
}
if(j >= 10 && j < 100)
{
arrcount[j/10]++;
arrcount[j%10]++;
}
if(j >= 100 && j < 1000)
{
arrcount[j/10/10]++;
arrcount[j/10%10]++;
arrcount[j%10]++;
}
if(j >= 1000 && j < 10000)
{
arrcount[j/1000]++;
arrcount[j/100%10]++;
arrcount[j/10%10]++;
arrcount[j%10]++;
}

}
for(int k = 0;k < arrcount.length;k++)
                                 //輸出陣列中統計完的結果
{
if(k == (arrcount.length-1))
{
System.out.println(arrcount[k]);
                                                //最後一個數字要換行
}
else
{
System.out.print(arrcount[k] + " ");
                                                //每個數字之間要留空白
}
}

for(int x = 0;x < arrcount.length;x++)  //陣列歸零
{
arrcount[x] = 0;
}
}
}
}
}


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. }