Update Code

This commit is contained in:
Hmtsai 2024-01-23 19:03:41 +08:00
parent a89df8b2a3
commit 4ea01eb477
19 changed files with 419 additions and 0 deletions

View File

@ -0,0 +1,19 @@
#include <iostream>
int sum = 0;
void add(int n) {
if (n == 0) {
return;
}
sum += n;
add(n - 1);
}
int main() {
int n;
std::cin >> n;
add(n);
std::cout << sum << std::endl;
return 0;
}

View File

@ -0,0 +1,16 @@
#include <algorithm>
#include <iostream>
int commonFactor(int n, int m) {
if (std::max(n, m) % std::min(n, m) == 0) {
return std::min(n, m);
}
return commonFactor(std::min(n, m), std::max(n, m) % std::min(n, m));
}
int main() {
int n, m;
std::cin >> n >> m;
std::cout << commonFactor(n, m) << std::endl;
return 0;
}

View File

@ -0,0 +1,27 @@
#include <iostream>
int numberSize(int n) {
int numberSize;
while (n != 0) {
n /= 10;
numberSize++;
}
return numberSize;
}
int daoxu(int n, int count) {
if (n == 0) {
return 0;
}
std::cout << n % 10;
daoxu(n / 10, count - 1);
return 0;
}
int main() {
int n;
std::cin >> n;
daoxu(n, numberSize(n));
std::cout << std::endl;
return 0;
}

View File

@ -0,0 +1,17 @@
#include <iostream>
int fibMap[100] = {0, 1, 1};
int fib(int n) {
if (fibMap[n] == 0) {
return fib(n - 1) + fib(n - 2);
}
return fibMap[n];
}
int main() {
int n;
std::cin >> n;
std::cout << fib(n) << std::endl;
return 0;
}

View File

@ -0,0 +1,64 @@
#include <ios>
#include <iostream>
class stack {
public:
stack(int size) {
this->data = new int[size]; // 分配内存
this->topIndex = 0;
}
int pop() {
topIndex--;
return this->data[topIndex];
}
void push(int value) { this->data[this->topIndex++] = value; }
int top() { return this->data[this->topIndex - 1]; }
bool empty() { return this->topIndex == 0; }
private:
int *data;
int topIndex;
} number(100);
int change(int n, int m) {
if (n == 0) {
return 0;
}
number.push(n % m);
return change(n / m, m);
}
int main() {
int n, m;
std::cin >> n >> m;
change(n, m);
while (!number.empty()) {
if (number.top() > 9) {
switch (number.top()) {
case 10:
std::cout << 'A';
break;
case 11:
std::cout << 'B';
break;
case 12:
std::cout << 'C';
break;
case 13:
std::cout << 'D';
break;
case 14:
std::cout << 'E';
break;
case 15:
std::cout << 'F';
break;
}
} else {
std::cout << number.top();
}
number.pop();
}
std::cout << std::endl;
return 0;
}

View File

@ -0,0 +1,18 @@
#include <iostream>
void daoxu(int n, std::string s) {
if (n == 0) {
return;
}
std::cout << s[n - 1];
daoxu(n - 1, s);
return;
}
int main() {
std::string s;
std::cin >> s;
daoxu(s.size() - 1, s);
std::cout << std::endl;
return 0;
}

View File

@ -0,0 +1,26 @@
#include <iostream>
#include <ostream>
long long llist[10000] = {0, 1, 2, 4};
long long count = 0;
long long f(int x) {
if (llist[x] == 0) {
count++;
return f(x - 1) + f(x - 2) + f(x - 3);
}
count++;
return llist[x];
}
int main() {
long long x;
while (std::cin >> x) {
if (x == 0 || x == 114514) {
break;
}
std::cout << f(x) << std::endl;
std::cout << count << std::endl;
}
return 0;
}

View File

@ -0,0 +1,25 @@
#include <iostream>
long long llist[10000] = {0, 1, 2, 4};
long long count;
long long f(int x) {
if (llist[x] == 0) {
llist[x] = f(x - 1) + f(x - 2) + f(x - 3);
count++;
return llist[x];
}
return llist[x];
}
int main() {
long long x;
while (std::cin >> x) {
if (x == 0 || x == 114514) {
break;
}
std::cout << f(x) << std::endl;
std::cout << count << std::endl;
}
return 0;
}

View File

@ -0,0 +1,36 @@
#include <algorithm>
#include <cstring>
#include <iostream>
struct temp {
int value = 0;
int index = 0;
};
int main() {
int n;
std::cin >> n;
temp tmp[n];
int listn[n][n], jtmp = 0, answer = 0;
memset(tmp, 0, sizeof(tmp));
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
std::cin >> listn[i][j];
}
}
for (int i = n - 1; i >= 0; i++) {
for (int j = jtmp; j <= i; j++) {
tmp[j].value = std::max(listn[i + 1][j], listn[i + 1][j + 1]);
tmp[j].index = j;
std::sort(tmp, tmp + n, [](temp a, temp b) { return a.value > b.value; });
jtmp = tmp[0].index;
}
answer += tmp[0].value;
}
std::cout << answer << std::endl;
return 0;
}

Binary file not shown.

View File

@ -0,0 +1,40 @@
#include <iostream>
int lastNumber = 0;
void find(int l, int r, int x, int arr[]) {
if (l >= r) {
lastNumber = -1;
return;
}
int mid = (l + r) / 2;
if (arr[mid] == x) {
lastNumber = arr[mid - 1];
return;
} else if (arr[mid] < x && arr[mid + 1] < x) {
find(mid + 1, r, x, arr);
} else if (arr[mid] > x && arr[mid - 1] > x) {
find(l, mid - 1, x, arr);
} else if (arr[mid] < x && arr[mid + 1] > x) {
lastNumber = arr[mid];
} else if (arr[mid] <= x) {
lastNumber = arr[mid];
}
return;
}
int main() {
int n, m;
std::cin >> n >> m;
int arr[n];
for (auto &i : arr) {
std::cin >> i;
}
while (n > 0) {
int x;
std::cin >> x;
find(0, n, x, arr);
std::cout << lastNumber << std::endl;
n--;
}
}

View File

@ -0,0 +1,17 @@
#include <cmath>
#include <iostream>
int main() {
int n, m;
std::cin >> m;
int size = std::pow(2, m);
int arr[size][size];
for (int i = 0; i < m; i++) {
for (int j = 1 + (i * 2); j <= 2 + (i * 2); j++) {
arr[j][k] = j + (i * 2);
}
for (int k = 1 + (i * 2); k <= 2 + (i * 2); k++) {
}
}
}

BIN
src/algorithm/findNumber Executable file

Binary file not shown.

View File

@ -0,0 +1,31 @@
#include <cstdlib>
#include <iostream>
bool findNumber(int list[], int low, int high, int x) {
bool flag;
if (low < high) {
int mid = low + (high - low) / 2;
if (list[mid] == x) {
std::cout << "YES" << std::endl;
exit(0);
} else if (list[mid] < x) {
flag = findNumber(list, low, mid - 1, x);
} else {
flag = findNumber(list, mid + 1, high, x);
}
return flag;
}
std::cout << "NO" << std::endl;
exit(1);
}
int main() {
int n, x;
std::cin >> n >> x;
int listn[n];
for (int i = 0; i < n; i++) {
std::cin >> listn[i];
}
findNumber(listn, 0, n - 1, x);
return 0;
}

BIN
src/algorithm/hanoi Executable file

Binary file not shown.

21
src/algorithm/hanoi.cpp Normal file
View File

@ -0,0 +1,21 @@
#include <cstdio>
#include <iostream>
int count = 0;
void mov(int n, char from, char to, char aux) {
if (n == 0) {
return;
}
mov(n - 1, from, aux, to);
count++;
printf("%d:from%c-->%c\n", count, from, to);
mov(n - 1, aux, to, from);
}
int main() {
int n;
std::cin >> n;
mov(n, 'A', 'C', 'B');
return 0;
}

37
src/luckyNumber.cpp Normal file
View File

@ -0,0 +1,37 @@
#include <iostream>
#include <string>
int main() {
int n;
std::cin >> n;
std::string slistn[n];
for (int i = 0; i < n; i++) {
std::getline(std::cin, slistn[i]);
for (int j = 0; j < slistn[i].size(); j++) {
slistn[i][j] = slistn[i][j] - '0';
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
slistn[i][j] *= 7;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j += 2) {
while (slistn[i][j] > 9) {
slistn[i][j] = (slistn[i][j] / 10) + (slistn[i][j] % 10);
}
}
}
int sum[n];
for (int i = 0; i < n; i++) {
for (int j : slistn[i]) {
sum[i] += j;
}
if (sum[i] % 8 == 0) {
std::cout << "T" << std::endl;
}
std::cout << "F" << std::endl;
}
return 0;
}

BIN
src/type/stringTo Executable file

Binary file not shown.

25
src/type/stringTo.cpp Normal file
View File

@ -0,0 +1,25 @@
#include <iostream>
char listn[] = {'a', 'e', 'i', 'o', 'u'};
int main() {
int n;
std::cin >> n;
while (n >= 0) {
std::string s;
std::getline(std::cin, s);
for (auto &i : s) {
i = std::tolower(i);
}
for (auto &i : s) {
for (auto j : listn) {
if (i == j) {
i -= 32;
}
}
}
std::cout << s << std::endl;
n--;
}
return 0;
}