2017年12月11日 星期一

[C_SL16-中] 計算z-order(C++)

[C_SL16-中] 計算z-order

C_SI16-3.JPG
C_SI16-2.JPG
C_SI16-1.JPG
請你寫一個程式,輸入一個格子的編號,並計算出這個格子的 z-order
Input File Format輸入的第一行為一個整數 n ,代表底下有幾筆測資。接著共有 n 行的測資。每筆測資中包含了 2 個整數(以 10 進位表示),這兩個整數以逗號隔開。第一個整數為 column 編號。第 2 個整數為 row 編號。兩個整數間沒有空白。
Output Format輸出每個測資所對應的 z-order 值。每一行為一個 z-order 值。
Example
Sample Input:Sample Output:
2
1,1
1,2
3
9


  1. #include <iostream>    
  2. #include <string>    
  3. #include <sstream>    
  4. #include <cmath>    
  5. using namespace std;    
  6.     
  7. int main() {    
  8.     // [C_SL16-中] 計算z-order    
  9.     // g++(c++11)   
  10.     int n;    
  11.     cin >> n;    
  12.     for(int i = 0; i < n;i++)    
  13.     {    
  14.         string input;    
  15.         cin >> input;    
  16.         stringstream ss(input);    
  17.         string token;    
  18.         int count = 0;    
  19.         string col, row;    
  20.         //字串切割    
  21.         while (getline(ss, token, ','))    
  22.         {    
  23.             //先進來的是col, 後進來的是row    
  24.             if(count == 0)    
  25.             {    
  26.                 col = token;    
  27.             }    
  28.             else if(count == 1)    
  29.             {    
  30.                 row = token;    
  31.             }    
  32.             count++;    
  33.         }    
  34.             
  35.         int i_col = stoi(col);    
  36.         int i_row = stoi(row);    
  37.         int bin[16];//交錯完結果    
  38.         int arr_col[8];//col二進位    
  39.         int arr_row[8];//row二進位    
  40.         fill(arr_col, arr_col+8, 0);    
  41.         fill(arr_row, arr_row+8, 0);    
  42.         int num = i_col;    
  43.         //10進位轉2進位    
  44.         for(int j = 7;j >= 0;j--)//column    
  45.         {    
  46.             if(num >= pow(2,j))    
  47.             {    
  48.                 num = num - pow(2,j);    
  49.                 arr_col[7-j]++;    
  50.             }    
  51.         }    
  52.         num = i_row;    
  53.         for(int j = 7;j >= 0;j--)//row    
  54.         {    
  55.             if(num >= pow(2,j))    
  56.             {    
  57.                 num = num - pow(2,j);    
  58.                 arr_row[7-j]++;    
  59.             }    
  60.         }    
  61.         count = 0;    
  62.         //交錯    
  63.         for(int j = 1;j < 16;j+=2)//column    
  64.         {    
  65.             bin[j] = arr_col[count];    
  66.             count++;    
  67.         }    
  68.         count = 0;    
  69.         for(int j = 0;j < 16;j+=2)//row    
  70.         {    
  71.             bin[j] = arr_row[count];    
  72.             count++;    
  73.         }    
  74.         //將交錯結果由2進位轉成10進位    
  75.         int result = 0;    
  76.         for(int j = 0;j < 16;j++)//fin    
  77.         {    
  78.             result = result + bin[j]*pow(2,(15-j));    
  79.         }    
  80.         cout << result << endl;    
  81.         fill(bin, bin+16, 0);    
  82.     }    
  83.     return 0;    
  84. }  

沒有留言:

張貼留言