2020年9月19日 星期六

e621: 1. 免費停車 (Free Parking)

題目來源:https://toi-reg.csie.ntnu.edu.tw/question/201910/B1-FreeParking.pdf

線上檢驗:https://zerojudge.tw/ShowProblem?problemid=e621


求範圍區間內,不能被指定數字整除的數字,

要注意的是開頭跟結尾兩個數字都不能算進來。


程式碼如下:

有問題歡迎留言或傳訊討論。

n = int(input())
while n > 0:
  n -= 1
  ans = ""
  s,e,d = tuple([int(i) for i in input().split()])
  for i in range(s+1 , e):
    if i % d != 0:
      ans += str(i) + " "
  if ans != "":
    print(ans)
  else: 

print("No free parking spaces.") 

e623: 2. PPAP

題目來源:https://toi-reg.csie.ntnu.edu.tw/question/201910/B2-PPAP.pdf

檢測系統:https://zerojudge.tw/ShowProblem?problemid=e623


四種物品循環,每新的一輪重複次數+1,所以分別需要4、8、12、.....。

這題測資不大,可以直接用遞迴或迴圈,先算出輸入的測資需要循環幾次,

再找出該次循環內的物品。


程式碼如下:

有問題歡迎留言或來信討論。

n = int(input())
count = 0
while n > count * 4:
  n -= count * 4
  count += 1

index = int(n / count)
if n % count != 0:
  index += 1

a = ["Pen" , "Pineapple" , "Apple" , "Pineapple pen"]
print(a[index-1])

2020年9月12日 星期六

f259: 皓宇的青蛙


 題目來源:高中生程式解題系統


https://zerojudge.tw/ShowProblem?problemid=f259


這題測資量很大,c++的話可以用set來處理,

PYTHON我用set或dict都是超時,

所以這題我選用c++來解題。


額外要注意的是資料量真的很大,

所以記得加上cin.tie(0),cin.sync_with_stdio(0)


程式碼如下,有問題歡迎留言、來信、私訊討論


#include <iostream>
#include <set>

using namespace std;

int main()
{
    cin.tie(0) , cin.sync_with_stdio(0);
    string s;
    set<string> sets;
    while(cin >> s) {
        if(sets.count(s) == 1) {
            cout << 1 <<"\n";
        }
        else {
            sets.insert(s);
            cout << 0 << "\n";
        }
    }

    return 0;
}

2020年9月1日 星期二

c315: I, ROBOT 前傳

題目來源:https://zerojudge.tw/ShowProblem?problemid=c315

這題只要對於條件判斷式的運用有一定的熟悉度,相信不會太困難,而我這題選擇用switch case的方式來解決,概念上差不多。
算是初期很好的練習題目。 


以下附上我的程式,有任何問題歡迎留言或私訊我。 

#include <iostream>
/*4
0 10
1 4
2 3
3 6*/

using namespace std;

int main()
{
    int x = 0 , y = 0;
    int n , m , o;
    cin >> n;
    while(n--) {
        cin >> m >> o;
        switch(m) {
            case 0:
                y += o;
                break;
            case 1:
                x += o;
                break;
            case 2:
                y -= o;
                break;
            case 3:
                x -= o;
                break;
        }
        
    }
    cout << x << " " << y;

    return 0;
}

2020年8月16日 星期日

a740: 質因數之和

 a740: 质因数之和

題目來源 https://zerojudge.tw/ShowProblem?problemid=a740


顧名思義,求質因數的和。求因數當然不難,但這題的測資最大接近20億,如果是跑回圈一個一個檢測,難保不會TLE(超時),給幾個優化的思維。

1.將收到的數開根號,找2到測資開根號之間有沒有因數。

2.質數只可能出現在6A+1或6A-1的地方。


以下附上程式碼:

如果有不懂得地方歡迎來信或留言。

#include <iostream>
#include <math.h>

using namespace std;


int main()
{
    cin.tie(0) , cin.sync_with_stdio(0);
    int n , sum;
    while(cin >> n) {
        if(n <= 1) {
            cout << 0;
            continue;
        }
        sum = 0;
        while(n % 2 == 0) {
            sum += 2;
            n /= 2;
        }
        while(n % 3 == 0) {
            sum += 3;
            n /= 3;
        }
        int a = sqrt(n) + 1;
        for(int i = 6; i < a; i++) {
            
            while(n % (i - 1) == 0) {
                sum += (i - 1);
                n /= (i - 1);
            }
            while(n % (i + 1) == 0) {
                sum += (i + 1);
                n /= (i + 1);
            }
        }
        if(n != 1) sum += n;
        cout << sum << "\n";
    }

    return 0;
}

a417: 螺旋矩陣

 題目來源 https://zerojudge.tw/ShowProblem?problemid=a417


這題關鍵思維就是定義出順時鐘跟逆時鐘兩種走向的向量改變,順時鐘是{0 , 1} , {1 , 0} , {0 , -1} , {-1 , 0} ,逆時鐘是{1 , 0} , {0 , 1} , {-1 , 0} , {0 , -1},再來便是開始填數字,要注意的是每填完一圈,下一圈要填的格子會少一個。

如果有什麼不懂得地方再麻煩私訊或是寄信給我囉。


以下附上程式碼:(這題寫得有點亂)

#include <iostream>
#include <vector>
#include <string>
#include <iomanip>

using namespace std;
int n;
void check(int a[4][2] , vector<vector<int>> v) {
    int count = 1;
    int x = 0 , y = 0;
    v[0][0] = count;
    int index = 0 , start = 1;
    while(true) {
        for(int i = start; i < n; i++) {
            //cout <<x;
            x += a[index][0];
            y += a[index][1];
            if(v[x][y] == 0) {
                v[x][y] = ++count;
            } else {
                x = x - a[index][0];
                y = y - a[index][1];
                break;
            }
        }
        if(count == n*n) break;
        
        if(++index == 4) {//每跑完一圈,則每圈要改變的位置少1(start++)
            index = 0;
            start++;
        }
    }
    
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            cout << setw(5) << to_string(v[i][j]);
        }
        cout << "\n";
    }
}

int main()
{
    //cin.tie(0) , cin.sync_with_stdio(0);
    int t;
    int  m;
    vector<vector<int>> v;
    vector<int> s;
    int a[4][2] = {{0 , 1} , {1 , 0} , {0 , -1} , {-1 , 0}}; //找出順時針跟逆時針兩種方向
    int b[4][2] = {{1 , 0} , {0 , 1} , {-1 , 0} , {0 , -1}};
    cin >> t;
    while(t--) {
        cin >> n >> m;
        v.clear();
        s.clear();
        s.assign(n , 0);
        v.assign(n , s);
        if(m == 1) {
            check(a , v);
        } else {
            check(b , v);
        }
        
            
    }

    return 0;
}

d115: 數字包牌

 題目來源 https://zerojudge.tw/ShowProblem?problemid=d115

DFS(深度優先演算法)練習題

這題算是很標準很單純的DFS練習題了,硬要說的話就是前面多了一個由小到大的排序,非常適合剛開始學習DFS的設計師。


DFS網路上的說明非常多了,這邊就不贅述,如果爬文不是很了解的話,歡迎寄信或私訊我詢問囉。


以下附上程式碼:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
int m;
void dfs(vector<int> v , int index , vector<int> ans) {
    ans.push_back(v[index]);
    v.erase(v.begin() + index);
    if(ans.size() == m) {
        for(int i = 0; i < m; i++) {
            cout << ans[i] << " ";
        }
        cout << "\n";
    }
    for(int i = index; i < v.size(); i++) {
        dfs(v , i , ans);
    }
}

int main()
{
    vector<int> v;
    int n;
    cin.tie(0) , cin.sync_with_stdio(false);
    int t;
    string s;
    vector<int> temp;
    while(cin >> n) {
        v.clear();
        for(int i = 0; i < n; i++) {
            cin >> t;
            v.push_back(t);
        }
        sort(v.begin() , v.end());
        cin >> m;
        s = "";
        for(int i = 0; i < n; i++) {
            dfs(v , i , temp);
        }
    }
    

    return 0;
}

o079. 4. 最佳選擇

 題目描述: 給一個長度為 n 的正整數序列 a1,a2...an ,你可以執行多次操作 (包含 0 次),每次操作只能選擇這個序列的第一個或最後一個數字,再將這個數字從序列中刪除並自己搜集起來。 求滿足總和不超過 k 且搜集的數字奇數和偶數個數相同的條件下,所能搜集的數字總和最...