# <center>[WriteUP] CPE研習 </center>
## 告知
- 本題解為MCUCSIE所有
- 本題解不一定為最佳解 可能寫的也很醜 如有意見麻煩協助註解
- 不要給我照抄
# 系統測試
### 簡單翻譯
給予兩個人數
要求算出兩者之差
### 想法
隨便讀兩個數字相加取abs就出來了
然後小心int上限 他的range是不大於2^32次 int絕對存不下
:::spoiler Code(Py)
```python=
import sys
for s in sys.stdin:
a, b= map(int, s.split())
print(abs(a-b))
```
:::
# Week 3
## [PA - Odd Num](https://vjudge.net/contest/459911#problem/A)
### 簡單翻譯
給予一個開區間(a,b)
將該區間之間所有奇數相加
### 想法
先確定a跟b本身是不是奇數
是的話維持 否則內縮一單位
然後公式解 梯形數那個
:::spoiler Code(Cpp)
```cpp=
#include <bits/stdc++.h>
#define endl "\n"
#define IO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define MAXN maxn
#define MOD modn
using namespace std;
signed main(){
IO;
int n, a, b;
cin>>n;
for (int i=0;i<n;i++){
cin>>a>>b;
a%2?a:a++;
b%2?b:b--;
int res = (a+b)*((b-a)/2+1)/2;
cout<<"Case "<<i+1<<": "<<res<<endl;
}
return 0;
}
```
:::
:::spoiler Code(Py)
```python=
for i in range(int(input())):
a = int(input()); b = int(input())
a=a if a&1 else a+1
b=b if b&1 else b-1
res = "Case " + str(i+1) + ": " + str(int((a+b)*((b-a)/2+1)/2))
print(res)
```
:::
## [PB - The 3n + 1 problem](https://vjudge.net/contest/459911#problem/A)
### 簡單翻譯
給你一個流程 看會經過幾步驟
### 想法
無他 暴力破解
:::spoiler Code(Cpp)
```cpp=
#include <bits/stdc++.h>
#define endl "\n"
#define IO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
//#define MAXN maxn
#define MOD modn
using namespace std;
int len(int n){
int l = 1;
while (n!=1){
if (n%2) n=3*n+1;
else n/=2;
l++;
}
return l;
}
int m(int a, int b){
int maxCt=0;
for (int i=a;i<=b;i++){
int l = len(i);
if (maxCt<l) maxCt=l;
}
return maxCt;
}
signed main(){
int i, j, tmp, maxn;
while (cin>>i>>j){
cout<<i<<' '<<j<<' ';
if (i>j) swap(i, j);
cout<<m(i, j)<<endl;
}
return 0;
}
```
:::
:::spoiler Code(Py)
```python=
import sys
def leng(n):
l = 1
while n!=1:
if n%2:
n=3*n+1
else:
n/=2
l+=1
return l
def maxn(a, b):
n = 0
for i in range(a,b+1):
l =leng(i)
if n < l:
n = l
return n
for s in sys.stdin:
arr = [int(x) for x in s.split()]
if arr[0]>arr[1]:
arr[0], arr[1] = arr[1], arr[0]
print(arr[0], arr[1], maxn(arr[0], arr[1]))
```
:::
## [PC - The Decoder](https://vjudge.net/contest/460987#problem/A)
### 簡單翻譯
給你一串加密過的字串
要你解密
### 想法
超級無腦的caesar
Offset固定是7
用ascii加減一下就有了
:::spoiler Code(Cpp)
```cpp=
#include <bits/stdc++.h>
#define endl "\n"
#define IO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define MAXN maxn
#define MOD modn
using namespace std;
signed main(){
IO;
string s;
while (cin>>s){
for (int i=0;i<s.length();i++){
s[i]=char(int(s[i])-7);
}
cout<<s<<endl;
}
return 0;
}
```
:::
:::spoiler Code(Py)
```python=
import sys
for s in sys.stdin:
for char in s:
print(chr(ord(char)-7),end='')
print()
```
:::
# Week 4
## [PA - Beat the Spread!](https://vjudge.net/contest/460987#problem/A)
### 簡單翻譯 真的很簡單
輸入給予兩個數s和d
分別為x和y的和及差的絕對值
求x和y 大的在前
### 想法
代數解
$x+y=sum$
$x-y=dif$
則 $2x = sum+dif$
剩下就國中數學了 小心非整數解 筆者被陰了一次
:::spoiler Code(Cpp)
```cpp=
#include <bits/stdc++.h>
#define endl "\n"
#define IO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define MAXN maxn
#define MOD modn
using namespace std;
signed main(){
IO;
int n, s, d, x, y;
cin>>n;
while (n--){
cin>>s>>d;
x = (s+d)/2; y = x - d;
if ((s+d)%2) cout<<"impossible"<<endl;
else if (x < 0 || y < 0) cout<<"impossible"<<endl;
else cout<<x<<' '<<y<<endl;
}
return 0;
}
```
:::
:::spoiler Code(Py)
```python=
for i in range(int(input())):
s, d = map(int, input().split())
x = int((s + d) / 2); y = int(x - d)
if ((s + d) % 2) or (x < 0 or y <0):
print("impossible")
else:
print(x, y)
```
:::
## [PB - Parity](https://vjudge.net/contest/460987#problem/B)
### 簡單翻譯
給你一個數 不大於2^31
叫你轉成bin 就二進位
然後算有幾個1
### 想法
因為數字並不大於2^31 不會很大
因此打算直接暴力轉成bin再遍歷一次
(也可以一邊轉一邊紀錄 反正一邊除就會一邊知道有多少1了
不過筆者是分開 對 不要問我 我懶得分開存)
:::spoiler Code(Cpp)
```cpp=
#include <bits/stdc++.h>
#define endl "\n"
#define IO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define MAXN maxn
#define MOD modn
using namespace std;
vector<int> int2bin(int i){
vector<int> bin;
int ct = 0;
while (i > 0){
bin.push_back(i % 2);
i /= 2;
}
reverse(bin.begin(),bin.end());
return bin;
}
int int2par(vector<int> *v){
int c = 0;
for (auto &e:*v){
if (e == 1) c++;
}
return c;
}
signed main(){
int i;
while (cin>>i && i!=0){
auto B = int2bin(i); int P = int2par(&B);
cout<<"The parity of ";
for (int i:B) cout<<i;
cout<<" is "<<P<<" (mod 2)."<<endl;
}
return 0;
}
```
:::
:::spoiler Code(Py)
```python=
while True:
n = int(input())
if not n:
break
s = "";ct = 0
while n>0:
if n&1:
s = '1' + s;ct+=1
else:
s = '0' + s
n/=2
print("The parity of", s, "is", ct, "(mod 2).")
```
:::
## [PC - Division of Nlogonia](https://vjudge.net/contest/460987#problem/C)
### 簡單翻譯
給你一個自定義原點
再給你多個點 判斷該點是在原點的什麼方位
要注意不管是水平或是垂直與原點在同一直線上都是divisa
### 想法
國中代數座標
x大就是往右 y大就是往上
以此類推
:::spoiler Code(Cpp)
```cpp=
#include <bits/stdc++.h>
#define endl "\n"
#define IO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define MAXN maxn
#define MOD modn
using namespace std;
signed main(){
int k, n, m, x, y;
while (cin>>k && k !=0){
cin>>n>>m;
while (k--){
cin>>x>>y;
if (x == n || y == m){
cout<<"divisa"<<endl;
continue;
}
char posy = y > m?'N':'S'; char posx = x > n?'E':'O';
cout<<posy<<posx<<endl;
}
}
return 0;
}
```
:::
:::spoiler Code(Py)
```python=
while True:
k = int(input())
if k == 0:
break
n, m = map(int, input().split())
for i in range(k):
x, y = map(int, input().split())
if x == n or y == m:
print('divisa')
continue
posx = 'E' if x > n else 'O'
posy = 'N' if y > m else 'S'
print(posy+posx)
```
:::
# Week 5
## [PA - Back to High School Physics](https://vjudge.net/contest/462140#problem/A)
### 簡單翻譯
題目敘述有點爛
大致上是
假設在t秒時速度為v,問此粒子2t秒經過的位移為多少?
### 想法
無腦公式解
res = 2vt
:::spoiler Code(Cpp)
```cpp=
#include <bits/stdc++.h>
#define endl "\n"
#define IO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define MAXN maxn
#define MOD modn
using namespace std;
signed main(){
IO;
int v, t;
while (cin>>v>>t){
cout<<2*v*t<<endl;
}
return 0;
}
```
:::
:::spoiler Code(Py)
```python=
try:
while True:
v, t = map(int, input().split())
print(2*v*t)
except EOFError:
pass
```
:::
## [PB - Doom's Day Algorithm](https://vjudge.net/contest/462140#problem/B)
### 簡單翻譯
給你2011年的任意日期
輸出該日為星期幾
### 想法
紀錄2011第一天是星期幾
然後根據天數mod7
:::spoiler Code(Cpp)
```cpp=
#include <bits/stdc++.h>
#define endl "\n"
#define IO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define MAXN maxn
#define MOD modn
using namespace std;
signed main(){
int n, m, d, days[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
string w[] = {"Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"};
cin>>n;
while (n--){
cin>>m>>d;
cout<<w[(days[m-1]+d)%7]<<endl;
}
return 0;
}
```
:::
:::spoiler Code(Py)
```python=
days = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
w = ["Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"]
try:
while True:
m, d = map(int, input().split())
print(w[(days[m-1]+d)%7])
except EOFError:
pass
```
:::
## [PC - Alarm Clock](https://vjudge.net/contest/462140#problem/C)
### 簡單翻譯
給定一組時間
判斷兩時間差
可能為跨日 最多跨一日
### 想法
跨日就加24hr
:::spoiler Code(Cpp)
```cpp=
#include <bits/stdc++.h>
#define endl "\n"
#define IO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define MAXN maxn
#define MOD modn
using namespace std;
signed main(){
int h1, h2, m1, m2, res;
while (cin>>h1>>m1>>h2>>m2){
if ((h1+h2+m1+m2) == 0 && (h1*h2*m1*m2) == 0) break;
if (h1>h2 || (h1==h2 && m1>m2)) h2+=24;
res = (h2*60+m2)-(h1*60+m1);
cout<<res<<endl;
}
return 0;
}
```
:::
:::spoiler Code(Py)
while True:
h1, m1, h2, m2 = map(int, input().split())
if not (h1+h2+m1+m2) and not (h1*h2*m1*m2):
break
if h1>h2 or (h1==h2 and m1>m2):
h2+=24
res = (h2*60+m2)-(h1*60+m1);
print(res)
:::
<!-- Just for fun -->
<style>
span.hidden-xs:after {
content: ' × CPE' !important;
}
</style>