Update Code

This commit is contained in:
Hmtsai 2024-01-17 14:24:42 +08:00
parent 1fe0e63142
commit 3845a0a66e
11 changed files with 1751 additions and 63 deletions

View File

@ -0,0 +1,26 @@
#include <iostream>
using namespace std;
int main() {
int n, num, maxi = 0;
cin >> n;
bool *arr = new bool[10001];
for (int i = 0; i < n; i++) {
int tmp;
cin >> tmp;
arr[tmp] = true;
maxi = max(maxi, tmp);
}
for (int i = 0; i < maxi; i++) {
if (arr[i]) {
num++;
}
}
cout << num << endl;
for (int i = 0; i <= maxi; i++) {
if (arr[i]) {
cout << i << " ";
}
}
cout << endl;
return 0;
}

View File

@ -1,5 +1,7 @@
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;

View File

@ -0,0 +1,21 @@
#include <cstdio>
#include <cstring>
#include <iostream>
int count[27], maxIndex, max;
int main() {
char *str = new char[1000];
memset(count, 0, sizeof(count));
scanf("%s", str);
for (int i = 0; i < strlen(str); i++) {
count[str[i] - 97]++;
}
for (int i = 0; i < 27; i++) {
if (max == count[i]) {
continue;
}
max = std::max(count[i], max);
maxIndex = i;
}
std::cout << (char)(maxIndex + 71) << " " << max << std::endl;
return 0;
}

View File

@ -0,0 +1,52 @@
#include <iostream>
#include <ostream>
int a[101], r[101];
void mergeSort(int s, int t) {
if (s == t) {
return;
}
int mid = (s + t) / 2;
mergeSort(s, mid);
mergeSort(mid + 1, t);
int i = s, j = mid + 1, k = s;
while (i <= mid && j <= t) {
if (a[i] <= a[j]) {
r[k] = a[i];
k++;
i++;
} else {
r[k] = a[j];
k++;
j++;
}
}
while (i <= mid) {
r[k] = a[i];
k++;
i++;
}
while (j <= t) {
r[k] = a[j];
k++;
j++;
}
for (int i = s; i <= t; i++) {
a[i] = r[i];
}
}
int main() {
int n;
std::cin >> n;
for (int i = 1; i <= n; i++) {
std::cin >> a[i];
}
mergeSort(1, n);
for (int i = 1; i <= n; i++) {
std::cout << a[i] << " ";
}
std::cout << std::endl;
return 0;
}

View File

@ -0,0 +1,31 @@
#include <iostream>
#include <vector>
int findMissing(std::vector<int> &nums) {
int left = 0, right = nums.size() - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] == mid + nums[0]) {
left = mid + 1;
} else {
right = mid;
}
}
return left + nums[0];
}
int main() {
int n;
std::cin >> n;
std::vector<int> listn(n);
for (int i = 0; i < n; i++) {
std::cin >> listn[i];
}
int missingNumber = findMissing(listn);
if (missingNumber == listn[n - 1]) {
std::cout << "Sequence is consecutive" << std::endl;
return 0;
}
std::cout << "Missing Number is" << missingNumber << std::endl;
return 0;
}

View File

@ -0,0 +1,41 @@
#include <iostream>
int partition(int *arr, int low, int high) {
int i = low, j = high, pivot = arr[low];
while (i < j) {
while (i < j && arr[j] > pivot) {
j--;
}
while (i < j && arr[i] <= pivot) {
i++;
}
if (i < j) {
std::swap(arr[i++], arr[j--]);
}
}
if (arr[i] > pivot) {
std::swap(arr[i - 1], arr[low]);
return i - 1;
}
std::swap(arr[i], arr[low]);
return i;
}
void quickSort(int *arr, int low, int high) {
int mid;
if (low < high) {
mid = partition(arr, low, high);
quickSort(arr, low, mid - 1);
quickSort(arr, mid + 1, high);
}
}
int main() {
int a[] = {57, 68, 59, 52, 72, 28, 98, 33, 24};
quickSort(a, 0, sizeof(a) / sizeof(a[0]) - 1);
for (int i : a) {
std::cout << i << " ";
}
std::cout << "\n";
return 0;
}

View File

@ -0,0 +1,45 @@
#include <iostream>
#include <iterator>
void quickSort(int *arr, int low, int high) {
if (high <= low) {
return;
};
int i = low, j = high;
int key = arr[low];
while (true) {
while (arr[i] <= key) {
i++;
if (i == high) {
break;
};
}
while (arr[j] >= key) {
j--;
if (j == low) {
break;
};
}
if (i >= j) {
// 终止相等的情况
break;
}
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
arr[low] = arr[j];
arr[j] = key;
quickSort(arr, low, j - 1);
quickSort(arr, j + 1, high);
}
int main() {
int a[] = {57, 68, 59, 52, 72, 28, 96, 33, 24};
quickSort(a, 0, sizeof(a) / sizeof(a[0]) - 1);
for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) {
std::cout << a[i] << " ";
}
std::cout << std::endl;
return 0;
}

View File

@ -0,0 +1,65 @@
#include <cstdlib>
#include <iostream>
#include <ostream>
#include <stdlib.h>
#define BUCKET_COUNT 10
typedef struct {
int count;
int *values;
} Bucket;
void bucketSort(int arr[], int n) {
Bucket buckets[BUCKET_COUNT];
for (int i = 0; i < BUCKET_COUNT; i++) {
buckets[i].count = 0;
buckets[i].values = nullptr;
}
for (int i = 0; i < n; i++) {
int bucketIndex = arr[i];
Bucket *bucket = &buckets[bucketIndex];
bucket->values =
(int *)realloc(bucket->values, (bucket->count + 1) * sizeof(int));
bucket->values[bucket->count] = arr[i];
bucket->count++;
}
for (int i = 0; i < BUCKET_COUNT; i++) {
Bucket *bucket = &buckets[i];
int bucketSize = bucket->count;
for (int j = 1; j < bucketSize; j++) {
int key = bucket->values[j];
int k = j - 1;
while (k >= 0 && bucket->values[k] > key) {
bucket->values[k + 1] = bucket->values[k];
k--;
}
bucket->values[k + 1] = key;
}
}
int index = 0;
for (int i = 0; i < BUCKET_COUNT; i++) {
Bucket *bucket = &buckets[i];
int bucketSize = bucket->count;
for (int j = 0; j < bucketSize; j++) {
arr[index] = bucket->values[j];
index++;
}
free(buckets->values);
}
}
int main() {
int arr[] = {29, 25, 3, 49, 9, 37, 21, 43, 5};
int n = sizeof(arr) / sizeof(arr[0]);
for (int i : arr) {
std::cout << i << " ";
}
std::cout << std::endl;
bucketSort(arr, n);
for (int i : arr) {
std::cout << i << " ";
}
std::cout << std::endl;
return 0;
}

View File

@ -0,0 +1,7 @@
#include <iostream>
template <typename T> class bucket {
public:
bucket() {}
1 private : T *list = new T[10][10];
};

View File

@ -1,77 +1,65 @@
#ifndef __INTVECTOR_H__
#define __INTVECTOR_H__
#include <iostream>
class vector
{
class vector {
public:
vector(int size)
{
this->size = size;
this->list = new int[size];
}
int append(int);
int begin() { return this->list[0]; }
int end() { return this->list[size - 1]; }
int read(int index) { return this->list[index]; }
int print(const char *);
int erase(int index);
int getSize(){ return this->size; };
vector(int size) {
this->size = size;
this->list = new int[size];
}
int append(int);
int begin() { return this->list[0]; }
int end() { return this->list[size - 1]; }
int read(int index) { return this->list[index]; }
int print(const char *);
int erase(int index);
int getSize() { return this->size; };
private:
int size;
int *list;
int size;
int *list;
};
int vector::append(int number)
{
int tmp[size++];
for (int i = 0; i < size - 1; i++)
{
tmp[i] = this->list[i];
}
this->list = new int[size];
for (int i = 0; i < size - 1; i++)
{
this->list[i] = tmp[i];
}
this->list[size - 1] = number;
return 0;
int vector::append(int number) {
int tmp[size++];
for (int i = 0; i < size - 1; i++) {
tmp[i] = this->list[i];
}
this->list = new int[size];
for (int i = 0; i < size - 1; i++) {
this->list[i] = tmp[i];
}
this->list[size - 1] = number;
return 0;
}
int vector::print(const char *end)
{
for (int i = 0; i < size; i++)
{
std::cout << this->list[i];
}
std::cout << end;
return 0;
int vector::print(const char *end) {
for (int i = 0; i < size; i++) {
std::cout << this->list[i];
}
std::cout << end;
return 0;
}
int vector::erase(int index)
{
if (index >= size || index < 0)
{
throw "Index is so big or small";
return -1;
}
this->list[index] = 0;
for (int i = index + 1; i < size; i++)
{
this->list[i - 1] = this->list[i];
}
int tmp[size--];
for (int i = 0; i < size; i++)
{
tmp[i] = this->list[i];
}
this->list = new int[size];
for (int i = 0; i < size; i++)
{
this->list[i] = tmp[i];
}
return 0;
int vector::erase(int index) {
if (index >= size || index < 0) {
throw "Index is so big or small";
return -1;
}
this->list[index] = 0;
for (int i = index + 1; i < size; i++) {
this->list[i - 1] = this->list[i];
}
int tmp[size--];
for (int i = 0; i < size; i++) {
tmp[i] = this->list[i];
}
this->list = new int[size];
for (int i = 0; i < size; i++) {
this->list[i] = tmp[i];
}
return 0;
}
#endif

1410
test Normal file

File diff suppressed because it is too large Load Diff