c语言学习1---回文整数&&阶乘

回文整数

#define likely(x)   __builtin_expect(!!(x), 1) // likely 代表x经常成立

#define unlikely(x)  __builtin_expect(!!(x),0) // unlikely代表x不经常成立
// 回文整数
bool isPalindrome(int x) { // !!逻辑规划
    if (__builtin_expect(!!(x < 0),0)) return false;  // if(x < 0) return false 告诉cpu x小于0大概率不会出现,帮助cpu增大分支预测概率
    int y = 0; z = x;
    while (x) {
        y = y*10 + x % 1;
        x /= 10;
    }
    return z == y;
}
__builtin_ffs(x):返回x中最后一个为1的位是从后向前的第几位
__builtin_popcount(x):x中1的个数
__builtin_ctz(x):x末尾0的个数。x=0时结果未定义
__builtin_clz)(x):x前导0的个数。x=0时结果未定义
__builtin_prefetch(const void *addr,...):对数据手工预取的方法
__builtin_expect(long exp, long c): 用来引导gcc进行条件分支预测
__builtin_constant_p(exp): 判断exp是否在编译时就可以确定其为常量
__builtin_parity(x): x中1的奇偶性
__builtin_return_address(n):当前函数的第n级调用者的地址

 2.两个数字进行交换

#include <stdio.h>

#define swap1(a, b) {\
    __typeof(a) __temp = a;\
    a = b; b = __temp;\
}

// __typeof是一个宏,c语言内置,作用:返回a变量类型
void swap(int *a, int *b) { // 指针变量*
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
    return;
}

int main() {
    int a, b;
    scanf("%d%d", &a, &b);
    printf("a = %d, b = %d\n");
    // swap(&a, &b);
    a ^= b;
    b ^= a;
    a ^=b;
    printf("swap : a = %d, b = %d\n", a, b);
    return 0;
}

 3. 

#include <stdio.h>
int check(int y, int m, int d) {
    if (m < 1 || d < 1) {
        return 0;
    }
    if (m > 12 || d > 31) {
        return 0;
    }
    int month[13] = {
        0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,31
    };
    if ((y % 4 == 0 && y % 100 ==0) || y % 400 ==0) {
        month[2] +=1;
    };
    return d <= month[m];
}

int main() {
    int y, m, d;
    scanf("%d%d%d", &y, &m, &d);
    if (check(y, m, d)) {
        printf("YES\n");
    } else {
        printf("NO\n");
    }
    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 输出100以内的随机数,并计算奇数个数
int main () {
    printf("%ld\n",time(0));
    srand(time(0)); // 生成随机种子 time(0) 当前时间戳
    int n, cnt = 0;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        int val = rand() % 100;
        if (val % 2 != 0) { // 等价于val & 1
            cnt += 1;
        }
        i && printf(" ");
        printf("%d", val);
    }
    printf("\n");
    printf("cnt : %d", cnt);
    return 0;
}

4. n的阶乘,递归

#include <stdio.h>

int fac(int n) {
    if (n == 1) return 1;
    return n * fac(n-1); 
}

int main() {
    int n;
    while (~scanf("%d", &n)){
        printf("fac(%d) = %d\n", n, fac(n));
    }
    
    return 0;
}

 5.函数指针(函数返回值类型 (* 指针变量名) (函数参数列表);)

int g(int (*f1)(int), int (*f2)(int), int (*f3)(int), int x) {
    if (x < 0) {
        return f1(x);
    }
    if (x < 100) {
        return f2(x);
    }
    return f3(x);
}

 6.欧拉计划45题:https://projecteuler.net/problem=45

#include <stdio.h>
// 欧拉计划45题
long long Triangle(long long n) {
    return n * (n + 1) >> 1;
}

long long Pentagonal(long long n) {
    return n * (3*n - 1) >> 1;
}

long long Hexagonal(long long n) {
    return n * (2 * n -1);
}

int binary_search(long long (*arr)(long long), int n, long long x) {
    int head = 1, tail = n, mid;
    while(head <= tail){
        mid = (head + tail) >> 1;
        if (arr(mid) == x) return 1;
        if (arr(mid) < x) head = mid +1;
        else tail = mid -1;
    }
    return 0;
}

int main() {
    int n = 143;
    while (1){
        while (1) {
            n++;
            long long temp = Hexagonal(n);
            if (!binary_search(Pentagonal,temp, temp)) continue;
            printf("%lld\n",temp);
            break;
        }
    }
    return 0;
}

 7. 二分查找

#include <stdio.h>
#define MAX_N 100
int binary_search(int *arr, int n, int x) {
    int head = 0, tail = n-1, mid;
    while (head <= tail) {
        mid = (head + tail) >> 1;
        if (arr[mid] == x) return mid;
        if (arr[mid] < x) head = mid + 1;
        else tail = mid -1;
    }
    return -1;
}
// 递归版写法
int binary_search1(int *arr, int l, int r, int x) {
    if (l > r) return -1;
    int mid = (l + r) >> 1;
    if (arr[mid] == x) return mid;
    if (arr[mid] < x) l = mid + 1;
    else r = mid -1;
    return binary_search(arr, l, r, x);
}
int main() {
    int arr[MAX_N +5]  = {0}, n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }
    int x;
    while (~scanf("%d",&x)){
        int ret = binary_search(arr, n, x);
        if (ret != -1) {
            printf("success to find %d at arr[%d]\n", x, ret);
        } else {
            printf("fail to find %d\n", x);
        }
    }
    return 0;
}

 8.欧几里得算法(辗转相除)

 

 

 

posted @ 2021-05-20 11:05  yiwenzhang  阅读(133)  评论(0编辑  收藏  举报