随笔分类 - c/c++
摘要:1、 #include <stdio.h> int gcd(int x, int y) { int max, min, tmp; if(x != y) { max = x > y ? x : y; min = x > y ? y : x; tmp = max % min; if(tmp != 0)
阅读全文
摘要:1、 #include <stdio.h> int fac(int x) { if(x > 0) return x * fac(x - 1); else return 1; } int main(void) { int a, b; puts("please input two integers.")
阅读全文
摘要:1、 #include <stdio.h> int fac(int x) { int i, fac = 1; if(x > 0) { for(i = x; i > 0; i--) fac *= i; } else fac = 1; return fac; } int main(void) { int
阅读全文
摘要:c语言中利用递归求非负整数的阶乘。 1、 #include <stdio.h> int factorial(int x) { if(x > 0) return x * factorial(x - 1); else return 1; } int main(void) { int a; puts("p
阅读全文
摘要:1、 #include <stdio.h> enum info { Gender, Season, Age, Invalid}; void gender(void) { puts("male.\n"); } void season(void) { puts("summer.\n"); } void
阅读全文
摘要:1、 #include <stdio.h> #define NUMBER 5 void bsort(int x[], int n) { int i, j; for(i = 0; i < n - 1; i++) { for(j = 1; j < n - i; j++) { if(x[j - 1] >
阅读全文
摘要:1、 #include <stdio.h> enum animal {Dog, Cat, Monkey, Invalid}; void dog(void) { puts("wang wang\n"); } void cat(void) { puts("miao miao\n"); } void mo
阅读全文
摘要:c语言中冒泡排序法。 1、升序排列 #include <stdio.h> #define NUMBER 5 void bsort(int x[], int n) { int i, j; for(i = 0; i < n - 1; i++) { for(j = n - 1; j > i; j--) {
阅读全文
摘要:c语言函数式宏、逗号表达式 一般由逗号运算符连接的两个表达式“a, b”在语法上可以视为一个表达式,在表达式后面添加分号,就构成了表达式语句。 #include <stdio.h> #define puts_alert(str) (putchar('\a'), puts(str)) int main
阅读全文
摘要:1、 #include <stdio.h> #define swap(type, a, b) {type tmp = a; a = b; b = tmp;} int main(void) { int x = 5, y = 10; printf("initial value x = %d\n", x)
阅读全文
摘要:定义函数式宏,返回两个数中的最大值。 1、 #include <stdio.h> #define max(x, y) ((x > y) ? (x) : (y)) int main(void) { int a, b, c, d; puts("please input four integers.");
阅读全文
摘要:定义一个函数式宏diff(x, y),返回x, y的差。 1、 #include <stdio.h> #define diff(x, y) ((x) - (y)) int main(void) { int a, b; puts("please input two integers."); print
阅读全文
摘要:c语言中使用函数式宏返回不同数据类型的值的平方。 1、 #include <stdio.h> #define sqr(x) ((x) * (x)) int main(void) { int a; puts("please input an integer."); printf("a = "); sc
阅读全文
摘要:c语言中使用十进制、二进制、八进制和十六进制输出0到65535的整数。 1、 #include <stdio.h> int count_bits(unsigned x) { int bits = 0; while(x) { if(x & 1U) bits++; x >>= 1; } return b
阅读全文
摘要:c语言中printf函数输出十进制、八进制和十六进制数。 1、 #include <stdio.h> int main(void) { unsigned a = 45; printf("101010 %u\n", a); printf("888888 %o\n", a); printf("16161
阅读全文
摘要:1、 #include <stdio.h> unsigned set_n(unsigned x, int pos, int n) { int i; for(i = pos; i <= pos + n - 1; i++) { x = x | 1U << i; } return x; } unsigne
阅读全文
摘要:1、 #include <stdio.h> unsigned set(unsigned x, int pos) { return x | 1U << pos; } unsigned reset(unsigned x, int pos) { return x & (~(1U << pos)); } u
阅读全文
摘要:1、 #include <stdio.h> unsigned rrotate(unsigned x, int n) { return x >> n; } unsigned lrotate(unsigned x, int n) { return x << n; } int main(void) { u
阅读全文
摘要:1、 #include <stdio.h> #include <math.h> int main(void) { unsigned x, n; printf("test number: "); scanf("%u", &x); printf("move bits : "); scanf("%u",
阅读全文
摘要:c语言中输出十进制转换为二进制结果并指定显示的位数。 1、 #include <stdio.h> int count_bits(unsigned x) { int bits = 0; while(x) { if(x & 1U) bits++; x >>= 1; } return bits; } in
阅读全文

浙公网安备 33010602011771号