# 111 APCS羅工初階班
# 8/1(一)
## Hello World!
```c++=
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!";
return 0;
}
```
$$result\downarrow$$
```
Hello World!
```
## Print a triangle
```c++=
#include <iostream>
using namespace std;
int main()
{
cout << "*\n";
cout << "**\n";
cout << "***";
return 0;
}
```
$$result\downarrow$$
```
*
**
***
```
## Hello World! 2
```c++=
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1,str2;
str1 = "Hello";
str2 = "World";
cout << str1 << " "<< str2 <<"!";
return 0;
}
```
$$result\downarrow$$
```
Hello World!
```
## Hello World! 3
```c++=
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1,str2,str3;
str1 = "Hello ";
str2 = "World!";
str3 = str1 + str2;
cout << str3;
return 0;
}
```
$$result\downarrow$$
```
Hello World!
```
## Hello World! 4
```c++=
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
cout << "Enter some word: ";
cin >> str;
cout << "word: " << str;
return 0;
}
```
$$result\downarrow$$
```
Enter some word: Kenneth
word: Kenneth
```
> ***包含空格的字串存入str**
```c++=
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
cout << "Enter some word: ";
getline(cin,str);
cout << "word: " << str;
return 0;
}
```
# 8/2(二)
## 常見的資料型態
| Data Types | Size(bytes) | Range |
|:---------- |:-----------:|:------------------------------ |
| bool | 1 | false ~ true |
| char | 1 | -128 ~ 127 |
| short | 2 | -32768 ~ 32767 |
| int | 4 | -2,147,483,648 ~ 2,147,483,647 |
| float | 4 | 小數 |
| double | 8 | 更多小數 |
| long | | 變長 |
| long long | | 變更長 |
| unsigned | | 沒有負號 |
詳細鏈結
https://docs.microsoft.com/zh-tw/cpp/cpp/data-type-ranges?view=msvc-170
## 練習
```c++=
#include <iostream>
using namespace std;
int main()
{
int a,b;
while(1)
{
cout << "Enter a and b (use space to spare two num): ";
cin >> a >> b;
cout << "a+b :" << a+b <<"\n";
cout << "a-b :" << a-b <<"\n";
cout << "a*b :" << a*b <<"\n";
cout << "a/b :" << a/b <<"\n";
}
}
```
###### (用 Ctrl+C結束程式)
#### 試試看其他的Data Types....
### BMI(公斤/公尺平方)
```c++=
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float kg,m;
cout << "Enter kg and meter: ";
cin >> kg>>m;
cout << "Your BMI is: "<< kg/pow(m,2);
}
```
### 四捨五入到整數
```c++=
#include <iostream>
using namespace std;
int main()
{
float f;
cout << "Enter f:";
cin >> f;
cout << int(f+0.5) << endl;
return 0;
}
```
### 無條件進位到整數
```c++=
#inclue <iostream>
using namespace std;
int main()
{
float f;//float 會有精度上的問題,改成double就沒有問題了!
cout << "Enter f:";
cin >> f;
cout << int(f+0.9) << endl;
return 0;
}
```
### BMI做四捨五入到整數
```c++=
#inclue <iostream>
using namespace std;
int main()
{
float kg,m;
cout << "Enter kg and meter: ";
cin >> kg>>m;
cout << "Your BMI is: "<< int(kg/pow(m,2)+0.5)<<endl;
return 0;
}
```
# 8/3(三)
## 比較,邏輯運算子
| 運算 | 運算子 | 例子 |
| -------- |:------:| -------- |
| 等於 | == | a == b |
| 不等於 | != | a != b |
| 大於 | > | a > b |
| 小於 | < | a < b |
| 大於等於 | >= | a >= 0 |
| 小於等於 | <= | a <= 9 |
| AND | && | a && b |
| OR | \|\| | (a\=\=1) \|\| (b\=\=2) |
| NOT | ! | !(a+b>3) |
## if-else
```c++=
#include <iostream>
using namespace std;
int main()
{
if (條件式A)
{
若為真執行該區塊程式碼......
}
else if (條件式B)
{
若為未達條件A且條件式B為真,執行該區塊程式碼.....
}
else
{
若都為假則執行該區塊程式碼.....
}
}
```
## switch
```c++=
#include <iostream>
using namespace std;
int main()
{
int num = 0;
cin >> num;
switch (num)
{
case 1:
cout << 1;
break;
case 2:
cout << 2;
break;
default:
cout << "out of range";
break;//可加可不加
}
}
```
## 練習
### 閏年判斷
```c++=
#include <iostream>
using namespace std;
int main()
{
int num = 0;
if (num% 400 == 0)
{
cout << "Yes\n";
}
else if(num % 4 ==0 && (num % 100 != 0))
{
cout << "Yes\n";
}
else
{
cout << "No\n";
}
}
```
### 猜答案
```c++=
#include <iostream>
using namespace std;
int main()
{
char c;
cin >> c;
switch (c)
{
case 'A':
cout << "Correct";
break;
cout << 2;
break;
default:
cout << "wrong";
break;//可加可不加
}
}
```
### 星期的日期與英文轉換
```c++=
#include <iostream>
using namespace std;
int main()
{
int day = 0;
cin >> day;
switch (day)
{
case 1:
cout << "Mon" << endl;
break;
case 2:
cout << "Tue" << endl;
break;
case 3:
cout << "Wed" << endl;
break;
case 4:
cout << "Thu" << endl;
break;
case 5:
cout << "Fri" << endl;
break;
case 6:
cout << "Sat" << endl;
break;
case 7:
cout << "Sun" << endl;
break;
default:
cout << "out of range\n";
break;
}
return 0;
}
```
### 尋找最接近的正整數平方根
```c++=
#include <iostream>
using namespace std;
int main()
{
int i = 1,num = 17;
while(i < num)
{
cout << i << " * " << i << " : " << i * i <<endl;
if (i * i == num)
{
cout << "answer root: " << i;
break;
}
else if (i * i > num)
{
int tmp = i - 1;
if (num - tmp * tmp < i * i - num)
{
cout << "answer root: " << tmp << endl;
}
else
{
cout << "answer root: " << i << endl;
}
cout << num << " - " << tmp << " * " << tmp << " = " << num - tmp * tmp <<endl;
cout << i << " * " << i << " - " << num << " = " << i * i - num <<endl;
break;
}
i++;
}
return 0;
}
```
# 8/4(四)
## while
```c++=
#include <iostream>
using namespace std;
int main()
{
int i = 0;
while(i < 10)
{
cout << i++ << "\n";
}
return 0;
}
```
## do-while
```c++=
#include <iostream>
using namespace std;
int main()
{
int i = 0;
do
{
cout << i++ << "\n";
}while(i < 10);
return 0;
}
```
## for
```c++=
#include <iostream>
using namespace std;
int main()
{
for (int i=0;i<10;i++)
{
cout << i << "\n";
}
return 0;
}
```
## 練習
### 找質數
```c++=
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int num = 0,k=0;
for (int j=2;j<100; j++)
{
for (int i=2; i<=sqrt(j); i++)
{
k=0;
if (j % i == 0)
{
k++;
break;
}
}
if (!k)
{
cout << j << "is prime!\n";
}
}
}
```
### 找最大公因數
```c++=
#include <iostream>
using namespace std;
int main()
{
int a=0,b=0;
cin >> a >> b;
if (b)
{
while((a%=b) && (b%=a)){}
}
cout << a+b;
return 0;
}
```
### 找最小公倍數
```c++=
#include <iostream>
using namespace std;
int main()
{
int a=0,b=0;
cin >> a >> b;
int aa = a, bb = b;
if (b)
{
while((a%=b) && (b%=a)){}
}
int c = a+b;
cout << aa*bb / c;
return 0;
}
```
### 九九乘法表
```c++=
#include <iostream>
using namespace std;
int main()
{
for (int i=1;i<10;i++)
{
for (int j=1;j<10;j++)
{
cout << j << "*" << i << "=" << i * j << "\t";
}
cout << endl;
}
}
```
### 列印三角形(右下)
```c++=
#include <iostream>
using namespace std;
int main()
{
for (int i=0;i<6;i++)
{
for (int j=5;j>=0;j--)
{
if (i>=j)
{
cout << " ";
}
else
{
cout << "*";
}
}
cout << endl;
}
return 0;
}
```
### 列印三角形(左上)
```c++=
#include <iostream>
using namespace std;
int main()
{
for (int i=0;i<6;i++)
{
for (int j=5;j>=0;j--)
{
if (i>=j)
{
cout << "*";
}
else
{
cout << " ";
}
}
cout << endl;
}
return 0;
}
```
### 列印三角形(右上)
```c++=
#include <iostream>
using namespace std;
int main()
{
for (int i=0;i<6;i++)
{
for (int j=0;j<6;j++)
{
if (i>=j)
{
cout << "*";
}
else
{
cout << " ";
}
}
cout << endl;
}
return 0;
}
```
### 列印三角形(左下)
```c++=
#include <iostream>
using namespace std;
int main()
{
for (int i=0;i<6;i++)
{
for (int j=0;j<6;j++)
{
if (i>=j)
{
cout << " ";
}
else
{
cout << "*";
}
}
cout << endl;
}
return 0;
}
```
# 8/5(五)
## 一維陣列
```c++=
#include <iostream>
using namespace std;
int main()
{
int arr[10];
for (int i=0;i<10;i++)
{
arr[i] = i+1;
}
for (int i=0;i<10;i++)
{
cout << arr[i];
}
}
```
$$result\downarrow$$
```
1
2
3
4
5
6
7
8
9
10
```
## 二維陣列
```c++=
#include <iostream>
using namespace std;
int main()
{
int arr[10][10];
for (int i=0;i<10;i++)
{
for (int j=0;j<10;j++)
{
arr[i][j] = i * 10 + (j + 1);
}
for (int j=0;j<10;j++)
{
cout << arr[i][j] << " ";
}
}
cout << endl;
}
```
$$result\downarrow$$
```
1 2 3 4 5 6 7 8 9 10
11 12 ...
...
91 92 93 94 95 96 97 98 99 100
```
## vector
```c++=
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int>arr_1(5);//一維陣列宣告
vector<vector<int>>arr_2(5);//二維陣列宣告
for (int i=0;i<5;i++)
{
arr_2[i].resize(5);
}
}
```
## 練習
### 九九乘法表
```c++=
#include <iostream>
#include <string>
using namespace std;
int main()
{
int arr[9][9];
for (int i=0;i<9;i++)
{
for (int j =0;j<9;j++)
{
arr[i][j] = (j+1) * (i+1);
}
}
for (int i=0;i<9;i++)
{
for (int j =0;j<9;j++)
{
cout << j+1 << "*" << i+1 << "=" << arr[i][j] << "\t";
}
cout <<endl;
}
}
```
### 月份數字英文轉換
```c++=
#include <iostream>
#include <string>
using namespace std;
string month[12]{"Jan","Feb","Mar","Apr","May","Jun","July","Aug","Sep","Oct","Nov","Dec"};
int main()
{
int num;
while(cin >> num)
{
cout << month[num-1]<<endl;
}
}
```
### 泡沫排序
```c++=
#include <iostream>
using namespace std;
int main()
{
int arr[10];
for (int i=0;i<10;i++)
{
cin >> arr[i];
}
for (int i=1;i<10;i++)
{
for (int j=0;j<i;j++)
{
if (arr[i] < arr [j])
{
swap(arr[i],arr[j]);
}
}
}
for (int i=0;i<10;i++)
{
cout << arr[i] << endl;
}
}
```
### Couting number
> 輸入M個數字,0<M<20,輸出由大到小排列,並數出每個數字各輸入了幾次。輸入的數字為N,-100 < N < 100
>輸入:
```
-3 1 -3 -5 0 5 0 5 -7 8
```
>輸出:中間間隔用"\t"隔開
```
N Count
8 1
5 2
1 1
0 2
-3 2
-5 1
-7 1
```
```c++=
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int num;
vector<int> arr;
while (cin >> num)
{
arr.push_back(num);
}
for (int i = 1; i < arr.size(); i++)
{
for (int j = 0; j < i; j++)
{
if (arr[i] > arr[j])
{
swap(arr[i], arr[j]);
}
}
}
vector<pair<int, int>> output;
output.push_back(make_pair(arr[0],1));
for (int i = 1; i < arr.size(); i++)
{
if (arr[i] == arr[i - 1])
{
output[output.size() - 1].second++;
}
else
{
output.push_back(make_pair(arr[i], 1));
}
}
cout << "N\tcount\n";
for (int i=0;i<output.size();i++)
{
cout << output[i].first << "\t" << output[i].second << "\n";
}
}
```
```c++=
#include <iostream>
#include <vector>
#include <set>
using namespace std;
int main()
{
long long int num;
vector<long long int>AllNum;
set<long long int>Num;
while (cin >> num)
{
AllNum.push_back(num);
Num.insert(num);
}
cout << "N\tcount" << endl;
for (auto i = Num.rbegin(); i != Num.rend(); i++)
{
int tmp = 0;
for (int j = 0; j < AllNum.size(); j++)
{
if (*i == AllNum[j])
{
tmp++;
}
}
cout << *i << "\t" << tmp << endl;
}
}
```
# 8/8(一)
## scope
```c++=
#include <iostream>
using namespace std;
int a = 5;
int main()
{
cout << a << "\n"; //output 5
int a = 4;
{
cout << a << "\n"; //output 4
int a = 3;
cout << a << "\n"; // output3
}
cout << a << "\n"; // output 4
}
void function()
{
cout << a << "\n";
}
```
## Function
```c++
data_type function_name(parameter)
{
int a = 5; //argument
return data_type();
}
int function(char c)
{
...
return int();
}
```
## 範例
```c++=
#include <iostream>
#include <string>
using namespace std;
void f(string str);
int main()
{
string str;
cout << "enter your name:";
cin >> str;
f(str);
return 0;
}
void f(string str)
{
cout << "Hi~" << str << endl;
}
```
```c++=
#include <iostream>
#include <string>
using namespace std;
float f(float kg,float m);
int main()
{
float kg, m;
cout << "enter your weight(kg) and hight(meter):";
cin << kg << m;
cout << "Your BMI is:"" << f(kg, m);
}
float f(float kg, float m)
{
return (kg / (m * m));
}
```
## 練習
### 階乘運算
```c++=
#include <iostream>
using namespace std;
int factorial(int num);
int main()
{
int n = 0;
while(1)
{
cin >> n;
cout << factorial(n) << endl;
}
}
int factorial(int num)
{
int ans = 1;
for (int i=1;i<=num;i++)
{
ans *= i;
}
return ans;
}
```
# 8/9(二)
## recursion
```c++=
#include <iostream>
using namespace std;
void f(int n);
int main()
{
f(0);
return 0;
}
void f(int n)
{
if (n < 5)
{
cout << "*\n";
n++;
f(n);
}
return;
}
```
$$result\downarrow$$
```
*
*
*
*
*
```
## 練習
### Fibonacci 費氏數列
```c++=
#include<iostream>
using namespace std;
int f(int n);
int main()
{
cout << f(10);
}
int f(int n)
{
if (n == 1 || n == 2)
{
return 1;
}
return f(n-1) + f(n-2);
}
```
### 階乘運算
```c++=
#include <iostream>
using namespace std;
int factorial(int n);
int main()
{
int n;
while (cin >> n)
{
cout << factorial(n) << endl;
}
}
int factorial(int n)
{
if (n == 1)
{
return 1;
}
return n * factorial(n - 1);
}
```
### 最大公因數
```c++=
#include <iostream>
using namespace std;
int GCD(int a,int b);
int main()
{
int a,b;
while(cin >> a >> b)
{
if(b)
cout << GCD(a,b) << endl;
else
cout << 0;
}
}
int GCD(int a,int b)
{
if(!b)
{
return a;
}
return GCD(b,a%b);
}
```
### 遞迴小題目 判斷n最後等於多少
```c++=
#include <iostream>
using namespace std;
int n = 0;
void k(int b);
void g(int m);
int main()
{
g(100);
cout << n;
}
void k(int b)
{
n++;
if (b%4)
{
k(b+1);
}
}
void g(int m)
{
for (int i=0;i<m;i++)
{
k(i);
}
}
```
# 8/10(三)
## 練習
### +\-*/判斷運算
| 輸入 | 輸出 |
|:------ |:---- |
| 2 * 2 | 4 |
| 12 / 8 | 1.5 |
| 8 - 2 | 6 |
| 5 + 3 | 8 |
| 5 ^ 3 | 125 |
| 6 ! | 720 |
```c++=
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int num1 = 0, num2 = 0;
char c;
while (cin >> num1 >> c)
{
if (c == '+' || c == '-' || c == '/' || c == '*' || c == '/' || c == '^')
{
cin >> num2;
switch (c)
{
case '+':
cout << num1 + num2 << endl;
break;
case '-':
cout << num1 - num2 << endl;
break;
case '*':
cout << num1 * num2 << endl;
break;
case '/':
cout << double(num1) / num2 << endl;
break;
case '^':
cout << pow(num1, num2) << endl;
break;
}
}
else
{
int tmp = 1;
for (int i = 1; i <= num1; i++)
{
tmp *= i;
}
cout << tmp << endl;
}
}
}
```
```c++=
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int num1 = 0, num2 = 0;
char c;
while (cin >> num1 >> c)
{
double ans = 0;
if (c!='!')
{
cin >> num2;
switch (c)
{
case '+':
ans = num1+num2;
break;
case '-':
ans = num1-num2;
break;
case '*':
ans = num1*num2;
break;
case '/':
ans = double(num1)/num2;
break;
case '^':
ans = double(pow(num1, num2));
break;
}
}
else
{
ans = 1;
for (int i = 1; i <= num1; i++)
{
ans *= i;
}
}
cout << ans << "\n";
}
}
```
### 零錢系統
>優先使用最大幣值,依序變小。
>輸入:87
>輸出:(中間用"\t"隔開)
```
50: 1
10: 3
5: 1
1: 2
```
```c++=
#include <iostream>
using namespace std;
int main()
{
int num = 0;
while (cin >> num)
{
cout << "50:\t" << num / 50 << endl;
num %= 50;
cout << "10:\t" << num / 10 << endl;
num %= 10;
cout << "5:\t" << num / 5 << endl;
num %= 5;
cout << "1:\t" << num << endl;
}
}
```
### 逆向輸出
>將所有輸入逆向輸出,空格不輸出。
| 輸入 | 輸出 |
|:--------- |:-------- |
| 12345 | 54321 |
| 1a1b2s | s2b1a1 |
| ASDASD | DSADSA |
| P@SS W0Rd | dR0WSS@P |
```c++=
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
while(getline(cin,str))
{
for (int i = str.size() - 1 ; i >=0;i--)
{
if (str[i]==' ')
{
continue;
}
cout << str[i];
}
cout << "\n";
}
}
```
# 8/11(四)
## 練習
### 座標翻轉
>輸入:2 3
>輸出:
```
2 3
3 -2
-2 -3
-3 2
```

# 8/12(三)
##APCS 考古題
### h081程式交易 https://zerojudge.tw/ShowProblem?problemid=h081
```c++=
#include<iostream>
using namespace std;
int main()
{
int n,D;
int money=0,tmp=0,have=1;
cin>>n>>D;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
for(int i=1;i<n;i++)
{
if(arr[i]>=arr[tmp]+D&&have)
{
money+=arr[i]-arr[tmp];
tmp=i;
have=0;
}
else if(arr[tmp]-D>=arr[i]&&!have)
{
tmp=i;
have=1;
}
}
cout<<money;
}
```
### h083數位占卜 https://zerojudge.tw/ShowProblem?problemid=h083
```c++=
h083.
#include<iostream>
#include<string>
using namespace std;
int main(){
string ans[10000];
string aa,bb,cc;
int a,t,coutans=0;
cin>>a;
for(int i=0;i<a;i++){
cin>>ans[i];
}
for(int i=0;i<a;i++){
for(int j=0;j<a;j++){
aa="";
bb="";
cc="";
cc=ans[i]+ans[j];
if(i!=j && cc.length()%2==0){
for(int k=0;k<((cc.length()/2));k++){
aa=aa+cc[k];
}
for(int k=(cc.length()/2);k<cc.length();k++){
bb=bb+cc[k];
}
cout<<cc<<" "<<aa<<" "<<bb<<endl;
if(aa==bb){
coutans=coutans+1;
}
}
}
}
cout<<coutans/2;
}
```
### h084牆上海報 https://zerojudge.tw/ShowProblem?problemid=h084
```c++=
#include <iostream>
using namespace std;
// n:有幾個柵欄 m:有幾張海報 w:每張海報的寬度 h:每根柵欄的高度 highest:柵欄最高高度 total:海報總寬度
bool greedy(unsigned int hight, unsigned int h[], unsigned int h_size, unsigned int start, unsigned int w[], unsigned int w_size, int index);
int main()
{
//---------------------input----------------------
unsigned int n = 0, m = 0, highest = 0, total = 0;
cin >> n >> m;
unsigned int h[n];
unsigned int w[m];
// h[i]每個柵欄的高度
for (int i = 0; i < n; i++)
{
cin >> h[i];
if (h[i] > highest)
{
highest = h[i];
}
}
// w[i]每張海報的寬度
for (int i = 0; i < m; i++)
{
cin >> w[i];
total += w[i];
}
//--------------------process-----------------
int ans = 0;
for (int i = highest; i >= 1; i--) // i:目前高度
{
bool flag = false;
int j = 0;
while (j < n && !flag) // j:第幾根柵欄
{
if (h[j] >= i) //判斷第j根柵欄的高度是否符合i
{
if (greedy(i, h, n, j, w, m, 0))
{
flag = true;
ans = i;
break;
}
else
j++;
}
else
j++;
}
if (flag)
break;
}
//--------------------output------------------
cout << ans;
}
bool greedy(unsigned int hight, unsigned int h[], unsigned int h_size, unsigned int start, unsigned int w[], unsigned int w_size, int index)
{
// hight , 目前高度 , h_size 柵欄的總數 , start 起始柵欄 , w_size 海報數 , index 第幾張海報
//如果已經塞不下這一張海報 return false
if (h_size - start <= w[index])
{
return false;
}
//如果這組柵欄塞不下海報 return false 塞的下就往後走
for (int i = 1; i < w[index]; i++)
{
if (h[++start] < hight)
{
return false;
}
}
//目前的start還在貼海報的最後一個柵欄 所以先把start往下一根走
// index 因為已經貼完海報了,所以把index++;
start++;
index++;
//如果已經貼完海報了就return true
if (index >= w_size)
{
return true;
}
//如果目前這根柵欄高度比目前高度還要低,就往下一根走,直到找到下一根符合高度的柱子,或是走到底了
while (h[start] < hight && start < h_size)
{
start++;
}
//如果柵欄走到底了 return false
if (start >= h_size)
{
return false;
}
//查看下一張海報能不能放進去
return greedy(hight, h, h_size, start, w, w_size, index);
}
```
### i399數字遊戲 https://zerojudge.tw/ShowProblem?problemid=i399
```c++=
i399.
#include<iostream>
using namespace std;
int main(){
int a=3,t;
int ans[3];
for(int i=0;i<a;i++){
cin>>ans[i];
}
for(int i=0;i<a;i++){
for(int j=0;j<a;j++){
if(ans[i]>ans[j]){
t=ans[i];
ans[i]=ans[j];
ans[j]=t;
}
}
}
if((ans[0]==ans[1] && ans[1]==ans[2])){
cout<<3<<" ";
}
else if(((ans[0]==ans[1] && ans[1]!=ans[2])) || ((ans[0]!=ans[1] && ans[1]==ans[2]))){
cout<<2<<" ";
}
else{
cout<<1<<" ";
}
for(int i=0;i<a;i++){
cout<<ans[i]<<" ";
}
}
```
### i400 字串解碼 https://zerojudge.tw/ShowProblem?problemid=i400
```c++=
i400
#include<iostream>
#include<string>
using namespace std;
int main(){
int a,b,t,z,z1;
string ans[10000];
string en,en1;
cin>>a>>b;
for(int i=0;i<a;i++){
cin>>ans[i];
}
cin>>en;
for(int i=a-1;i>=0;i--){
t=0;
z=0;
z1=b-1;
en1=en;
en="";
for(int j=b-1;j>=0;j--){
if(ans[i][j]=='1'){
en=en+en1[z1];
}
else if(ans[i][j]=='0'){
en=en1[z1]+en;
}
z1=z1-1;
}
for(int j=0;j<b;j++){
if(ans[i][j]=='1'){
t=t+1;
}
}
if(t%2!=0 && en.length()%2!=0){
en1="";
for(int k=((en.length()/2)+1);k<(en.length());k++){
en1=en1+en[k];
}
en1=en1+en[en.length()/2];
for(int k=0;k<((en.length()/2));k++){
en1=en1+en[k];
}
}
else if(t%2!=0 && en.length()%2==0){
en1="";
for(int k=((en.length()/2));k<(en.length());k++){
en1=en1+en[k];
}
for(int k=0;k<((en.length()/2));k++){
en1=en1+en[k];
}
}
}
cout<<en1;
}
```