2017年11月30日 星期四

[C_AR01-易] 一維陣列反轉 I(C++)

[C_AR01-易] 一維陣列反轉 I

題目描述:
一維陣列反轉
輸入說明:
輸入一個一維陣列,元素最多不超過100個
輸出說明:
輸出反轉後的陣列
最後需有換行。
範例:

輸入範例:輸出範例:
4 6 3 69 234
56 89 23 3 1
176 5 890 643 2
0 500 6 634 55 123
87 77 32 22 111 4
234 69 3 6 4
1 3 23 89 56
2 643 890 5 176
123 55 634 6 500 0
4 111 22 32 77 87


  1. #include <iostream>  
  2. #include<sstream>  
  3. #include<algorithm>  
  4.   
  5. using namespace std;  
  6.   
  7. int main() {  
  8.     // [C_AR01-易] 一維陣列反轉 I  
  9.     string s;  
  10.     string str[100];//切割後放入str[]  
  11.     int count = 0; //計算有幾個數字  
  12.     while(getline(cin , s))//切割  
  13.     {  
  14.         stringstream ss(s);//將s字串塞入ss  
  15.         string token; //將切割後的結果存入token  
  16.         while (getline(ss, token, ' '))  
  17.         {  
  18.             str[count] = token;  
  19.             count++;  
  20.         }  
  21.         reverse(str, str+count); // 反轉陣列  
  22.         for(int i = 0;i < count;i++)  
  23.         {  
  24.             if(i != count-1)  
  25.             {  
  26.                 cout << str[i] << " ";  
  27.             }  
  28.             else  
  29.             {  
  30.                 cout << str[i] << endl;  
  31.             }  
  32.         }  
  33.         count = 0;  
  34.     }  
  35.     return 0;  
  36. }  

2017年11月28日 星期二

[ITSA Basic]題目29. 身分證驗證器(C++, Java)

題目29. 身分證驗證器

問題描述:
設計一個程式可以檢查身分證字號的正確性 ( 應檢查性別欄及檢查碼是否正確 ) 。
身分證字號共有十個碼,且有一定的編碼規則,其檢查編碼的規則如下:
11
其中檢查碼的計算方法如下:
Step 1: 根據下表查出第一碼的英文字母對應到的兩位數代號。
字母ABCDEFGHJKLMNPQRSTUVXYWZIO
代號1011121314151617181920212223242526272829303132333435
Step 2: 令此代號之十位數為 X1 ,個位數為 X2 。例如 Y 的代號 31 ,則 X1=3 ; X2=1 。
Step 3: 運用下面的公式計算之。如果 P 可以被 10 整除,則此組身份證號碼是對的,反之則是錯的。
CAR34
輸入說明:
輸入身分證字號,第一碼為英文大寫。
輸出說明:
若身分證字號正確,印出「CORRECT!!!」;不正確則印出「WRONG!!!」。
範例:
Sample InputSample Output
A123456789CORRECT!!!
L163690274WRONG!!!
C++

  1. #include <iostream>  
  2. #include <string.h>  
  3. #include <cmath>  
  4. using namespace std;  
  5.   
  6. int main() {  
  7.     // 題目29. 身分證驗證器  
  8.     string NUM;  
  9.     char char_num[10];  
  10.     char A[26] = {'A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','X','Y','W','Z','I','O'};  
  11.     int int_num[10];  
  12.     while(cin >> NUM)  
  13.     {  
  14.         strcpy(char_num, NUM.c_str());  
  15.         for(int i = 0;i < 10;i++)  
  16.         {  
  17.             if(i == 0)//代表身分證第一碼  
  18.             {  
  19.                 for(int j = 0;j < 26;j++)  
  20.                 //搜尋一樣的字,並把數字存入陣列  
  21.                 {  
  22.                     if(char_num[0] == A[j])  
  23.                     {  
  24.                         int_num[0] = j+10;  
  25.                     }  
  26.                 }  
  27.             }  
  28.             else  
  29.             {  
  30.                 int_num[i] = ((int)char_num[i]-48);  
  31.             }  
  32.         }  
  33.         //P=X1+(9*X2)+(8*N1)+(7*N2)+(6*N3)+(5*N4)+(4*N5)+(3*N6)+(2*N7)+N8+N9  
  34.         int P = floor(int_num[0]/10) + (9*(int_num[0]%10));//P=X1+(9*X2)  
  35.         for(int i = 1;i < 8;i++)  
  36.         //(8*N1)+(7*N2)+(6*N3)+(5*N4)+(4*N5)+(3*N6)+(2*N7)  
  37.         {  
  38.             P = P + (9-i)*int_num[i];  
  39.         }  
  40.         P = P + int_num[8] + int_num[9];//N8+N9  
  41.         if(P % 10 == 0)//輸出  
  42.         {  
  43.             cout << "CORRECT!!!" << endl;  
  44.         }  
  45.         else  
  46.         {  
  47.             cout << "WRONG!!!" << endl;  
  48.         }  
  49.     }  
  50.     return 0;  
  51. }  


JAVA

  1. import java.io.*; 
  2. public class Main 
  3.   public static void main(String args[]) throws Exception 
  4.     { 
  5.       String getID; 
  6.       int a,b; 
  7.        BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); 
  8.         
  9.         
  10.        while (true
  11.        { 
  12.           getID = br.readLine(); 
  13.           
  14.             
  15.            if (getID.equals("0")) 
  16.          { 
  17.               return
  18.             } 
  19.           a=0; 
  20.            getID =getID.toUpperCase(); 
  21.              
  22.             
  23.             
  24.            if (getID.charAt(0)=='A') a=10; 
  25.              
  26.                else if (getID.charAt(0)=='B') a=11; 
  27.                else if (getID.charAt(0)=='C') a=12; 
  28.                else if (getID.charAt(0)=='D') a=13; 
  29.                else if (getID.charAt(0)=='E') a=14; 
  30.                else if (getID.charAt(0)=='F') a=15; 
  31.                else if (getID.charAt(0)=='G') a=16; 
  32.                else if (getID.charAt(0)=='H') a=17; 
  33.                else if (getID.charAt(0)=='I') a=34; 
  34.                else if (getID.charAt(0)=='J') a=18; 
  35.                else if (getID.charAt(0)=='K') a=19; 
  36.                else if (getID.charAt(0)=='L') a=20; 
  37.                else if (getID.charAt(0)=='M') a=21; 
  38.                else if (getID.charAt(0)=='N') a=22; 
  39.                else if (getID.charAt(0)=='O') a=35; 
  40.                else if (getID.charAt(0)=='P') a=23; 
  41.                else if (getID.charAt(0)=='Q') a=24; 
  42.                else if (getID.charAt(0)=='R') a=25; 
  43.                else if (getID.charAt(0)=='S') a=26; 
  44.                else if (getID.charAt(0)=='T') a=27; 
  45.                else if (getID.charAt(0)=='U') a=28; 
  46.                else if (getID.charAt(0)=='V') a=29; 
  47.                else if (getID.charAt(0)=='W') a=32; 
  48.                else if (getID.charAt(0)=='X') a=30; 
  49.                else if (getID.charAt(0)=='Y') a=31; 
  50.                else if (getID.charAt(0)=='Z') a=33; 
  51.             
  52.                        a=(a%10)*9+a/10; 
  53.             
  54.             
  55.            b=0; 
  56.            b=(getID.charAt(1)-'0')*8+ 
  57.          (getID.charAt(2)-'0')*7+ 
  58.            (getID.charAt(3)-'0')*6+ 
  59.            (getID.charAt(4)-'0')*5+ 
  60.            (getID.charAt(5)-'0')*4+ 
  61.            (getID.charAt(6)-'0')*3+ 
  62.            (getID.charAt(7)-'0')*2+ 
  63.            (getID.charAt(8)-'0')*1; 
  64.             
  65.             
  66.            a=a+b; 
  67.          a=10-a%10; 
  68.          if (a >= 10)a = 0; 
  69.           { 
  70.               if (getID.charAt(9)-'0'==a) 
  71.                 { 
  72.                   System.out.println("CORRECT!!!"); 
  73.               } 
  74.               else 
  75.                { 
  76.                   System.out.println("WRONG!!!"); 
  77.                 } 
  78.           } 
  79.       } 
  80.   } 
  81. }