简单算法随笔(两个整形的交换、二分查找、求n的阶乘 ...)
1 #define _CRT_SECURE_NO_WARNINGS 2 3 #include <stdio.h> 4 #include <limits.h> 5 6 //两整形的交换 7 int main(){ 8 9 //int a = 3; int b = 5; 10 //int tem = 3; 11 //a = b; 12 //b = tem; 13 //printf("%d %d\n", a, b); // 5 3 14 15 //增加条件:不准使用临时变量 16 //1: 17 //int a = 3; int b = 5; 18 //a = a + b; 19 //b = a - b; 20 //a = a - b; 21 //printf("%d %d\n", a, b); // 5 3 此方法有不足,int是4个字节32个比特位最大存储数值是INT_MAX即2147483647(查看需头文件limits.h),如果超出范围将溢出 22 23 //2: 24 int a = 3; int b = 5; 25 a = a^b; // 011 ^ 101 = 110 26 b = a^b; // 110 ^ 101 = 011 27 a = a^b; // 110 ^ 011 = 101 28 printf("%d %d\n", a, b); // 5 3 结论:两个数字异或得第三个结果,使用第三个结果异或其中的一个数值得另外一个数字 29 30 return 0; 31 }
1 #define _CRT_SECURE_NO_WARNINGS 2 3 #include <stdio.h> 4 5 // 找出只出现过一次的数字 6 7 int main(){ 8 9 // 1: 此种算法匹配次数较多,可进行优化 10 //int arr1[] = {1,2,3,4,5,1,2,3,4}; 11 //int lenght = sizeof(arr1) / sizeof(arr1[0]); 12 //for (int i = 0; i < lenght; i++){ 13 // int count = 0; 14 // for (int j = 0; j < lenght; j++){ 15 // if (arr1[i] == arr1[j]){ 16 // count++; 17 // }; 18 // }; 19 // if (count == 1){ 20 // printf("%d\n", arr1[i]); 21 // break; 22 // }; 23 //}; 24 25 // 2: 26 int arr1[] = { 1, 2, 3, 4, 5, 1, 2, 3, 4 }; 27 int lenght = sizeof(arr1) / sizeof(arr1[0]); 28 int tem = 0; 29 for (int i = 0; i < lenght; i++){ 30 tem ^= arr1[i]; // 实验得到:a^a = 0; 0^a = a; a^a^b = b;a^b^a = b; 31 } 32 printf("%d\n", tem); 33 34 return 0; 35 }
1 #define _CRT_SECURE_NO_WARNINGS 2 3 #include <stdio.h> 4 5 // 使用递归按顺序打印整形1234 6 int print(int unm){ 7 8 if (unm > 9){ 9 print(unm / 10); 10 }; 11 printf("%d \n", unm % 10); // 1 2 3 4 12 } 13 14 int main(){ 15 int unm = 1234; 16 print(unm); 17 return 0; 18 }
1 #define _CRT_SECURE_NO_WARNINGS 2 3 #include <stdio.h> 4 5 // 不创建临时变量求字符串长度 6 7 // 1: 8 //int str(char* one){ 9 // int count = 0; 10 // while(*one != '\0'){ 11 // count++; 12 // one++; 13 // } 14 // return count; 15 //} 16 17 //2: 18 int str(char* one){ 19 if (*one != '\0') 20 return 1 + str(one + 1); 21 else 22 return 0; 23 } 24 25 int main(){ 26 27 char arr1[] = "asdfghjkl"; 28 int lenght = str(arr1); 29 printf("%d\n",lenght); 30 return 0; 31 }
1 #define _CRT_SECURE_NO_WARNINGS 2 3 #include <stdio.h> 4 5 // 求n的阶乘 6 7 // 注:如5的阶乘等于 5*4*3*2*1 = 120 8 //1: 9 //int func(int input){ 10 // int unm = 1; 11 // for (int i = 1; i <= input; i++){ 12 // unm *= i; // 1*1*2*3*4*5... 13 // } 14 // return unm; 15 //} 16 //2: 17 int func(int input){ // 如果input等于5最后一个等于1将返回1,倒数第二个返回4*1给倒数第三个依此类推 18 if (input <= 1) 19 return 1; 20 else 21 return input*func(input - 1); // ...5*4*3*2*1 递归一般都会有终止条件、有条件与终止条件越来越接近、最后爆发~ 22 } 23 int main(){ 24 int input = 0; int ret = 0; 25 printf("请输入一个数字:"); 26 scanf("%d", &input); 27 ret = func(input); 28 printf("它的阶乘是 %d\n", ret); 29 return 0; 30 }
1 //求第n个斐波纳契数是什么: 2 //注斐波纳契数是 1 1 2 3 5 8 13 21 34 55 ...,即前后面的数字是前连个数字的和 3 4 //1: 5 //int func(int n){ // 次方法过于消耗,如果是50则找49,48,49、48有要找依此类推.. 6 // if (n <= 2) 7 // return 1; 8 // else 9 // return func(n - 1) + func(n-2); 10 //} 11 12 //2: 13 int func(int n){ 14 int a = 1; int b = 1; int c = 0; 15 while (n>2){ 16 c = a + b; 17 a = b; 18 b = c; 19 n--; 20 }; 21 return c; 22 } 23 24 int main(){ 25 int n = 0; int ret = 0; 26 scanf("请输入第几个%d",&n); 27 ret = func(n); 28 printf("第%d个的斐波那契数是:%d",n,ret); 29 return 0; 30 }
1 #define _CRT_SECURE_NO_WARNINGS 2 3 #include <stdio.h> 4 5 // 算法之二分查找 6 // 注此算法粗数据有序 7 8 int main(){ 9 int arr[] = {1,2,3,4,5,6,7,8,9,10}; //找出6的索引值 10 11 int lenght = sizeof(arr) / sizeof(arr[0]); 12 int left = 0; int right = lenght - 1; 13 14 while (left <= right){ 15 int index = (left + right) / 2; 16 if (arr[index] > 6){ 17 right = index - 1; 18 } 19 else if (arr[index] < 6){ 20 left = index + 1; 21 } 22 else{ 23 printf("找到了,它的索引是:%d\n", index); 24 break; 25 } 26 } 27 if (left > right){ 28 printf("找不到\n"); 29 } 30 31 return 0; 32 }
1 // 九九乘法表 2 3 int main(){ 4 5 for (int i = 1; i <= 9;i++){ 6 for (int j = 1; j <= i;j++){ 7 printf("%d*%d=%-2d ", i,j,i*j); 8 } 9 printf("\n"); 10 } 11 12 return 0; 13 }
1 // 素数求解(100到200之间) 2 // 注:素数只能被1和自身整除本案例采用试除法,以后学了其它的再加上去 3 4 // 1: 5 int main(){ 6 7 for (int i = 100; i <= 200; i++){ 8 9 int j = 2; 10 for (j = 2; j < 200; j++){ 11 if (i%j == 0){ 12 break; 13 }; 14 }; 15 if (i == j){ 16 printf("%d ",i); 17 } 18 } 19 20 return 0; 21 } 22 23 //2(优化): 24 int main(){ 25 for (int i = 101; i <= 200; i+=2) 26 { 27 int j = 2; 28 for (j = 2; j <= sqrt(i); j++) 29 { 30 if (i%j == 0) 31 { 32 break; 33 } 34 } 35 if(j > sqrt(i)) 36 { 37 printf("%d ", i); 38 } 39 } 40 return 0; 41 }
1 void reverse(int* arr, int len){ 2 3 int left = 0; int right = len - 1; 4 5 while(left < right){ 6 int tem = arr[left]; 7 arr[left] = arr[right]; 8 arr[right] = tem; 9 left++; right--; 10 } 11 12 } 13 14 void print(int* arr, int len){ 15 for (int i = 0; i < len; i++){ 16 printf("%d", arr[i]); 17 } 18 } 19 20 int main(){ 21 int arr[10] = {1,2,3,4,5,6,7,8,9,10}; 22 int len = sizeof(arr) / sizeof(arr[0]); 23 reverse(arr,len); 24 print(arr, len); 25 return 0; 26 }
1 //交换两个数组的内容 2 int main(){ 3 int arr1[] = {1,3,5,7,9}; 4 int arr2[] = {2,4,6,8,0}; 5 int len = sizeof(arr1) / sizeof(arr1[0]); 6 int tem = 0; 7 for (int i = 0; i < len; i++){ 8 tem = arr1[i]; 9 arr1[i] = arr2[i]; 10 arr2[i] = tem; 11 } 12 printf("%d", arr1[0]); 13 return 0; 14 }
1 //求一个数字的二进制中(补码)有几个1 2 3 // 1: 4 //int countOne(unsigned int a){ 5 // int count = 0; 6 // while (a){ 7 // if (a % 2 == 1){ 8 // count++; 9 // }; 10 // a = a / 2; 11 // }; 12 // return count; 13 //} 14 15 // 2: 16 //int countOne(int a){ 17 // int count = 0; 18 // for (int i = 0; i < 32; i++){ 19 // if (((a >> i) & 1) == 1){ 20 // count++; 21 // }; 22 // }; 23 // return count; 24 //} 25 26 // 3: 27 // 13二进制为1101;公式: n = n&(n-1); 结果:1101 n、 1100 n-1、 1100 n、 1011 n-1、 1000 n、 0111 n-1、 0000 n;结论:每次尾数1会消失,且n最终为0 28 int countOne(int a){ 29 int count = 0; 30 while (a){ 31 a = a&(a - 1); 32 count++; 33 } 34 return count; 35 } 36 37 int main(){ 38 int a = 13; 39 // 00000000000000000000000000000011 40 int count = countOne(a); 41 printf("%d\n", count); 42 return 0; 43 }
1 int func(int m, int n){ 2 int tem = m^n; 3 int count = 0; 4 while (tem){ 5 tem = tem&(tem - 1); 6 count++; 7 }; 8 return count; 9 }; 10 int main(){ 11 int m = 1999; int n = 2299; 12 int count = func(m, n); 13 printf("%d\n", count); 14 return 0; 15 }
1 void func(int n){ 2 //奇数 3 for (int i = 30; i >= 0; i -= 2){ 4 printf("%d", (n >> i) & 1); 5 } 6 printf("\n"); 7 8 // 偶数 9 for (int i = 31; i >= 0; i -= 2){ 10 printf("%d", (n >> i) & 1); 11 } 12 printf("\n"); 13 } 14 15 int main(){ 16 int n = 13; 17 func(n); 18 return 0; 19 }
1 int func(unsigned int n){ 2 if (n > 9){ 3 return func(n / 10) + n % 10; 4 } 5 else{ 6 return n; 7 } 8 } 9 10 int main(){ 11 int n = 1729; 12 int unm = func(n); 13 printf("%d\n", unm); 14 return 0; 15 }
1 // 判断当前机器是大端存储还是小端存储。**指针类型决定了指针解引用操作符能访问几个字节**;**指针类型决定了指针+1,-1会是几个字节**; 2 3 //1: 4 //int main(){ 5 // int n = 1; 6 // char* p = (char*)&n; 7 // if (*p == 1){ 8 // printf("小端\n"); 9 // } 10 // else{ 11 // printf("大端\n"); 12 // } 13 // 14 // return 0; 15 //} 16 17 //2: 18 int func(){ 19 int n = 1; 20 return *(char*)&n; 21 } 22 int main(){ 23 int n = func(); 24 if (n == 1){ 25 printf("小端\n"); 26 } 27 else{ 28 printf("大端\n"); 29 } 30 return 0; 31 }
本文来自博客园,作者:封兴旺,转载请注明原文链接:https://www.cnblogs.com/fxw1/p/15064583.html