baekjoon 20260104
This commit is contained in:
parent
b6564903d1
commit
fc86127050
38
code_study/Baekjoon/c/10757.c
Normal file
38
code_study/Baekjoon/c/10757.c
Normal file
@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void reverse(char arr[]) {
|
||||
int len = strlen(arr);
|
||||
for (int i = 0; i < len / 2; i++) {
|
||||
char temp = arr[i];
|
||||
arr[i] = arr[len - i - 1];
|
||||
arr[len - i - 1] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
char A[10002] = { 0 }, B[10002] = { 0 }, res[10003] = { 0 };
|
||||
int carry = 0;
|
||||
|
||||
scanf("%s %s", A, B);
|
||||
reverse(A);
|
||||
reverse(B);
|
||||
|
||||
int len = strlen(A) > strlen(B) ? strlen(A) : strlen(B);
|
||||
|
||||
for (int i = 0; i < len; i++) {
|
||||
int sum = A[i] - '0' + B[i] - '0' + carry;
|
||||
|
||||
while (sum < 0) sum += '0';
|
||||
if (sum > 9) carry = 1;
|
||||
else carry = 0;
|
||||
res[i] = sum % 10 + '0';
|
||||
}
|
||||
|
||||
if (carry == 1) res[len] = '1';
|
||||
reverse(res);
|
||||
printf("%s", res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
1
code_study/Baekjoon/python/10757.py
Normal file
1
code_study/Baekjoon/python/10757.py
Normal file
@ -0,0 +1 @@
|
||||
print(sum(map(int, input().split())))
|
||||
49
code_study/Baekjoon/ts/1197_1.ts
Normal file
49
code_study/Baekjoon/ts/1197_1.ts
Normal file
@ -0,0 +1,49 @@
|
||||
export {};
|
||||
const input = require("fs").readFileSync(0).toString().trim().split("\n");
|
||||
const [V, E]: number[] = input[0].split(" ").map(Number);
|
||||
|
||||
let edges: number[][] = [];
|
||||
for(let edge of input.slice(1)) edges.push(edge.split(" ").map(Number));
|
||||
|
||||
let parent: number[] = Array.from({length: V+1}, (_, i) => i);
|
||||
let rank: number[] = new Array(V+1).fill(0);
|
||||
|
||||
const find = (x: number): number => {
|
||||
if(x !== parent[x]) return parent[x] = find(parent[x]);
|
||||
return parent[x];
|
||||
}
|
||||
|
||||
const union = (x: number, y: number) => {
|
||||
x = find(x);
|
||||
y = find(y);
|
||||
|
||||
if(x === y) return;
|
||||
|
||||
if(rank[x] < rank[y]) [x, y] = [y, x];
|
||||
|
||||
parent[y] = x;
|
||||
if(rank[x] === rank[y]) rank[x]++;
|
||||
}
|
||||
|
||||
const kruskal = (): number => {
|
||||
edges.sort((a,b) => a[2] - b[2]);
|
||||
|
||||
let total_weight = 0;
|
||||
let edge_count = 0;
|
||||
|
||||
for(let edge of edges) {
|
||||
const [u, v, w]: number[] = edge;
|
||||
|
||||
if(find(u) !== find(v)) {
|
||||
union(u, v);
|
||||
total_weight += w;
|
||||
edge_count += 1;
|
||||
|
||||
if(edge_count === V-1) break;
|
||||
}
|
||||
}
|
||||
|
||||
return total_weight;
|
||||
}
|
||||
|
||||
console.log(kruskal());
|
||||
101
code_study/Baekjoon/ts/1197_2.ts
Normal file
101
code_study/Baekjoon/ts/1197_2.ts
Normal file
@ -0,0 +1,101 @@
|
||||
export {};
|
||||
|
||||
class PriorityQueue {
|
||||
private heap: [number, number][] = [];
|
||||
|
||||
push(item: [number, number]): void {
|
||||
this.heap.push(item);
|
||||
this._bubbleUp();
|
||||
}
|
||||
|
||||
pop(): [number, number] | undefined {
|
||||
if (this.heap.length === 0) return undefined;
|
||||
if (this.heap.length === 1) return this.heap.pop();
|
||||
const root = this.heap[0];
|
||||
this.heap[0] = this.heap.pop()!;
|
||||
this._bubbleDown();
|
||||
return root;
|
||||
}
|
||||
|
||||
size(): number {
|
||||
return this.heap.length;
|
||||
}
|
||||
|
||||
isEmpty(): boolean {
|
||||
return this.heap.length === 0;
|
||||
}
|
||||
|
||||
private _bubbleUp(): void {
|
||||
let index = this.heap.length - 1;
|
||||
while (index > 0) {
|
||||
const parentIdx = Math.floor((index - 1) / 2);
|
||||
if (this.heap[index][0] >= this.heap[parentIdx][0]) break;
|
||||
[this.heap[index], this.heap[parentIdx]] = [this.heap[parentIdx], this.heap[index]];
|
||||
index = parentIdx;
|
||||
}
|
||||
}
|
||||
|
||||
private _bubbleDown(): void {
|
||||
let index = 0;
|
||||
const length = this.heap.length;
|
||||
|
||||
while (true) {
|
||||
let smallestIdx = index;
|
||||
const leftIdx = 2 * index + 1;
|
||||
const rightIdx = 2 * index + 2;
|
||||
|
||||
if (leftIdx < length && this.heap[leftIdx][0] < this.heap[smallestIdx][0]) {
|
||||
smallestIdx = leftIdx;
|
||||
}
|
||||
|
||||
if (rightIdx < length && this.heap[rightIdx][0] < this.heap[smallestIdx][0]) {
|
||||
smallestIdx = rightIdx;
|
||||
}
|
||||
|
||||
if (smallestIdx === index) break;
|
||||
|
||||
[this.heap[index], this.heap[smallestIdx]] = [this.heap[smallestIdx], this.heap[index]];
|
||||
index = smallestIdx;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const input = require("fs").readFileSync(0).toString().trim().split("\n");
|
||||
const [V, E]: number[] = input[0].split(" ").map(Number);
|
||||
|
||||
let graph: number[][][] = Array.from({length: V+1}, () => []);
|
||||
for(let i=1; i<=E; i++) {
|
||||
let [u,v,w]: number[] = input[i].split(" ").map(Number);
|
||||
graph[u].push([w,v]);
|
||||
graph[v].push([w,u]);
|
||||
}
|
||||
|
||||
const prim = (): number => {
|
||||
let visited: boolean[] = new Array(V+1).fill(false);
|
||||
let pq = new PriorityQueue();
|
||||
pq.push([0,1]);
|
||||
|
||||
let total_weight: number = 0;
|
||||
let count: number = 0;
|
||||
|
||||
while(pq.size() !== 0) {
|
||||
let [w, v]: number[] = pq.pop();
|
||||
|
||||
if(visited[v]) continue;
|
||||
|
||||
visited[v] = true;
|
||||
total_weight += w;
|
||||
count++;
|
||||
|
||||
if(count === V) break;
|
||||
|
||||
for(let next of graph[v]) {
|
||||
let [nw, nv] = next;
|
||||
if(!visited[nv]) pq.push([nw, nv]);
|
||||
}
|
||||
}
|
||||
|
||||
return total_weight;
|
||||
}
|
||||
|
||||
console.log(prim());
|
||||
Loading…
x
Reference in New Issue
Block a user