-
Notifications
You must be signed in to change notification settings - Fork 814
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎉 feat: initial commit for pat advanced level 1160 to 1163 using C++
- Loading branch information
Showing
4 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <cmath> | ||
#include <algorithm> | ||
using namespace std; | ||
struct node { | ||
int n, num; | ||
friend bool operator < (node &a, node &b) { | ||
if (a.n != b.n) return a.n < b.n; | ||
return a.num < b.num; | ||
} | ||
}T; | ||
int N, K, m, temp, sum, sum2, I, II; | ||
vector<node> A; | ||
int is_prime(int x) { | ||
if (x <= 2) return 0; | ||
for (int i = 2; i <= sqrt(x); i++) { | ||
if (x % i == 0) return 0; | ||
} | ||
return 1; | ||
} | ||
int main() { | ||
cin >> N; | ||
for (int i = 1 ; i <= N; i++) { | ||
A.clear(); | ||
cout << "Case " << i << '\n'; | ||
cin >> K >> m; | ||
if (K * 9 < m) cout << "No Solution\n"; | ||
else { | ||
temp = pow(10, K - 2); | ||
for (int i = temp / 10; i < temp; i++) { | ||
sum = 18, sum2 = 0, I = i, II = i + 1; | ||
while (I) { | ||
sum += I % 10; | ||
I /= 10; | ||
if (sum > m) break; | ||
} | ||
while (II) { | ||
sum2 += II % 10; | ||
II /= 10; | ||
} | ||
if (sum == m && is_prime(__gcd(m, sum2))) { | ||
T.n = sum2, T.num = i; | ||
A.push_back(T); | ||
} | ||
} | ||
sort(A.begin(), A.end()); | ||
if (A.empty()) cout << "No Solution\n"; | ||
for (auto &it : A) { | ||
cout << it.n << ' ' << it.num << "99\n"; | ||
} | ||
} | ||
|
||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include <iostream> | ||
#include <vector> | ||
using namespace std; | ||
struct node { | ||
int data, next; | ||
}A[100000]; | ||
vector<int> L1, L2, ans; | ||
int sa, sb, n, a, ta, tb, c; | ||
int main() { | ||
cin >> sa >> sb >> n; | ||
for (int i = 0; i < n; i++) { | ||
cin >> a >> A[a].data >> A[a].next; | ||
} | ||
ta = sa; | ||
while (ta != -1) { | ||
L1.push_back(ta); | ||
ta = A[ta].next; | ||
} | ||
tb = sb; | ||
while (tb != -1) { | ||
L2.push_back(tb); | ||
tb = A[tb].next; | ||
} | ||
if (L1.size() < L2.size()) swap(L1, L2); | ||
for (int i = 0, c = L2.size() - 1; i < L1.size(); i++) { | ||
ans.push_back(L1[i]); | ||
if (i & 1 && c >= 0) ans.push_back(L2[c--]); | ||
} | ||
for (int i = 1; i < ans.size(); i++) { | ||
printf("%05d %d %05d\n", ans[i-1], A[ans[i-1]].data, ans[i]); | ||
} | ||
printf("%05d %d -1", ans.back(), A[ans.back()].data); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include <iostream> | ||
using namespace std; | ||
int n, root = 1, lc[32], rc[32], mark[32]; | ||
string Data[32]; | ||
void deal(int x) { | ||
cout << '('; | ||
if (lc[x] * rc[x] > 1) { | ||
deal(lc[x]); | ||
deal(rc[x]); | ||
} | ||
cout << Data[x]; | ||
if (lc[x] * rc[x] < 0) deal(rc[x]); | ||
cout << ')'; | ||
} | ||
int main() { | ||
cin >> n; | ||
for (int i = 1; i <= n; i++) { | ||
cin >> Data[i] >> lc[i] >> rc[i]; | ||
mark[lc[i]] = mark[rc[i]] = 1; | ||
} | ||
while(mark[root]) root++; | ||
deal(root); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <iostream> | ||
#include <vector> | ||
using namespace std; | ||
int V, E, K, Start, End, a, b, d; | ||
int Edge[1002][1002], Distance[1002], flag, book[1002]; | ||
void deal(int x, int index, vector<int> Path) { | ||
for(int k = 1; k <= V; k++) { | ||
int Min = 9999999, now = Path[index]; | ||
for(int i = 1; i <= V; i++) { | ||
if(Distance[i] != 9999999 && !book[i] && Distance[i] <= Min) { | ||
if(Distance[i] < Min) { | ||
if(i == now) flag = 1; | ||
else flag = 0; | ||
} else if(Distance[i] == Min) { | ||
if(i == now) flag = 1; | ||
} | ||
Min = Distance[i]; | ||
} | ||
} | ||
if(!flag) return; | ||
++index; | ||
if(index > V) return; | ||
book[now] = 1; | ||
for(int i = 1; i <= V; i++) | ||
if(!book[i] && Distance[i] > Distance[now] + Edge[now][i] && Edge[now][i] != 0) | ||
Distance[i] = Distance[now] + Edge[now][i]; | ||
} | ||
} | ||
int main() { | ||
cin >> V >> E; | ||
for(int i = 0; i < E; i++) { | ||
cin >> a >> b >> d; | ||
Edge[a][b] = Edge[b][a] = d; | ||
} | ||
cin >> K; | ||
for(int i = 0; i < K; i++) { | ||
vector<int> Path(V); | ||
flag = 1; | ||
fill(book, book + 1002, 0); | ||
fill(Distance, Distance + 1002, 9999999); | ||
for(int j = 0; j < V; j++) cin >> Path[j]; | ||
Start = Path[0], End = Path[V - 1]; | ||
Distance[Start] = 0; | ||
deal(Start, 0, Path); | ||
if(flag) cout << "Yes\n"; | ||
else cout << "No\n"; | ||
} | ||
return 0; | ||
} |