人工智能大数据,工作效率生产力
Ctrl + D 收藏本站,更多好用AI工具
当前位置:首页 » AI资讯

C C++最新人工智能写的十段代码,九个通过测试了_ai人工智能编程代码,2024年最新【原理+实战+视频+源码】

2024-06-24 78

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

 int num = s[i] - '0'; char c = 'A' + num - 1; ans += c; } sort(ans.begin(), ans.end(), greater<char>()); cout << ans << endl; return 0; 

}

 测试错误,输入123242526 输出 FEDCBBBBA。你能找出错在哪里吗? 写一段正确的参考代码,如下: 

#include <bits/stdc++.h>
using namespace std;

int main()
{
string in;
string re = “”;
cin >> in;
int len = in.length();

in = in + 'Z'; for (int i = 0; i < len;) { int temp = int(in[i] - '0') * 10 + int(in[i + 1] - '0'); if (temp > 26) { re = re + char(in[i] - '0' + 'A' - 1); i++; } else { re = re+char(temp + 'A' - 1); i += 2; } } cout << re; return 0; 

}

 #### 7. 请给出七大排序算法的python代码。 

#1. 冒泡排序:
def bubble_sort(nums):
for i in range(len(nums)-1):
for j in range(len(nums)-i-1):
if nums[j] > nums[j+1]:
nums[j], nums[j+1] = nums[j+1], nums[j]
return nums

#2. 选择排序:
def selection_sort(nums):
for i in range(len(nums)-1):
min_index = i
for j in range(i+1, len(nums)):
if nums[j] < nums[min_index]:
min_index = j
nums[i], nums[min_index] = nums[min_index], nums[i]
return nums

#3. 插入排序:
def insertion_sort(nums):
for i in range(1, len(nums)):
j = i
while j > 0 and nums[j] < nums[j-1]:
nums[j], nums[j-1] = nums[j-1], nums[j]
j -= 1
return nums

#4. 希尔排序:
def shell_sort(nums):
gap = len(nums) // 2
while gap > 0:
for i in range(gap, len(nums)):
j = i
while j >= gap and nums[j] < nums[j-gap]:
nums[j], nums[j-gap] = nums[j-gap], nums[j]
j -= gap
gap //= 2
return nums

#5. 归并排序:
def merge_sort(nums):
if len(nums) <= 1:
return nums
mid = len(nums) // 2
left = merge_sort(nums[:mid])
right = merge_sort(nums[mid:])
return merge(left, right)
def merge(left, right):
res = []
while left and right:
if left[0] <= right[0]:
res.append(left.pop(0))
else:
res.append(right.pop(0))
res += left
res += right
return res

#6. 快速排序:
def quick_sort(nums):
if len(nums) <= 1:
return nums
pivot = nums[0]
left = [x for x in nums[1:] if x < pivot]
right = [x for x in nums[1:] if x >= pivot]
return quick_sort(left) + [pivot] + quick_sort(right)

#7. 堆排序:
def heap_sort(nums):
n = len(nums)
for i in range(n//2-1, -1, -1):
heapify(nums, n, i)
for i in range(n-1, 0, -1):
nums[i], nums[0] = nums[0], nums[i]
heapify(nums, i, 0)
return nums
def heapify(nums, n, i):
largest = i
l = 2i + 1
r = 2
i + 2
if l < n and nums[i] < nums[l]:
largest = l
if r < n and nums[largest] < nums[r]:
largest = r
if largest != i:
nums[i], nums[largest] = nums[largest], nums[i]
heapify(nums, n, largest)

 #### 8. 请给出七大排序算法的C代码。 

1.冒泡排序:
void bubble_sort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)

 for (j = 0; j < n-i-1; j++) if (arr[j] > arr[j+1]) swap(&arr[j], &arr[j+1]); 

}

//2.选择排序:
void selection_sort(int arr[], int n)
{
int i, j, min_idx;

for (i = 0; i < n-1; i++) { min_idx = i; for (j = i+1; j < n; j++) if (arr[j] < arr[min_idx]) min_idx = j; swap(&arr[min_idx], &arr[i]); } 

}

//3.插入排序:
void insertion_sort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i-1;

 while (j >= 0 && arr[j] > key) { arr[j+1] = arr[j]; j = j-1; } arr[j+1] = key; 

}
}

//4.希尔排序:
void shell_sort(int arr[], int n)
{
for (int gap = n/2; gap > 0; gap /= 2)
{
for (int i = gap; i < n; i += 1)
{
int temp = arr[i];
int j;
for (j = i; j >= gap && arr[j – gap] > temp; j -= gap)
arr[j] = arr[j – gap];
arr[j] = temp;
}
}
}

//5.归并排序:
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m – l + 1;
int n2 = r – m;

int L[n1], R[n2]; for (i = 0; i < n1; i++) L[i] = arr[l + i]; for (j = 0; j < n2; j++) R[j] = arr[m + 1+ j]; i = 0; j = 0; k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } 

}
void merge_sort(int arr[], int l, int r)
{
if (l < r)
{
int m = l+(r-l)/2;
merge_sort(arr, l, m);
merge_sort(arr, m+1, r);

 merge(arr, l, m, r); } 

}

//6.快速排序:
int partition (int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low – 1);

for (int j = low; j <= high- 1; j++) { if (arr[j] <= pivot) { i++; swap(&arr[i], &arr[j]); } } swap(&arr[i + 1], &arr[high]); return (i + 1); 

}
void quick_sort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
quick_sort(arr, low, pi – 1);
quick_sort(arr, pi + 1, high);
}
}

//7.堆排序:
void heapify(int arr[], int n, int i)
{
int largest = i;
int l = 2i + 1;
int r = 2
i + 2;

if (l < n && arr[l] > arr[largest]) largest = l; if (r < n && arr[r] > arr[largest]) largest = r; if (largest != i) { swap(arr[i], arr[largest]); heapify(arr, n, largest); } 

}
void heap_sort(int arr[], int n)
{
for (int i = n / 2 – 1; i >= 0; i–)
heapify(arr, n, i);

for (int i=n-1; i>=0; i--) { swap(arr[0], arr[i]); heapify(arr, i, 0); } 

}

 #### 9. 请给出七大排序算法的Java代码。 

//1. 冒泡排序:
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n – 1; i++) {
for (int j = 0; j < n – i – 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

//2. 选择排序:
public static void selectionSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n – 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}

//3. 插入排序:
public static void insertionSort(int[] arr) {
int n = arr.length;
for (int i = 1; i < n; i++) {
int value = arr[i];
int j = i – 1;
while (j >= 0 && arr[j] > value) {
arr[j + 1] = arr[j];
j–;
}
arr[j + 1] = value;
}
}

//4. 希尔排序:
public static void shellSort(int[] arr) {
int n = arr.length;
for (int gap = n / 2; gap > 0; gap /= 2) {
for (int i = gap; i < n; i++) {
int value = arr[i];
int j = i – gap;
while (j >= 0 && arr[j] > value) {
arr[j + gap] = arr[j];
j -= gap;
}
arr[j + gap] = value;
}
}
}

//5. 归并排序:
public static void mergeSort(int[] arr) {
int n = arr.length;
if (n < 2) {
return;
}
int mid = n / 2;
int[] left = new int[mid];
int[] right = new int[n – mid];
for (int i = 0; i < mid; i++) {
left[i] = arr[i];
}
for (int i = mid; i < n; i++) {
right[i – mid] = arr[i];
}
mergeSort(left);
mergeSort(right);
merge(arr, left, right);
}
public static void merge(int[] arr, int[] left, int[] right) {
int i = 0, j = 0, k = 0;
int leftLen = left.length;
int rightLen = right.length;
while (i < leftLen && j < rightLen) {
if (left[i] <= right[j]) {
arr[k++] = left[i++];
} else {
arr[k++] = right[j++];
}
}
while (i < leftLen) {
arr[k++] = left[i++];
}
while (j < rightLen) {
arr[k++] = right[j++];
}
}

//6. 快速排序:
public static void quickSort(int[] arr, int low, int high) {
if (low < high) {
int pivot = partition(arr, low, high);
quickSort(arr, low, pivot – 1);
quickSort(arr, pivot + 1, high);
}
}
public static int partition(int[] arr, int low, int high) {
int pivot = arr[high];
int i = low – 1;
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
}

//7. 堆排序:
public static void heapSort(int[] arr) {
int n = arr.length;
for (int i = n / 2 – 1; i >= 0; i–) {
heapify(arr, n, i);
}
for (int i = n – 1; i >= 0; i–) {
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
public static void heapify(int[] arr, int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest]) {
largest = left;
}
if (right < n && arr[right] > arr[largest]) {
largest = right;
}
if (largest != i) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}

 #### 10. 请给出十大排序算法的Golang代码。 

//1. 冒泡排序:
func BubbleSort(arr []int) {
for i := 0; i < len(arr); i++ {
for j := 0; j < len(arr)-i-1; j++ {
if arr[j] > arr[j+1] {
arr[j], arr[j+1] = arr[j+1], arr[j]
}
}
}
}

//2. 选择排序:
func SelectionSort(arr []int) {
for i := 0; i < len(arr); i++ {
minIndex := i
for j := i + 1; j < len(arr); j++ {
if arr[j] < arr[minIndex] {
minIndex = j
}
}
arr[i], arr[minIndex] = arr[minIndex], arr[i]
}
}

//3. 插入排序:
func InsertionSort(arr []int) {
for i := 1; i < len(arr); i++ {
for j := i; j > 0; j– {
if arr[j] < arr[j-1] {
arr[j], arr[j-1] = arr[j-1], arr[j]
}
}
}
}

//4. 希尔排序:
func ShellSort(arr []int) {
n := len(arr)
h := 1
for h < n/3 {
h = 3*h + 1
}
for h >= 1 {
for i := h; i < n; i++ {
for j := i; j >= h && arr[j] < arr[j-h]; j -= h {
arr[j], arr[j-h] = arr[j-h], arr[j]
}
}
h /= 3
}
}

//5. 归并排序:
func MergeSort(arr []int) {
if len(arr) <= 1 {
return
}
mid := len(arr) / 2
left := arr[:mid]
right := arr[mid:]
MergeSort(left)
MergeSort(right)
i := 0
j := 0
k := 0
for i < len(left) && j < len(right) {
if left[i] < right[j] {
arr[k] = left[i]
i++
} else {
arr[k] = right[j]
j++
}
k++
}
for i < len(left) {
arr[k] = left[i]
i++
k++
}
for j < len(right) {
arr[k] = right[j]
j++
k++
}
}

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

n(left) && j < len(right) {
if left[i] < right[j] {
arr[k] = left[i]
i++
} else {
arr[k] = right[j]
j++
}
k++
}
for i < len(left) {
arr[k] = left[i]
i++
k++
}
for j < len(right) {
arr[k] = right[j]
j++
k++
}
}

[外链图片转存中…(img-T9xKoh6i-1715727423016)]
[外链图片转存中…(img-yu820krI-1715727423017)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

原文链接:https://blog.csdn.net/2401_84973321/article/details/138887111?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522171851498816800186586291%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=171851498816800186586291&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-3-138887111-null-null.nonecase&utm_term=AI%E9%9D%A2%E8%AF%95

相关推荐

阅读榜

hellenandjeckett@outlook.com

加入QQ群:849112589

回顶部