# Daily coding until I give up
每天寫一題程式直到我放棄:))
### Day 1 - [zerojudge g595 修補圍籬](https://zerojudge.tw/ShowProblem?problemid=g595)
陣列的基礎題,寫的爆幹醜,一堆可以優化的地方。
```cpp
#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
int t = n;
int total = 0;
int a[150];
while(n--){
int num;
cin >> num;
a[t-n-1] = num;
}
for(int i=0;i<t;i++){
if(a[i]==0&&i==0){
total+=a[i+1];
}
else if(a[i]==0&&i==t-1){
total+=a[i-1];
}
else if(a[i]==0){
if(a[i-1]<a[i+1]) total+=a[i-1];
else total+=a[i+1];
}
}
cout << total;
}
```
其實用個for loop就可以了,完全不需要用while來讀array,單純我n行類型的題目寫慣了就直覺這樣寫。也可以直接include algorithm裡面的```min()```,不用if判斷。
### Day 2 - [zerojudge c382 加減乘除](https://zerojudge.tw/ShowProblem?problemid=c382)
```cpp
#include <iostream>
using namespace std;
int main(){
int n, p;
char c;
cin >> n >> c >> p;
switch(c){
case '+':
cout << n+p;
break;
case '-':
cout << n-p;
break;
case '*':
cout << n*p;
break;
case '/':
cout << n/p;
break;
}
}
```
沒什麼難的點,用switch case或if else都可以。
### Day 3 - [zerojudge b969 hello, everyone](https://zerojudge.tw/ShowProblem?problemid=b969)
```cpp
#include <iostream>
#include <sstream>
using namespace std;
int main(){
string n, a;
stringstream ss;
getline(cin, n); // 名字
getline(cin, a); // 問候語
ss << n;
while(ss >> n){ // 以空格分割字串
cout << a <<", "<< n <<endl;
}
}
```
這題用```getline```+```stringstream```,先用```getline```讀整行,再用```stringstream```分割字串(空格是預設分隔符)。
::: success
如果要用```,```分隔,則只要將while條件改為
```cpp
while(getline(ss, n, ',')){
```
:::
### Day 4 - [zerojudge a038 數字翻轉](https://zerojudge.tw/ShowProblem?problemid=a038)
```cpp
#include <iostream>
#include <vector>
#include <algorithm> // reverse的lib
using namespace std;
int main(){
char c;
bool y = false;
vector<char>v;
while(cin >> c){
v.push_back(c);
}
reverse(v.begin(), v.end());
for(int i=0;i<v.size();i++){
if(v[i]=='0'&&y==false) continue; //若=0且沒遇到過非0的數字,不輸出
cout << v[i];
y = true;
}
if(y == false) cout << '0';
}
```
用字元來輸入vector,並用algorithm函式庫裡面的```reverse()```來==反轉陣列==。
布林值y是判斷先前是否遇到過非0的數字,若沒遇到過則用```continue```跳過迴圈中接下來的輸出程式,若y到最後都是<font color="#ff70b4">false</font>,則代表整串都是0,直接輸出0。
### Day 5 - [zerojudge a034 二進位制轉換](https://zerojudge.tw/ShowProblem?problemid=a034)
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int n;
vector<int>v;
while(cin >> n){
if(n==0) v.push_back(0);
else{
while(n>0){
v.push_back(n%2);
n/=2;
}
}
reverse(v.begin(), v.end());
for(int i=0;i<v.size();i++){
cout << v[i];
}
cout <<endl;
v.clear();
}
}
```
這題就是十進制轉二進制,可以用==除2取餘==直到商數為0,再==倒序輸出==的解法。
重點是每筆測資要記得換行,第一次沒加換行吃了WA。
### Day 6 - [zerojudge b968 K-I-S-S-I-N-G](https://zerojudge.tw/ShowProblem?problemid=b968)
好累寫個水題
```cpp
#include <iostream>
using namespace std;
int main(){
string a, b;
getline(cin, a);
getline(cin, b);
cout << a <<" and "<< b <<" sitting in the tree";
}
```
被搞到了第一次用cin,這題有個測資是中間有空格的完整人名(分兩行),要用個getline讀整行。
### Day 7 - [zerojudge c290 APCS 2017-0304-1秘密差](https://zerojudge.tw/ShowProblem?problemid=c290)
這題有很多種解法可以解,像**存陣列**、**遍歷string然後加奇數項,減偶數項**都可以。
```cpp
#include <iostream>
#include <cmath>
using namespace std;
int main(){
char c;
int a = 0;
int b = 0;
bool y = false;
while(cin >> c){
if(y==false) {
a+=c-'0'; y=!y;
}
else {
b+=c-'0'; y=!y;
}
if(cin.peek()=='\n') break;
}
cout << abs(a-b);
}
```
我用的是一個一個字元慢慢讀的方法,所以```cin.peek()```要最後加,放一開始判斷的話會讀不到最後一個數字。
### Day 8 - [zerojudge c813 11332 - Summing Digits](https://zerojudge.tw/ShowProblem?problemid=c813)
```cpp
#include <iostream>
using namespace std;
int f(long long n){
if(n<10) return n;
int sum = 0;
while(n>0){ // 每個位數加總
sum+=n%10;
n/=10;
}
return f(sum);
}
int main(){
long long n;
while(cin >> n, n){
n = f(n);
cout << n <<endl;
}
}
```
這題主要是要寫出這段加總的code和遞迴的條件,可以像我一樣用**取餘整除換下一位數**,也可以用**遍歷string**加總。
### Day 9 - [zerojudge b971 等差數列](https://zerojudge.tw/ShowProblem?problemid=b971)
快樂寫水題,這題只要注意公差有負的測資就好。
```cpp
#include <iostream>
using namespace std;
int main(){
int f, e, t;
cin >> f >> e >> t;
while(1){
cout << f <<" ";
if(f==e) break;
f+=t;
}
}
```
### Day 10 - [zerojudge g496 彗星列車 (Comet)](https://zerojudge.tw/ShowProblem?problemid=g496)
```cpp
#include <iostream>
#include <cmath>
using namespace std;
int main(){
double a, v;
cin >> a >> v;
cout << (long long int)ceil(v/a);
}
```
這是不用if的解法。
用ceil向上取整後,因為==double會在數字很大或很小的時候輸出科學記號==,所以有兩種解法,一個是轉成==long long== 輸出,一種是加個==fixed+setprecision==。
### Day 11 - [zerojudge a244 新手訓練 ~ for + if](https://zerojudge.tw/ShowProblem?problemid=a244)
今天有點累讓我寫個水題。
雖然標題寫if但我用switch case哈哈。
這題測資有比較大的數字要用long或long long。
```cpp
#include <iostream>
using namespace std;
int main(){
long long int n;
cin >> n;
for(int i=0;i<n;i++){
long long int a, b, c;
cin >> a >> b >> c;
switch (a){
case 1:
cout << b+c<<endl; break;
case 2:
cout << b-c<<endl; break;
case 3:
cout << b*c<<endl; break;
case 4:
cout << b/c<<endl; break;
}
}
}
```
### Day 12 - [zerojudge a915 二維點排序](https://zerojudge.tw/ShowProblem?problemid=a915)
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int n;
cin >> n;
vector<pair<int, int>> v(n);
for(int i=0;i<n;i++){
int x, y;
cin >> x >> y;
v[i]={x, y};
}
sort(v.begin(), v.end());
for(int i=0;i<n;i++){
cout << v[i].first << " " << v[i].second <<endl;
}
}
```
這題用pair鍵值對很好解。
### Day 13 - [zerojudge a738 最大公約數](https://zerojudge.tw/ShowProblem?problemid=a738)
```cpp
#include <iostream>
using namespace std;
int main(){
int a, b, tmp;
while(cin >> a >> b){
while(a%b){
tmp = a%b;
a = b;
b = tmp;
}
cout << b <<endl;
}
}
```
簡單的gcd題目,用輾轉相除法即可。
這幾天著重在學指標跟linked-list上面。
### Day 14 - [zerojudge c294 APCS-2016-1029-1三角形辨別](https://zerojudge.tw/ShowProblem?problemid=c294)
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int n;
vector<int>v(3);
for(int i=0;i<3;i++){
cin >> n;
v[i]=n;
}
sort(v.begin(), v.end());
cout << v[0] << " " << v[1] << " " << v[2] << endl;
if(v[0]+v[1]<=v[2]) cout <<"No";
else{
if(v[0]*v[0]+v[1]*v[1]<v[2]*v[2]) cout << "Obtuse";
else if(v[0]*v[0]+v[1]*v[1]==v[2]*v[2]) cout << "Right";
else cout << "Acute";
}
}
```
### Day 15 - [Codeforces 4A Watermelon](https://codeforces.com/problemset/problem/4/A)
因為今天zerojudge系統關掉,第一次寫codeforces:))
```cpp
#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
if(n%2==0&&n>2) cout << "YES";
else cout << "NO";
}
```
### Day 16 - [zerojudge c508 去蟲](https://zerojudge.tw/ShowProblem?problemid=c508)
我可能要改一下都用vector的習慣了,多用一點array比較好。
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int n;
cin >> n;
vector<int>v(n);
for(int i=0;i<n;i++){
int m;
cin >> m;
v[i] = m;
}
sort(v.begin(), v.end());
for(int i=0;i<n;i++){
cout << v[i] << " ";
}
cout <<endl;
int i = 0;
while(i<v.size()-1){
if(v[i]==v[i+1]){
v.erase(v.begin()+i);
} else i++;
}
reverse(v.begin(), v.end());
for(int i=0;i<v.size();i++){
cout << v[i] << " ";
}
}
```
看了別人的解法才想到可以直接在輸出的時候判斷重複,我這樣多寫了一個迴圈...。
不過有第二種用```multiset```跟```set```的解法。
```cpp
#include <iostream>
#include <set>
using namespace std;
int main(){
int n;
cin >> n;
multiset<int>ms;
set<int>s;
for(int i=0;i<n;i++){
int m;
cin >> m;
ms.insert(m);
s.insert(m);
}
for(int i : ms){
cout << i << " ";
}
cout <<endl;
for(auto i = s.rbegin();i!=s.rend();++i){
cout << *i << " ";
}
}
```
輸出第一行的迴圈是語法糖,也就是說這段code等同於使用迭代器,如下的寫法:
```for(auto i = ms.begin(); i != ms.end(); ++i)```,只是可以用比較簡潔的方法寫。
第二行的迴圈因為輸出要反向遍歷,所以不能再用語法糖,然後迭代器要用```auto```。
### Day 17 - [zerojudge c186 蝸牛老師的點名單](https://zerojudge.tw/ShowProblem?problemid=c186)
30秒寫完今日水題:)
```cpp
#include <iostream>
using namespace std;
int main(){
string s;
while(cin >> s){
cout << s <<endl;
}
}
```
### Day 18 - [zerojudge a864 4. Stellar Classification](https://zerojudge.tw/ShowProblem?problemid=a864)
```cpp
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
string s;
double mb, mv;
while(cin >> s >> mb >> mv){
if(s=="END") break;
cout << s <<" "<< fixed << setprecision(2) << mb-mv <<" ";
if((mb-mv)>1.499) cout <<"M"<<endl;
else if((mb-mv)>0.999) cout <<"K"<<endl;
else if((mb-mv)>0.499) cout <<"G"<<endl;
else if((mb-mv)>0.249) cout <<"F"<<endl;
else if((mb-mv)>-0.001) cout <<"A"<<endl;
else if((mb-mv)>-0.251) cout <<"B"<<endl;
else cout <<"O"<<endl;
}
}
```
有點懶這個區間判斷的很寬鬆哈哈,複習了一下setprecision。
### Day 19 - [zerojudge e156 良心題: 求和](https://zerojudge.tw/ShowProblem?problemid=e156)
是真良心題,但我想怎麼不寫if跳出遞迴想很久。
```cpp
#include <iostream>
using namespace std;
int sum(int n){
int total = n;
(n!=0)&&(total+=sum(--n));
return total;
}
int main(){
int n;
cin >> n;
cout << sum(n);
}
```
一開始用```n--```還會TLE,因為先把n丟進遞迴的下一個函式才減,所以n一直都是同一個值,才會無限循環。
也是學到了可以直接用&&判斷左右兩式,來決定要不要執行那一行,甚至不用bool一個新變數。
### Day 20 - [zerojudge a693 吞食天地](https://zerojudge.tw/ShowProblem?problemid=a693)
這題因為有很大的測資,所以不能用迴圈硬暴。
要用前綴和(prefix)的概念。
```cpp
#include <iostream>
using namespace std;
int main(){
int n, m;
while(cin >> n >> m){
int food[n]={};
for(int i=1;i<=n;i++){
cin >> food[i];
food[i]+=food[i-1]; //前綴和
}
for(int i=0;i<m;i++){
int a, b;
cin >> a >> b;
cout << food[b]-food[a-1] <<endl;
}
}
}
```
下面這張圖是這題用到的簡易前綴和原理解釋。

### Day 21 - [zerojudge a799 正值國](https://zerojudge.tw/ShowProblem?problemid=a799)
一直在想有什麼陷阱,結果還真的沒有,難度堪比a001。
我第一直覺想到的還是用string讀然後判斷```s[0]```是不是```"-"``` xd。
```cpp
#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
cout << abs(n);
}
```
### Day 22 - [zerojudge b294 經濟大恐荒](https://zerojudge.tw/ShowProblem?problemid=b294)
立志把全部水題刷過一輪。
```cpp
#include <iostream>
using namespace std;
int main(){
int n, total = 0, p=1;
cin >> n;
while(n--){
int a;
cin >> a;
total += p*a;
p++;
}
cout << total;
}
```
### Day 23 - [zerojudge b757 頸美椰子樹](https://zerojudge.tw/ShowProblem?problemid=b757)
```cpp
#include <iostream>
using namespace std;
int main(){
double r;
cin >> r;
cout << (r*9)/5+32;
}
```
### Day 24 - [zerojudge b558 求數列第 n 項](https://zerojudge.tw/ShowProblem?problemid=b558)
用到高一數學遞迴關係式的公式解。
```cpp
#include <iostream>
using namespace std;
int main(){
int n;
while(cin >> n){
cout << 1+n*(n-1)/2 <<endl;
}
}
```
### Day 25 - [zerojudge b572 忘了東西的傑克](https://zerojudge.tw/ShowProblem?problemid=b572)
忘了=也算趕得上。
```cpp
#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
while(n--){
int h1, m1, h2, m2, m3;
cin >> h1 >> m1 >> h2 >> m2 >> m3;
if((h2*60+m2)-(h1*60+m1) >= m3) cout << "Yes" <<endl;
else cout << "No" <<endl;
}
}
```
### Day 26 - [zerojudge b884 電腦教室的傑克](https://zerojudge.tw/ShowProblem?problemid=b884)
用sqrt會輸出double,再轉int的話會有誤差,所以直接簡化式子。
```cpp
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int n;
cin >> n;
while(n--){
int x, y;
cin >> x >> y;
int yee = 100-x-y;
if(0<yee&&yee<=30) cout << "sad!" <<endl;
else if(30<yee&&yee<=60) cout << "hmm~~" <<endl;
else if(60<yee&&yee<100) cout << "Happyyummy" <<endl;
else cout << "evil!!" <<endl;
}
}
```
### Day 27 - [zerojudge c315 I, ROBOT 前傳](https://zerojudge.tw/ShowProblem?problemid=c315)
```cpp
#include <iostream>
using namespace std;
int main(){
int n, x = 0, y = 0;
cin >> n;
while(n--){
int a, b;
cin >> a >> b;
switch (a){
case 0:
y+=b;
break;
case 1:
x+=b;
break;
case 2:
y-=b;
break;
case 3:
x-=b;
break;
}
}
cout << x << " " << y;
}
```
### Day 28 - [zerojudge c350 “綠白黃” 四校聯課](https://zerojudge.tw/ShowProblem?problemid=c350)
寫這題的時候邏輯真的很不清楚,有雛型但是很難補完整。
```cpp
#include <iostream>
using namespace std;
int main(){
int n, k, w, add, total = 0;
cin >> n >> k >> w;
add = n;
if(n<k) cout << n;
else{
total+=n;
while(add>=k){
total+=(add/k)*w;
add = (add/k)*w+(add%k);
}
cout << total;
}
}
```
順便諮詢了[claude](https://claude.ai/chat/901b1f75-31ae-4b13-81a9-e15f3e948a96),有時候讓他引導式幫忙debug很不錯。
---
### 1/18 gave up了。
依舊會刷題但不會是每天,我覺得深入較難的題目比一直刷水題好多了。
後面會著重在資結跟演算法。
我怎麼還在看linked-list,要看多久。
---