C语言学习4-1: 汉若塔。10进制装二进制,8进制,16进制。杨辉三角。
1.。。。。。。。。。。。。。。。。。。。。
#include <stdio.h> void hanoi(char from, char pass, char to, unsigned n) { if (n == 1) { printf("%c ==> %c\n", from, to); } else { hanoi(from, to, pass, n - 1); hanoi(from, pass, to, 1); hanoi(pass, from, to, n - 1); } } void hanoi1(char from, char pass, char to, unsigned n) { if (n == 0) return; hanoi(from, to, pass, n - 1); printf("%c ==> %c\n", from, to); hanoi(pass, from, to, n - 1); } int main(void) { unsigned n; printf("pls input n: "); scanf("%d", &n); hanoi1('A', 'B', 'C', n); return 0; }
2.。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
#include <stdio.h> void __foo(unsigned n, unsigned radix) { unsigned q, r; static level; if (n <= 0) return; r = n % radix; q = n / radix; level++; __foo(q, radix); level--; if (r > 9) putchar(r - 10 + 'a'); else putchar(r + '0'); if (level % 4 == 0) putchar(' '); } void foo(unsigned n, unsigned radix) { if (n == 0) { printf("0\n"); return; } if (radix != 2 && radix != 8 && radix != 16) return; if (radix == 16) printf("0x"); else if (radix == 8) printf("o"); else printf("0b"); __foo(n, radix); putchar('\n'); } int main(void) { unsigned n, base; printf("input n & base: "); scanf("%d%d", &n, &base); printf("transformed: "); foo(n, base); return 0; }
level是用来分四个四个的
3,。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
#include <stdio.h> unsigned yh_triangle(unsigned row, unsigned col) { if (col == 1 || row == col) return 1; return yh_triangle(row - 1, col - 1) + yh_triangle(row - 1, col); } int main(void) { unsigned n; printf("input n: "); scanf("%d", &n); int i, j; for (i = 1; i <= n; i++) { for (j = 1; j <= i; j++) { printf("%4d ", yh_triangle(i, j)); } putchar('\n'); } return 0; }
结果:
input n: 6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1