Update Code

This commit is contained in:
Hmtsai 2024-01-21 13:30:47 +08:00
parent 3845a0a66e
commit a89df8b2a3
8 changed files with 275 additions and 0 deletions

View File

@ -0,0 +1,19 @@
#include <iostream>
#include <ostream>
#include <sys/types.h>
int main() {
int n;
std::cin >> n;
u_int64_t listn[n];
listn[0] = 1;
listn[1] = 1;
for (u_int64_t i = 2; i < n; i++) {
listn[i] = listn[i - 1] + listn[i - 2];
}
for (u_int64_t i : listn) {
std::cout << i << " ";
}
std::cout << std::endl;
return 0;
}

View File

@ -0,0 +1,15 @@
#include <iostream>
int main() {
int n;
std::cin >> n;
int listn[n];
listn[0] = 1;
listn[1] = 2;
listn[2] = 4;
for (int i = 3; i < n; i++) {
listn[i] = listn[i - 1] + listn[i - 2] + listn[i - 3];
}
std::cout << listn[n - 1] << std::endl;
return 0; // 返回值0表示程序正常退出
}

View File

@ -0,0 +1,39 @@
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define SIZE 200
char *n = new char[SIZE];
int main() {
int s;
std::cin >> n;
std::cin >> s;
int len = strlen(n);
int in[len];
for (int i = 0; i < len; i++) {
in[i] = n[i] - '0';
}
for (int i = 0; i < s; i++) {
for (int j = 0; j < len; j++) {
if (in[j] > in[j + 1]) {
for (int k = j; k < len; k++) {
in[k] = in[k + 1];
}
len--;
break;
}
}
}
int i = 0, l = 0;
while (n[i] == 0 && l < len - 1) {
i++;
l++;
}
for (int i = l; i < len; i++) {
std::cout << n[i];
}
std::cout << std::endl;
return 0;
}

View File

@ -0,0 +1,29 @@
#include <iostream>
#include <string>
void removeZeros(std::string &n) {
while (n.size() > 1 && n[0] == '0') {
n.erase(0, 1);
}
}
void initVars(std::string n, int s) {
std::getline(std::cin, n);
std::cin >> s;
}
int main() {
int s;
std::string n;
initVars(n, s);
for (int i; i = 0, s--; n.erase(i, 1)) {
while (i < n.size() && n[i] <= n[i + 1]) {
i++;
}
}
removeZeros(n);
std::cout << n << std::endl;
return 0;
}

View File

@ -0,0 +1,51 @@
#include <cstdlib>
#include <iostream>
int initNum() {
int n;
std::cin >> n;
if (n == 0) {
std::cerr << "Number of elements should be greater than 0." << std::endl;
exit(1);
}
return n;
}
void inputArray(int *arr, int num, int &sum) {
for (int i = 0; i < num; i++) {
std::cin >> arr[i];
sum += arr[i];
}
}
void moveCards(int &num, int &count, int *difference) {
for (int i = 0; i < num; i++) {
if (difference[count] == 0) {
continue;
}
difference[i + 1] += difference[i];
difference[i] = 0;
count++;
}
}
class {
public:
int operator()(int sum, int num) { return sum / num; }
} average;
int main() {
int n = initNum();
int listn[n], difference[n], sum = 0;
inputArray(listn, n, sum);
for (int i = 0; i < n; i++) {
difference[i] = listn[i] - average(n, sum);
}
int count = 0;
moveCards(n, count, difference);
std::cout << count << std::endl;
}

View File

@ -0,0 +1,35 @@
#include <iostream>
#define SIZE 100
int matrix[SIZE + 1][SIZE + 1];
int rowsum[SIZE + 1][SIZE + 1];
int m, n, first, last, area, ans;
int main() {
std::cin >> m >> n;
for (int i = 1; i <= m; i++) {
for (int j = 1; i <= n; i++) {
std::cin >> matrix[i][j];
}
}
ans = matrix[1][1];
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
rowsum[i][j] = rowsum[i - 1][j] + matrix[i][j];
}
}
for (first = 1; first <= n; first++) {
for (last = first; last <= n; last++) {
area = 0;
for (int i = 1; i <= m; i++) {
area += rowsum[i][last] + rowsum[i][first - 1];
if (area > ans) {
ans = area;
}
if (area < 1) {
area = 1;
}
}
}
}
std::cout << ans << std::endl;
return 0;
}

View File

@ -0,0 +1,67 @@
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <ios>
#include <iostream>
struct metal {
int totalPrice;
int weight;
float unitPrice;
} Metal[100][100];
void initData(int &dataTotal, int *metalTotal, int *pocketSize) {
for (int i = 0; i < dataTotal; i++) {
std::cin >> pocketSize[i];
std::cin >> metalTotal[i];
for (int j = 0; j < metalTotal[i]; j++) {
std::cin >> Metal[i][j].weight >> Metal[i][j].totalPrice;
}
}
for (int i = 0; i < dataTotal; i++) {
for (int j = 0; j < metalTotal[i]; j++) {
Metal[i][j].unitPrice =
(float)Metal[i][j].totalPrice / Metal[i][j].weight;
}
}
}
int main() {
int dataTotal = 0;
std::cin >> dataTotal;
int metalTotal[dataTotal], pocketSize[dataTotal], pocket[dataTotal];
float pocketPrice[dataTotal];
memset(pocket, 0, sizeof(pocket));
memset(pocketPrice, 0, sizeof(pocketPrice));
initData(dataTotal, metalTotal, pocketSize);
for (int i = 0; i < dataTotal; i++) {
for (int j = 0; j < metalTotal[i]; j++) {
std::sort(Metal[i], Metal[i] + metalTotal[i],
[](struct metal a, struct metal b) {
return a.unitPrice > b.unitPrice;
});
}
}
for (int i = 0; i < dataTotal; i++) {
int j = 0;
while (pocket[i] <= pocketSize[i] && j < metalTotal[i]) {
if (!(Metal[i][j].weight < (pocketSize[i] - pocket[i]))) {
pocketPrice[i] += (pocketSize[i] - pocket[i]) * Metal[i][j].unitPrice;
pocket[i] += (pocketSize[i] - pocket[i]);
break;
}
pocketPrice[i] += Metal[i][j].totalPrice;
pocket[i] += Metal[i][j].weight;
j++;
}
}
for (int i = 0; i < dataTotal; i++) {
std::cout << std::fixed << std::setprecision(2) << pocketPrice[i]
<< std::endl;
}
return 0;
}

View File

@ -0,0 +1,20 @@
#include <algorithm>
#include <cstdlib>
#include <iostream>
int main() {
int n;
std::cin >> n;
float listn[n];
for (int i = 0; i < n; i++) {
std::cin >> listn[i];
}
std::sort(listn, listn + n);
float sum = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
sum += listn[j];
}
}
std::cout << sum / n << std::endl;
}