Introduction
程式撰寫時,若寫得很亂或是很醜,極有可能導致無法找到錯誤的情況發生。在多人協作以及詢問他人問題時,好的程式碼風格可以使他人更快了解你在寫什麼。進而快速解決問題。
Detail
基本排版
1 | //首先,各層級之間應該要有 44 個空格。 |
1 | //再者,有些東西可以按照個人習慣,例如大括號的位置。 |
行內空格
1 | //行內空格排版,有兩種做法。 |
1 | //如果有需求將多個簡單指令壓在一行的話,建議使用空格。 |
程式撰寫時,若寫得很亂或是很醜,極有可能導致無法找到錯誤的情況發生。在多人協作以及詢問他人問題時,好的程式碼風格可以使他人更快了解你在寫什麼。進而快速解決問題。
1 | //首先,各層級之間應該要有 44 個空格。 |
1 | //再者,有些東西可以按照個人習慣,例如大括號的位置。 |
1 | //行內空格排版,有兩種做法。 |
1 | //如果有需求將多個簡單指令壓在一行的話,建議使用空格。 |
先來一個
1 | #include<bits/stdc++.h> |
還有一個 for 圖論題目的
1 | #include<bits/stdc++.h> |
我的作法可能沒有那麼好,我是用 vector 存數字,接著進行運算,最後再轉成 link-list。
1 | /** |
1 | class Solution { |
最近我同學有在架設我們學校的線上評測系統,而一開始我們的系統非常不穩定,因此,他想到可以寫離線的程式答題系統緩解我們上課的需求。而我看到這個系統,我覺得這是一個不錯的想法,並依此加以改良,新增 TLE 判定和超時時會強制停止,並且可以跨平台使用。
一題會是一個資料夾,裡面會有隱藏檔案和非隱藏檔案。
檔案名稱 | 大致內容 | 是否隱藏 |
---|---|---|
AC | ascii art 的 AC 字樣 | True |
WA | ascii art 的 WA 字樣 | True |
TLE | ascii art 的 TLE 字樣 | True |
main.h | 主要程式 | 原則上 True |
Run.cpp | 執行程式 | False |
Solve.h | 使用者的程式 | False |
Description | 題目敘述 | False |
README.txt | 說明使用方法(如下) | False |
最近想說來寫一下許多人推薦的 LeetCode。
我是用 pair 額外儲存陣列中元素的 index ,並用排序加上二分搜的方法。因為這題的 n 最多是 10000,我不確定 O(n^2) 的演算法會不會過,所以就決定用 O(nlog(n)),…一個比較保險的概念。
1 | class Solution { |
最近寫了一個 GRAPH_GEN.h 檔,想說可以方便生成測資。
前三個會回傳 vector<pair<int,int>>
每個皆有保證沒有自環,後三個則會回傳 vector<edge>
,以節省重複打 first
與 second
的時間
1 | #include<bits/stdc++.h> |
1 | #include<bits/stdc++.h> |
你有一個大整數 \(012345678910111213141516171819202122…\)
也就是說,這個大整數是把所有非負整數依序串在一起。
現在,給你 \(n\)、\(k\),請你輸出,在這個大整數中,由左至右第 \(n\) 次出現數字 \(k\),是出現在第幾個位置?
位置從 1 開始算,並保證 \(k\) 是介於 \(0\) 到 \(9\) 之間的整數。
注意到,因為這個大整數太特別了,你定義這個大整數的第一位是 \(0\) 。
\(n \le 10^{12}, 0 \le k \le 9\)
數字這麼大,一定是二分搜尋,找出最後一個含有數字 \(k\) 小於 \(n\) 的數字,而求出在 \(num\) 之前有多少數字 \(k\) 的方法如下。
第一天先買,維護一個 bool 變數,分為有持股與無持股兩種情況。
若交易結束仍持有股票,則不考慮該股票買進的成本 -> 賣掉的時候再加上獲利
1 | #include<bits/stdc++.h> |
就是下面這一行
1 | #pragma GCC optimize ("O3,unroll-loops") |
可以做到什麼效果呢?在某些情況下可以讓 \(O(n^2)\) 硬解 \(10^5\) 的測資。
1 | int a[100010]; |
原本要用前綴和,變爆搜就會過。
給定\(n, m\)下一行會有\(n\)個數字,輸出所有\(k in 1~m\),\(k\)是與所有數字皆互質的數。
1 | 3 12 |
1 | 3 |
用埃氏篩法在\(log(n)\) 的時間內做質因數分解,接著將所有數字的質因數聯集存在 bitset 中,最後判斷\(1~m\)的所有數字的質因數之中有無存於 bitset 中。
1 | #include<bits/stdc++.h> |