---
# System prepended metadata

title: 初階班能力檢定 題解
tags: [初階班]

---

---
tags: 初階班
---
# 初階班能力檢定 題解
## 第一題
應該不用多說甚麼吧
:::spoiler code
```python=
print('hello, world')
```
:::
## 第二題
就只是簡單的用```if```去判斷
:::spoiler code
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
	int a;
	cin >> a;
	if (a < 6)
		cout << 0;
	else if (a <= 11)
		cout << 590;
	else if (a <= 17)
		cout << 790;
	else if (a <= 59)
		cout << 890;
	else
		cout << 399;
}
```
:::
## 第三題
這題就去研究要如何判斷閏年
:::spoiler code
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
	int a, b;
	cin >> a >> b;
	int ans = 0;
	for (int i = a; i <= b; i++) {
		if (i % 400 == 0 || (i % 100 != 0 && i % 4 == 0))
			ans++;
	}
	cout << ans << '\n';
}
```
:::
## 第四題
就是判斷一堆數字的最大值，不過有兩種解法
:::spoiler 邊輸入邊判斷
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
	int n;
	cin >> n;
    int max = 0;
    int m;
    for (int i = 0; i < n; i++) {
        cin >> m;
        if (m > max) 
            max = m;
    }
    cout << max;
}
```
:::
:::spoiler sort
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
	int n;
	cin >> n;
    int arr[n];
	for (int i = 0; i < n; i++) cin >> arr[i];
	sort (arr, arr + n);	
    cout << arr[n  - 1];
}
```
:::
## 第五題
這一題要注意是```多筆測資```，一樣有兩種解法
:::spoiler array
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
	int n, m;
	while(cin >> n >> m) {
		int arr[n] = {0};
		int ans = 0;
		for(int i = 0; i < n; i++) {
			cin >> arr[i];
		}
		for(int i = 0; i < n; i++) {
			if( arr[i] <= m)
				ans++;
		}
		cout << ans << "\n";
	}
}
```
:::
:::spoiler 邊輸入邊判斷
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
	int n, m;
	while (cin >> n >> m) {
        int ans = 0;
        int a;
        for (int i = 0; i < n; i++) {
            cin >> a;
            if (a <= m) 
                ans++;
        }
        cout << ans << '\n';
    }   
}
```
:::
## 第六題
這題稍微有些難度，也是有兩種解法
:::spoiler string
```cpp=
#include <bits/stdc++.h>
using namespace std;
int main () {
	string str;
	while (getline(cin,str)) {
		string no = "我可以吃東西嗎?";
		int x = 0;
		for (int i = 0; i < str.length(); i++) {
			if (str[i] == ' ') {
				string ans;
				for (int j = x; j < i; j++) ans += str[j];
				if (ans == no) cout << "不行 ";
				else cout << "可以 ";
				x = i + 1;			
			}
			else if (i == str.length() - 1) {
				string ans;
				for (int j = x; j < str.length(); j++) ans += str[j];
				if (ans == no) cout << "不行 ";
				else cout << "可以 ";
				x = i + 1;	
			}
		}
		cout << '\n';
	}
}
```
:::
:::spoiler stringstream
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
	string str;
	while(getline(cin, str)) {
		stringstream ss;
		string ans;
		ss << str;
		while(ss >> ans) {
			if(ans == "我可以吃東西嗎?")
				cout << "不行 ";
			else
				cout << "可以 ";
		}
		cout << "\n";
	}
 }
```
:::
## 第七題
這一題考完試有講過，有問題再來問我
:::spoiler code
```cpp=
#include <bits/stdc++.h>
using namespace std;
int main() {
	long long a, b;
	cin >> a >> b;
	cout << (a + b + 1) / 2;
}
```
:::
## 第八題
這題其實很複雜，我當初也寫了十五分鐘以上吧，所以有寫出這一題很電 :zap:
然後我丟幾個的code給你們參考
思考過程叫出題者自己說，我懶得打 :poop:
:::spoiler fang
```cpp=
#include <bits/stdc++.h>
using namespace std;
#define IO cin.tie(0);ios_base::sync_with_stdio(false);
#define endl '\n';
#define ll long long int
bool cmp(pair<char, int> &x, pair<char, int> &y) {
	return x.first == y.first ? x.second < y.second : x.first < y.first;
}
int main() {
    IO
	int n, s;
	bool m;
	cin>>n>>m>>s;
	while(n--) {
		string str;
		cin>>str;
		vector<pair<char, int>> v;
		for(int i = 0; i < str.size(); i++)	v.push_back(make_pair(str[i], i + 1));
		for(auto &i : v)	if(i.first < 97)	i.first += 32;
		for(auto &i : v) {
			if(m) {
				if(i.first + (s % 26) > 122)	i.first = 'a' + (s % 26) - (122 - i.first) - 1;
				else	i.first += (s % 26);
			}
			else {
				if(i.first - (s % 26) < 97)	i.first = 'z' - (s % 26) + (i.first - 97) + 1;
				else	i.first -= (s % 26);
			}
		}
		sort(v.begin(), v.end(),cmp);
		cout<<!m<<' '<<s % 26<<endl
		for(auto &i : v)	cout<<i.second;
		cout<<endl
		for(auto &i : v)	cout<<i.first;
		cout<<endl
	}
}

```
:::
:::spoiler han
```cpp=
#include <bits/stdc++.h>
using namespace std;
int main () {
	ios::sync_with_stdio(0), cin.tie(0);
	int n, m, s;
	while (cin >> n >> m >> s) {
		s = s % 26;
		string str;
		while (n--) {
			cin >> str;
			int arr[str.size()];
			for (int i = 0; i < str.size(); i++) {
				str[i] = tolower(str[i]);
				arr[i] = int(str[i]);
			} 
			if (m) {
				for (int i = 0; i < str.size(); i++) {
					arr[i] += s;
					if (arr[i] > 122) arr[i] -= 26;
				}
			}
			else {
				for (int i = 0; i < str.size(); i++) {
					arr[i] -= s;
					if (arr[i] < 97) arr[i] += 26;
				}
			}
			string str2;
			for (int i = 0; i < str.size(); i++) {
				str2 += (char)arr[i];
			}
			cout << !m << ' ' << s << '\n';
			sort(arr, arr + str.size());
			for (int i = 0; i < str.size(); i++) {
				cout << str2.find((char)arr[i]) + 1;
				str2[str2.find((char)arr[i])] = 'A';
			}
			cout << '\n';
			for (int i = 0; i < str.size(); i++) cout << (char)arr[i];
			cout << '\n';
		}
	}
}
```
:::
:::spoiler tree
```cpp=
#include<bits/stdc++.h>
#define LL long long
#define f first
#define s second
using namespace std;
int main(){
	int n, m, s;
	cin >> n >> m >> s; cin.get();
	s %= 26;
	while(n--){
		string str; cin >> str;
		for(auto & i : str){
			if('A' <= i && i <= 'Z') i =i - 'A'+'a';
			i -= 'a';
		}
		if(m){
			for(auto & i : str) {
				i = (i + s) % 26 + 'a';
			}
		}
		else {
			for(auto & i : str) {
				i = (i - s + 26) % 26 + 'a';
			}
		}
		pair<char, int> p[str.size()];
		for(int x = 0; x < str.size(); x++){
			p[x].f = str[x];
			p[x].s = x + 1;
		}
		sort(p, p + str.size());
		cout << !m << ' ' << s<<'\n';
		for(int x = 0; x < str.size(); x++) cout << p[x].s;
		cout << '\n';
		for(int x = 0; x < str.size(); x++) cout << p[x].f;
		cout << '\n';
	}
	return 0;
}
```
:::
:::spoiler revival
```cpp=
#include <bits/stdc++.h>
using namespace std;

int mod(int x) {
	x %= 26;
	while(x < 0) x += 26;
	return x%26;
}

int main() {
	cin.tie(0), ios_base::sync_with_stdio(0);
	
	int n, m, s;
	cin >> n >> m >> s;
	while(n--) {
		string str;
		cin >> str;
		cout << !m << ' ' << mod(s) << '\n';
		for(int i = 0; i < str.size(); ++i) {
			if('A' <= str[i] && str[i] <= 'Z')
				str[i] = str[i] - 'A' + 'a';
		}
		for(auto& i : str) {
			int x = i;
			x -= 'a';
			if(m) x = mod(x+mod(s));
			else x = mod(x-mod(s));
			i = x+'a';
		}
		vector<vector<int>> cnt(26);
		for(int i = 0; i < str.size(); ++i) {
			cnt[str[i]-'a'].push_back(i+1);
		}
		for(auto& i : cnt)
			for(auto& j : i)
				cout << j;
		cout << '\n';
		sort(str.begin(), str.end());
		cout << str << '\n';
	}
}
```
:::
:::spoiler wildgeese
```cpp=
#include<iostream>
#include<string>
using namespace std;

int n,s;
int m=1;
int x=3;
string word,word_sort,word2;
int check[26];

void b_to_s();
void tran();
void sort();
void position();
void reset_check();

int main(){
	cin >> n >> m >> s;
	s=s%26;
	for(int j=0;j<n;j++){
		cin >> word;
		b_to_s();
		tran();
		word2=word;
		word_sort=word;
		sort();
		reset_check();
		
		if(m==0)
			x=1;
		else
			x=0;
		cout << x << " "<< s << '\n';
		position();
		cout << '\n';
		cout << word_sort << '\n';
	}
	
	return 0;
}

void position(){
	int t=0;
	for(int k=0;k<word.length();k++){
//		cout << " /" << check[word_sort[k]-97] << "/ ";
		t=word.find(word_sort[k],check[word_sort[k]-97]);
		cout << t+1;
		check[word_sort[k]-97]=t+1;
	}
	return;
}

void sort(){
	for(int k=0;k<word2.length();k++){
		int min=0;
		for(int m=0;m<word2.length();m++){
			if(word2[min]>word2[m]){
				min=m;	
			}
		}
		word_sort[k]=word2[min];	
		word2[min]=126;
	}
	return;
}

void tran(){
	for(int i = 0;i<word.length();i++){
		if(m==0){
			word[i]-=s;
		}else{
			word[i]+=s;
		}
		
		if(word[i]<97)          
			word[i]+=26;
		if(word[i]>122)
			word[i]-=26;
	}
	return;
}

void b_to_s(){
	for(int i = 0;i<word.length();i++){
		if(word[i]<97)
			word[i]+=32;
	}
	return;
}

void reset_check(){
	for(int ii=0;ii<26;ii++)
		check[ii]=0;
	return;
}
```
:::

## 第九題
這一題應該是整個測驗最難的
欣賞大家怎麼寫就好
:::spoiler han
```cpp=
#include <bits/stdc++.h>
using namespace std;
int main () {
	//ios::sync_with_stdio(0), cin.tie(0);
	int k, n;
	while (cin >> k >> n) {
		if (k == 0 && n == 0) return 0;
		vector <int> vec(n + 1, 0);
		vec[0] = 1;
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j < min(k ,i) + 1; j++) {
				vec[i] += vec[i - j];
			}
		}
		cout << vec[n] << '\n';
	}
}
```
:::
:::spoiler tree
```cpp=
#include<bits/stdc++.h>
#define LL long long
using namespace std;
int main(){
	LL m, n;
	while(cin >> m >> n){
		if(m == 0 && n == 0) return 0;
		LL arr[n + 1]; arr[0] = 1;
		for(int x = 1; x <= n; x++){
			if(x <= m) arr[x] = pow(2, x - 1);
			else arr[x] = arr[x - 1] * 2 - arr[x - m - 1];
		}
		cout << arr[n] << '\n';
	}
	return 0;
}
```
:::
:::spoiler revival
```cpp=
#include <bits/stdc++.h>
using namespace std;

int main() {
	//cin.tie(0), ios_base::sync_with_stdio(0);
	
	int k, n;
	while(cin >> k >> n) {
		if(k == 0 && n == 0)
			return 0;
		vector<int> vt(n+1, 0);
		vt[0] = 1;
		for(int i = 1; i <= n; ++i) {
			for(int j = 0; j < min(k, i); ++j)
				vt[i] += vt[i-j-1];
		}
		cout << vt[n] << '\n';
	}
}
```
:::
:::spoiler william
```cpp=
#include <bits/stdc++.h>
using namespace std;
#define ll long long 
int main () {
	int k, n;
	
	while(cin >> k >> n) {
		if(k == 0 && n == 0) break;
		vector<int> vc(n + 1, 0);
		vc[0] = vc[1] = 1;
		for(int i = 1; i <= n; i++) {
			int ans = 0;
			for(int j = 1; j <= k && i - j >= 0; j++) {
				ans += vc[i - j];
			}
			vc[i] = ans;
		}
		cout << vc[n] << endl;
	}
}
```
:::

---
## 以上就是本次測驗的題解，祝大家都越來越電 :zap: