//判断大小端;
int main()
{
int a = 1;
char* p = (char*)&a;
if (*p == 1)
printf("小端模式\n");
else
printf("大端模式");
return 0;
}
/
/二分查找法//
/
int FindValue(int* ar, int n, int val)
{
assert(ar != nullptr);
int pos = -1;
int left = 0, right = n - 1;
while (left<=right)
{
int mid = (right + left) / 2;
if (val < ar[mid])
{
right = mid - 1;
}
else if (val > ar[mid])
{
left = mid + 1;
}
else
{
pos = mid;
while (mid>0&&val == ar[mid - 1])
{
pos = mid - 1;
mid--;
}
break;
}
}
return pos;
}
int main()
{
//二分查找-->必须有序
int ar[] = {12,23,23,23,23,23,23,34,45,56,67,78,89,90,100 };
int n = sizeof(ar) / sizeof(ar[0]);
int k=FindValue(ar, n, 23);
printf("%d\n", k);
return 0;
}
//
//
//
//
// { 12 23 34 45 56 67 78 89 90 100 }
//
// { 45 34 23 12 100 90 89 78 67 56 }
//
// { 56 67 78 89 90 100 12 23 34 45 }
//
void swap(int* i,int* s)//交换函数
{
int temp;
temp = *i;
*i = *s;
*s = temp;
}
void Print_Ar(int* ar, int n)//打印函数
{
assert(ar != nullptr);
for (int i = 0; i < n; i++)
{
printf("%d ", ar[i]);
}
}
void convert2(int* ar, int n, int N)//设置卡口//数组内部逆序函数
{
assert(ar != nullptr);
int s = N - 1;
for (int i = n; i < (n + N) / 2; i++, s--)
{
swap(&ar[i], &ar[s]);
}
}
void Left_Move_Array_K(int* br, int k, int len)//左移k位函数
{
assert(br != nullptr);
convert2(br, 0, k);
convert2(br, k, len);
convert2(br, 0, len);
}
int main()
{
int num[] = { 12,23,34,45,56,67,78,89,99,100 };//一个整型数组
const int len = sizeof(num) / sizeof(num[0]); //计算这个数组的元素个数
int k; //定义一个变量k
printf("输入数字:");//输入一个数字
scanf_s("%d", &k);
Left_Move_Array_K(num, k,len);//调用函数
Print_Ar(num, len); //调用函数
return 0;
}
//
//
//
//
//
int my_strlen(const char* a)
{
assert(a != nullptr);
int i = 0;
while (a[i] != '\0')
{
i++;
}
return i;
}
char* my_strcopy(char* dest, const char* src)
{
assert(dest != nullptr && src != nullptr);
char* cp = dest;
while (*cp++ = *src++);
return dest;
}
//
//
//
//
//
char* my_strcat(char* dest, const char* src)
{
assert(dest != nullptr && src != nullptr);
char To[30] = {};
my_strcopy(To,src);
int i = 0;
while (To[i] != '\0')
{
i++;
}
int j = 0;
while (dest[j] != '\0')
{
To[i] = dest[j];
i++;
j++;
}
To[i] = dest[j];
return To;
}
//
//
/
///memcpy函数的实现//
/
void my_memcpy(void* dest, const void* src, size_t count)
{
assert(dest != nullptr && src != nullptr);
char* cp = (char*)dest;
char* fp = (char*)src;
for (int i = 0; i < count; i++)
{
//cp[i] = fp[i];
*(cp + i) = *(fp + i);
//*cp++ = *fp++;
}
}
/
///memset函数的实现//
/
void my_memset(void(*src), int val, int count)
{
assert(src != nullptr);
char* p = (char*)src;
for (int i = 0; i < count; i++)
{
p[i] = val;
}
}
//[]是地址偏移操作符
int main()
{
const int a = 5;
int ch[a] = { 12,23,34,45,56, };
int* p = ch;
for (int i = 0; i < a; i++)
{
printf("%d %d %d %d\n", p[i], *(p + i), i[p], *(i + p));
//[]实际上就是个地址偏移操作符
//c语言编译器就是把p[i]转换成*(p+i)处理的
//p[i]和*(p+i)无条件等价
}
return 0;
}
//比如有数组int p[4]={1,2,3,4};
//p就是这个数组的首地址
//p[2]就是从p开始偏移2个int单位后的地址内的值 就是3
//一个26*26矩阵
int main()
{
const int n = 26;
for (int i = 0; i < n; i++)
{
int k = i;
for (int j = 0; j < n; j++)
{
printf("%4d ", k);
k = (k + 1) % 26;
}
printf("\n");
}
return 0;
}
//一个26*26矩阵(DIY)
int main()
{
const int n = 25;
for (int i = 0; i < 26; i++)
{
for (int j = i; j <= n; j++)
{
printf("%4d", j);
}
for (int sum=0; sum <i; sum++)
{
printf("%4d", sum);
}
printf("\n");
}
return 0;
}
//认清*p指向x,*p就是x本身
void fun(int* p)
{
int a = 200;
printf("%d\n", *p);
*p = 100; //擦除?
printf("%d\n", *p);
p = &a;
}
int main()
{
int x = 0;
int* s = &x;
printf("%d\n", *s);
fun(s);
printf("%d %d\n", x, *s);
return 0;
}
//比较三个整数的大小
#include<stdio.h>
int Max(int a, int b, int c);
int main()
{
int a, b, c;
scanf_s("%d %d %d", &a, &b, &c);
printf("%d\n",Max(a, b, c));
return 0;
}
int Max(int a,int b,int c)
{
int max = a;
if (max < b)
max = b;
else
max = c;
return max;
}
//九九乘法表
#include<stdio.h>
int main()
{
int i, j;
for (i = 1; i < 10; i++) {
for (j = 1; j <= i; j++) {
printf("%d*%d=%d\t", i, j, i * j);
if (i == j) printf("\n");
}
}
return 0;
}
//一维数组的二维表达
#include<stdio.h>
int main()
{
int i, j;
int num[4] = { 4,1,3,2 };
for (i = 0; i <4; i++)//行数控制
{
for (j = 1; j <=4; j++)// 列数控制
{
if (j == num[i])
printf("@");
else
printf("#");
}
printf("\n");
}
return 0;
}
//输出三个整数的中间值
#include<stdio.h>
int main()
{
int num[3];
int num_one[3];
for (int i = 0; i < 3; i++)
{
printf("输入第%d个整数:", i+1);
scanf_s("%d", &num[i]);
}
if ((num[0] > num[1] && num[0] < num[2]) || (num[0]<num[1] && num[0]>num[2]))
printf("%d", num[0]);
if ((num[1] > num[0] && num[1] < num[2]) || (num[1]<num[0] && num[1]>num[2]))
printf("%d", num[1]);
if ((num[2] > num[0] && num[2] < num[1]) || (num[2]<num[0] && num[2]>num[1]))
printf("%d", num[2]);
return 0;
}
#include<ctype.h>
//统计英文字符出现次数,不区分大小写
int main()
{
int num = 0;
char ch = '\0';
while ((ch = getchar()) != '\n')
{
if (isalpha(ch))
{
num++;
}
}
printf("英文字符出现次数为:%d\n", num);
return 0;
}
#include<ctype.h>
//统计英文字符出现次数,区分大小写
int main()
{
int NUM = 0;
int num = 0;
char ch = '\0';
while ((ch = getchar()) != '\n')
{
if (isupper(ch))
{
NUM++;
}
else if (islower(ch)) {
num++;
}
}
printf("大写英文字符出现次数为:%d\n", NUM);
printf("小写英文字符出现次数为:%d\n", num);
return 0;
}
//打印数字字母表
//取余运算的循环操作
int main()
{
const int n = 26;
char ch[] = { "ABCDEFGHIJKLMNOPQRSTUVWXYZ" };
for (int i = 0; i < n; i++)
{
int k = i;
for (int j = 0; j < n; j++)
{
printf("%c",ch[k]);
k = (k + 1)%n;
}
printf("\n");
}
return 0;
}
//找最大公约数(普通法)
int main()
{
int a = 0, b = 0;
int i = 0;
int k = 0;
scanf_s("%d %d", &a, &b);
i = a < b ? a : b;
while (i > 0)
{
if (a % i == 0 && b % i == 0)
{
printf("%d\n", i);
break;
}
i--;
k++;
}
printf("%d\n", k);
return 0;
}
//找最大公约数(辗转相除法)
int main()
{
int a = 0, b = 0,i=0;
scanf_s("%d %d", &a, &b);
while (b != 0)
{
int c = a % b;
a = b;
b = c;
i++;
}
printf("%d\n%d\n", a,i);
return 0;
}
所用到的头文件
#include<stdio.h>
#include<ctype.h>
//第一题
//统计各字符个数
void Statistical_element()
{
char c;
int letters = 0;
int space = 0;
int digit = 0;
int others = 0;
while ((c = getchar()) != '\n')//直接用<ctype.h>给的函数
{
if (isalpha(c))
{
letters++;
}
else if (isblank(c))
{
space++;
}
else if (isdigit(c))
{
digit++;
}
else {
others++;
}
}
}
//第二题
//求和
int S_ummation(int n)
{
int a = 2;
int sum = 0;
int d = 0;
scanf_s("%d", &n); //输入n
for (int i = 0; i < n; i++) //控制累加循环次数
{
d = d * 10 + a;
sum += d;
}
return sum;
}
//第三题
//输出所有“水仙花数”
void Print_Daffodils()
{
int a = 0;//准备存放水仙花数的个位
int b = 0;//·········十位
int c = 0;//·········百位
for (int i = 100; i < 1000; i++)//遍历所有三位数
{
int num = i;
a = num % 10;
num /= 10;
b = num % 10;
num /= 10;
c = num;
if ((a * a * a + b * b * b + c * c * c) == i)
printf("%d ", i);
}
}
//第四题
//寻找1000以内的完数并输出
void Looking_for_Perfect_number()
{
int i, j, sum;
for (i = 1; i <= 1000; i++)//遍历1000以内所以整数
{
sum = 0;//每次循环初始化sum
for (j = 1; j < i; j++)//从1到i寻找因子
if (i % j == 0)//判断是否为此数的因子
sum += j;//如果是的话就加到sum里
if (i == sum)//检验是否符合完数的定义
{
printf("%d its factors are ", i);
for (j = 1; j < i; j++)
{
if (i % j == 0)
printf("%d,", j);
}
printf("\n");
}
}
}