做网站和做小程序有什么不同,网络结构图怎么画,网站推广目标,wordpress打开只显示代码目录 MT1401归并排序 MT1402堆排序 MT1403后3位排序 MT1404小大大小排序 MT1405小大大小排序II MT1406数字重排 MT1407插入 MT1408插入 MT1409旋转数组 MT1410逆时针旋转数组 MT1401归并排序 c 语言实现代码
#include stdio.h// merge two subarrays
void merge(int a…目录 MT1401·归并排序 MT1402·堆排序 MT1403·后3位排序 MT1404·小大大小排序 MT1405·小大大小排序II MT1406·数字重排 MT1407·插入 MT1408·插入 MT1409·旋转数组 MT1410·逆时针旋转数组 MT1401·归并排序 c 语言实现代码
#include stdio.h// merge two subarrays
void merge(int arr[], int left, int mid, int right) {int i, j, k;int n1 mid - left 1; // left sonArrayint n2 right - mid; // right sonArray// create new arrayint L[n1], R[n2];for (i 0; i n1; i) {L[i] arr[left i];}for (j 0; j n2; j) {R[j] arr[mid 1 j];}i 0;j 0;k left;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 mergeSort(int arr[], int left, int right) {if (left right) {int mid left (right - left) / 2;mergeSort(arr, left, mid);mergeSort(arr, mid 1, right);merge(arr, left, mid, right);}
}int main() {int arr[10];for (int i 0; i 10; i) {scanf(%d, arr[i]);}mergeSort(arr, 0, 10 - 1);for (int i 0; i 10; i) {printf(%d , arr[i]);}return 0;
}
MT1402·堆排序 c 语言实现代码
#include stdio.h// swap two element vlaue
void swap(int *a, int *b) {int temp *a;*a *b;*b temp;
}void heapify(int arr[], int n, int i) {int largest i; // root 节点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) {swap(arr[i], arr[largest]);heapify(arr, n, largest);}
}void heapSort(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);}
}int main() {int arr[10];for (int i 0; i 10; i) {scanf(%d, arr[i]);}heapSort(arr, 10);for (int i 0; i 10; i) {printf(%d , arr[i]);}return 0;
}
MT1403·后3位排序 c 语言代码实现
#include stdio.h
#include stdlib.h
#define N 10int compare(const void *a, const void *b) {int num1 *(int *)a;int num2 *(int *)b;int last_1 num1 % 1000;int last_2 num2 % 1000;if (last_1 ! last_2) {return last_1 - last_2;} else {return num2 - num1;}
}int main() {int numbers[N];for (int i 0; i N; i) {scanf(%d, numbers[i]);}qsort(numbers, N, sizeof(10), compare);for (int i 0; i N; i) {printf(%d , numbers[i]);}return 0;
}
MT1404·小大大小排序 c 语言代码实现
#include stdio.h
#include stdlib.h#define SIZE 10// 升序排序的比较函数
int ascending(const void *a, const void *b) { return (*(int *)a - *(int *)b); }// 反转函数 用于将下标N到M的元素反转
void reverse(int arr[], int start, int end) {while (start end) {int temp arr[start];arr[start] arr[end];arr[end] temp;start;end--;}
}int main() {int arr[SIZE];int N, M;for (int i 0; i SIZE; i) {scanf(%d, arr[i]);}scanf(%d %d, N, M);// 对整个数组进行升序排序qsort(arr, SIZE, sizeof(int), ascending);// 反转下标 N 到 M 的部分reverse(arr, N, M);for (int i 0; i SIZE; i) {printf(%d , arr[i]);}return 0;
}
MT1405·小大大小排序II c 语言代码实现
#include stdio.h
#include stdlib.h#define SIZE 10// 升序排序的比较函数
int ascending(const void *a, const void *b) { return (*(int *)a - *(int *)b); }// 反转函数 用于将下标N到M的元素反转
void reverse(int arr[], int start, int end) {while (start end) {int temp arr[start];arr[start] arr[end];arr[end] temp;start;end--;}
}int main() {int arr[SIZE];int N;for (int i 0; i SIZE; i) {scanf(%d, arr[i]);}scanf(%d, N);// 对整个数组进行升序排序qsort(arr, SIZE, sizeof(int), ascending);// 反转下标 N 到 M 的部分reverse(arr, N, SIZE - 1);for (int i 0; i SIZE; i) {printf(%d , arr[i]);}return 0;
}
MT1406·数字重排 这道题 c 案例一直出问题 有解决的可以在评论区d我
c 语言实现
#include stdio.h
#include stdbool.h// 去重函数从数组a中去重结果存入数组b返回去重后的长度
int del(int a[], int b[], int n) {int c 0;for (int i 0; i n; i) {bool flag true;for (int j 0; j c; j) {if (a[i] b[j]) { // 如果a[i]已在b中设置标志为falseflag false;break;}}if (flag) { // 如果a[i]不在b中加入bb[c] a[i];}}return c;
}// 排序函数
void sort(int arr[], int len) {for (int i 0; i len - 1; i) {for (int j 0; j len - i - 1; j) {if (arr[j] arr[j 1]) {int temp arr[j];arr[j] arr[j 1];arr[j 1] temp;}}}
}int main() {int a, b, c, len 0;// 输入三个数字scanf(%d %d %d, a, b, c);int A[6], B[6];A[len] a * 100 b * 10 c;A[len] a * 100 c * 10 b;A[len] b * 100 a * 10 c;A[len] b * 100 c * 10 a;A[len] c * 100 a * 10 b;A[len] c * 100 b * 10 a;// 去重int newLen del(A, B, len);// 排序sort(B, newLen);// 输出结果for (int i 0; i newLen; i) {printf(%d\n, B[i]);}return 0;
}MT1407·插入 c 语言实现代码
#include stdio.h
#define SIZE 10void sort(int arr[], int len) {for (int i 0; i len - 1; i) {for (int j 0; j len - i - 1; j) {if (arr[j] arr[j 1]) {int temp arr[j];arr[j] arr[j 1];arr[j 1] temp;}}}
}int insertPoistion(int arr[], int len, int n) {int left 0;int right len - 1;while (left right) {int mid (left right) / 2;if (arr[mid] n) {left mid 1;} else {right mid - 1;}}return left;
}int main() {int arr[SIZE 1], n;for (int i 0; i SIZE; i) {scanf(%d, arr[i]);}scanf(%d, n);sort(arr, SIZE);int pos insertPoistion(arr, SIZE, n);for (int i SIZE; i pos; i--) {arr[i] arr[i - 1];}arr[pos] n;for (int i 0; i SIZE; i) {printf(%d , arr[i]);}return 0;
}
MT1408·插入 c 语言代码实现
#include stdio.hint insertPosition(int arr[], int len, int n) {int left 0;int right len - 1;while (left right) {int mid (left right) / 2;if (arr[mid] n) {left mid 1;} else {right mid - 1;}}return left;
}int main() {int N;int array[N 1];int insertNumber;scanf(%d, N);for (int i 0; i N; i) {scanf(%d, array[i]);}scanf(%d, insertNumber);int pos insertPosition(array, N, insertNumber);for (int i N; i pos; i--) {array[i] array[i - 1];}array[pos] insertNumber;for (int i 0; i N 1; i) {printf(%d , array[i]);}return 0;
}
MT1409·旋转数组 c 语言代码实现 #include stdio.hvoid rotate_array(int arr[], int length, int n) {// 处理 N 的值确保在有效范围内n n % length; // 处理 N 大于数组长度的情况if (n 0) {n length; // 将负数旋转转换为正数旋转}// 创建一个临时数组来存储旋转后的结果int temp[length];// 进行旋转for (int i 0; i length; i) {temp[(i n) % length] arr[i];}// 将结果复制回原数组for (int i 0; i length; i) {arr[i] temp[i];}
}int main() {int arr[10];int n;// 输入整型元素for (int i 0; i 10; i) {scanf(%d, arr[i]);}// 输入旋转次数 Nscanf(%d, n);// 旋转数组rotate_array(arr, 10, n);// 输出结果for (int i 0; i 10; i) {printf(%d, arr[i]);if (i 9) {printf( ); // 输出空格分隔}}printf(\n);return 0;
}MT1410·逆时针旋转数组 c 语言实现代码
#include stdio.hvoid rotateArrayCounterClockwise(int array[], int N, int M) {M M % N; // 计算有效的旋转次数int result[N];// 将前 M 个元素放在新数组的末尾for (int i 0; i N - M; i) {result[i] array[M i];}// 将前 M 个元素移动到新数组的开头for (int i 0; i M; i) {result[N - M i] array[i];}// 将新数组的内容复制到原数组for (int i 0; i N; i) {array[i] result[i];}
}int main() {int N, M;scanf(%d %d, N, M);int array[N];for (int i 0; i N; i) {scanf(%d, array[i]);}rotateArrayCounterClockwise(array, N, M);for (int i 0; i N; i) {printf(%d ,array[i]);}return 0;
} 文章转载自: http://www.morning.dwhnb.cn.gov.cn.dwhnb.cn http://www.morning.dktyc.cn.gov.cn.dktyc.cn http://www.morning.lfpzs.cn.gov.cn.lfpzs.cn http://www.morning.syglx.cn.gov.cn.syglx.cn http://www.morning.wblpn.cn.gov.cn.wblpn.cn http://www.morning.sqgqh.cn.gov.cn.sqgqh.cn http://www.morning.nlysd.cn.gov.cn.nlysd.cn http://www.morning.dwwlg.cn.gov.cn.dwwlg.cn http://www.morning.nrbqf.cn.gov.cn.nrbqf.cn http://www.morning.ujianji.com.gov.cn.ujianji.com http://www.morning.gwtgt.cn.gov.cn.gwtgt.cn http://www.morning.ghfmd.cn.gov.cn.ghfmd.cn http://www.morning.zsyrk.cn.gov.cn.zsyrk.cn http://www.morning.nkqxb.cn.gov.cn.nkqxb.cn http://www.morning.lhrcr.cn.gov.cn.lhrcr.cn http://www.morning.yrflh.cn.gov.cn.yrflh.cn http://www.morning.xuejitest.com.gov.cn.xuejitest.com http://www.morning.qqrqb.cn.gov.cn.qqrqb.cn http://www.morning.mljtx.cn.gov.cn.mljtx.cn http://www.morning.ntkpc.cn.gov.cn.ntkpc.cn http://www.morning.mhcys.cn.gov.cn.mhcys.cn http://www.morning.khtyz.cn.gov.cn.khtyz.cn http://www.morning.gqtw.cn.gov.cn.gqtw.cn http://www.morning.brfxt.cn.gov.cn.brfxt.cn http://www.morning.jcbjy.cn.gov.cn.jcbjy.cn http://www.morning.hmwjk.cn.gov.cn.hmwjk.cn http://www.morning.daidudu.com.gov.cn.daidudu.com http://www.morning.znqfc.cn.gov.cn.znqfc.cn http://www.morning.pqryw.cn.gov.cn.pqryw.cn http://www.morning.cldgh.cn.gov.cn.cldgh.cn http://www.morning.rntyn.cn.gov.cn.rntyn.cn http://www.morning.rzdzb.cn.gov.cn.rzdzb.cn http://www.morning.stcds.cn.gov.cn.stcds.cn http://www.morning.qnklx.cn.gov.cn.qnklx.cn http://www.morning.tmzlt.cn.gov.cn.tmzlt.cn http://www.morning.mntxalcb.com.gov.cn.mntxalcb.com http://www.morning.qbxdt.cn.gov.cn.qbxdt.cn http://www.morning.npfkw.cn.gov.cn.npfkw.cn http://www.morning.cplym.cn.gov.cn.cplym.cn http://www.morning.xhftj.cn.gov.cn.xhftj.cn http://www.morning.smzr.cn.gov.cn.smzr.cn http://www.morning.zdzgf.cn.gov.cn.zdzgf.cn http://www.morning.dnhdp.cn.gov.cn.dnhdp.cn http://www.morning.nlgmr.cn.gov.cn.nlgmr.cn http://www.morning.rqmr.cn.gov.cn.rqmr.cn http://www.morning.wjdgx.cn.gov.cn.wjdgx.cn http://www.morning.nlrp.cn.gov.cn.nlrp.cn http://www.morning.rtbj.cn.gov.cn.rtbj.cn http://www.morning.llllcc.com.gov.cn.llllcc.com http://www.morning.rxpp.cn.gov.cn.rxpp.cn http://www.morning.llqky.cn.gov.cn.llqky.cn http://www.morning.zrmxp.cn.gov.cn.zrmxp.cn http://www.morning.smtrp.cn.gov.cn.smtrp.cn http://www.morning.mzwfw.cn.gov.cn.mzwfw.cn http://www.morning.yltyz.cn.gov.cn.yltyz.cn http://www.morning.wfzdh.cn.gov.cn.wfzdh.cn http://www.morning.frqtc.cn.gov.cn.frqtc.cn http://www.morning.fgppj.cn.gov.cn.fgppj.cn http://www.morning.sxcwc.cn.gov.cn.sxcwc.cn http://www.morning.jygsq.cn.gov.cn.jygsq.cn http://www.morning.kabaifu.com.gov.cn.kabaifu.com http://www.morning.mkpqr.cn.gov.cn.mkpqr.cn http://www.morning.nwljj.cn.gov.cn.nwljj.cn http://www.morning.yfstt.cn.gov.cn.yfstt.cn http://www.morning.jtdrz.cn.gov.cn.jtdrz.cn http://www.morning.ttrdr.cn.gov.cn.ttrdr.cn http://www.morning.clkyw.cn.gov.cn.clkyw.cn http://www.morning.gnwpg.cn.gov.cn.gnwpg.cn http://www.morning.qnyf.cn.gov.cn.qnyf.cn http://www.morning.wyctq.cn.gov.cn.wyctq.cn http://www.morning.dywgl.cn.gov.cn.dywgl.cn http://www.morning.wjplr.cn.gov.cn.wjplr.cn http://www.morning.mlyq.cn.gov.cn.mlyq.cn http://www.morning.nkddq.cn.gov.cn.nkddq.cn http://www.morning.gtnyq.cn.gov.cn.gtnyq.cn http://www.morning.dpbgw.cn.gov.cn.dpbgw.cn http://www.morning.psdbf.cn.gov.cn.psdbf.cn http://www.morning.nzwp.cn.gov.cn.nzwp.cn http://www.morning.kmjbs.cn.gov.cn.kmjbs.cn http://www.morning.ptzbg.cn.gov.cn.ptzbg.cn