经典题 附代码+注释+运行结果
Posted 王嘻嘻-
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了经典题 附代码+注释+运行结果相关的知识,希望对你有一定的参考价值。
- 杨氏矩阵
-
qsort 无类型排序模拟实现
-
对整型排序
-
对浮点型排序
-
对double型排序
-
使用回调函数,模拟实现qsort(采用冒泡的方式)
杨氏矩阵
已知一个2维矩阵,其中的元素每一行从左至右依次增加,每一列从上到下依次增加。即对于矩阵Table有Table [ i ] [ j ] ≤Table [ i ] [ j + 1], Table [ i ] [ j ] ≤ Table [ i + 1] [ j ] (左上角值最小,右下角值最大),我们称这样的矩阵为杨氏矩阵。
Step-wise线性搜索解法:从右上角开始,每次将搜索值与右上角的值比较,如果大于右上角的值,则直接去除1行,否则,则去掉1列。算法复杂度O(n),最坏情况需要2n步,即从右上角开始查找,而要查找的目标值在左下角的时候。如下图搜索10 过程详解。
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
//杨氏矩阵
int YangSearch(int arr[][4], int row, int col, int tag)
//int YangSearch(int (*arr)[4], int row, int col, int tag)
{
if (tag<arr[0][0] || tag>arr[row - 1][col - 1]) //判断是不是比最小的还小或者比最大的还大
return 0;
int i = 0;
int j = col - 1; //从右上角开始找
while (i < row && j >= 0)
{
if (arr[i][j] > tag) //所找数小于右上角的数,说明所找数在本行,左移
{
j--;
}
else if(arr[i][j] < tag) //所找数大于右上角的数,说明所找数本行以下,下移
{
i++;
}
else {
return 1;
}
}
return 0;
}
int main()
{
int n = 0;
int arr[][4] = { {1,3,5,7},{2,4,6,8},{9,10,13,16} };
printf("Please Enter Your Number n: ");
scanf("%d", &n);
if (YangSearch(arr, 3, 4, n))
{
printf("找到 %d \\n",n);
}
else
{
printf("没找到\\n");
}
system("pause");
return 0;
}
调试结果测试:
qsort函数 无类型排序
void qsort(void * base, size_t num, size_t size,int (*comper)(const void*, const void*));
base:void* 类型,指向要排序的数据的起始地址处。
num:unsigned int类型,要进行比较的数据个数。
size:数据的大小。
compar:函数指针类型,它传入的参数是进行比较的两个数据的地址,返回值为
正数、负数、或者0,返回值决定排序按升序还是降序进行。
对整型排序
#include <stdio.h>
#include <assert.h>
#include <windows.h>
//整型
int ComInt(const void* xp,const void* yp)
{
assert(xp); //合法性校验
assert(yp); //合法性校验
const int* x = (const int*)xp; //强制类型转换
const int* y = (const int*)yp; //强制类型转换
//比较 此处为升序排列,若想降序排列,return 1 和 return -1 交换就可
if (*x > *y)
{
return 1;
}
if (*x == *y)
{
return 0;
}
else if (*x < *y)
{
return -1;
}
}
int main()
{
int arr[] = { 13,35,24,48,0,777 }; //定义并初始化数组
int len = sizeof(arr) / sizeof(arr[0]); //数组长度
qsort(arr,len,sizeof(int),ComInt);
for(int i=0;i<len;i++) //遍历数组
{
printf("%d ", arr[i]);
}
printf("\\n");
system("pause");
return 0;
}
对浮点型排序
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <windows.h>
//字符型
int CompString(const char* xp, const char* yp)
{
assert(xp); //合法性校验
assert(yp); //合法性校验
const char** x = (const char**)xp; //强制类型转换
const char** y = (const char**)yp; //强制类型转换
//return strcmp(*x, *y); //strcmp函数是用来比较字符串的
//字符串升序排列
char *_x = *x;
char* _y = *y;
while (*_x || *_y)
{
if (*_x > *_y)
{
return 1;
}
else if (*_x < *_y)
{
return -1;
}
_x++, _y++;
}
return 0;
}
int main()
{
char* arr[] = { "world","english","hello","aaa2","aaa1" }; //定义初始化一个数组指针
int len = sizeof(arr) / sizeof(arr[0]); //数组的长度
qsort(arr, len, sizeof(char*), CompString);
for (int i = 0; i < len; i++) //循环遍历数组
{
printf("%s\\n", arr[i]);
}
system("pause");
return 0;
}
对double型排序
#include <stdio.h>
#include <assert.h>
#include<string.h>
#include <windows.h>
//double qsort
int ComDouble(const void* xp, const void* yp)
{
assert(xp); //合法性校验
assert(yp); //合法性校验
const double* x = (const double*)xp; //强制类型转换
const double* y = (const double*)yp; //强制类型转换
//此处为升序排列,若想降序排列,return 1 和 return -1 交换就可
if (*x > *y)
{
return 1;
}
if (*x < *y)
{
return -1;
}
else //注意是double型 不能直接比较两个字符串相等
{
return 0;
}
}
//void Print(double arr[], int num) //构造 打印 函数
//{
// for (int i = 0; i < num; i++)
// {
// printf("%.3f ", arr[i]); //控制精度打印,小数点后保留三位
// }
// printf("\\n");
//}
int main()
{
double arr[] = { 2.12,56.001,0.246,4.9,7.213 };
int num = sizeof(arr) / sizeof(arr[0]); //数组长度or数组元素个数
//Print(arr, num); //原始数组打印
for (int i = 0; i < num; i++)
{
printf("%lf ", arr[i]);
}
printf("\\n"); //原始数组打印
qsort(arr, num, sizeof(double), ComDouble);
for (int i = 0; i < num; i++)
{
printf("%lf ", arr[i]);
}
printf("\\n"); //排序后数组打印
//Print(arr, num); //排序后数组打印
system("pause");
return 0;
}
使用回调函数,模拟实现qsort(采用冒泡的方式)
法一:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include <assert.h>
#include <windows.h>
int CompString(const void* xp, const void* yp)
{
assert(xp); //合法性校验
assert(yp); //合法性校验
const char** x = (const char**)xp; //强制转换
const char** y = (const char**)yp;
return strcmp(*x, *y);
//strcmp函数是用来比较字符串的
//int strcmp(const char *s1,const char *s2);
//自左向右逐个按照ASCII码值进行比较,直到出现不同的字符或遇’\\0’为止。
//如果返回值 < 0,则表示 s1 小于 s2。
return 0;
}
static void Swap(char *x, char *y, size_t num) //交换两个相邻元素
{
while (num)
{
char tmp = *x;
*x = *y;
*y = tmp;
x++, y++;
num--;
}
}
void MyQsort(void* base, size_t num, size_t size, int (*comp)(const void *, const void *))
{
assert(base);
assert(comp);
char* p =(char*) base; //定义一个p指针
for (size_t i = 0; i < num; i++)//冒泡几轮
{
int flag = 0;
for (size_t j = 0; j < num - i - 1; j++) //某一轮
{
if (comp(p + j * size, p + (j + 1) * size) > 0) //相邻两个元素的地址
{
Swap(p + j * size, p + (j + 1) * size, size);
flag = 1;
}
}
if (!flag) //没有交换 说明原来数组里边的值是有序的
{
break;
}
}
}
int main()
{
char* arr[] = {
"aaad",
"shaa",
"ufhs",
"4321",
"iu23"
};
int num = sizeof(arr) / sizeof(arr[0]);
printf("before#\\n");
for (int i = 0; i < num; i++)
{
printf("%s\\n", arr[i]);
}
printf("\\nafter#\\n");
MyQsort(arr, num, sizeof(char*),CompString );
for (int i = 0; i < num; i++)
{
printf("%s\\n", arr[i]);
}
system("pause");
return 0;
}
法二:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
//冒泡 使用回调函数,模拟实现qsort
int int_cmp(const void* p1, const void* p2)
{
return(*(const int *)p1 - *(const int*)p2); //p1>p2,返回1;p1<p2,返回-1;p1=p2,返回0
}
void _swap(void* p1,void* p2,int size)
{
for (int i = 0; i < size; i++) //循环遍历实现交换
{
char tmp = *((char*)p1 + i);
*((char*)p1 + i) = *((char*)p2 + i);
*((char*)p2 + i) = tmp;
}
}
void bubble(void* base, int num, int size, int(*cmp)(const void*, const void*)) //模拟qsort函数
{
for (int i = 0; i < num - 1; i++)
{
for (int j = 0; j < num - i - 1 ; j++)
{
if (cmp((char*)base + j * size, (char*)base + (j + 1) * size) > 0)
{
_swap((char*)base + j * size, (char*)base + (j + 1) * size, size);
}
}
}
}
int main()
{
int arr[] = { 87,23,56,81,3,0,1 };
int len = sizeof(arr) / sizeof(arr[0]);
bubble(arr, len, sizeof(int), int_cmp);
for (int i = 0; i < len; i++)
{
printf("%d ", arr[i]);
}
printf("\\n");
system("pause");
return 0;
}
以上是关于经典题 附代码+注释+运行结果的主要内容,如果未能解决你的问题,请参考以下文章
【Pytorch+torchvision】MNIST手写数字识别(代码附最详细注释)