#include <stdio.h> #include <stdlib.h> #include <time.h> #include <windows.h> #define N 80 void print_text(int line, int col, char text[]); // 函数声明 void print_spaces(int n); // 函数声明 void print_blank_lines(int n); // 函数声明 int main() { int line, col, i; char text[N] = "hi, November~"; srand(time(0)); // 以当前系统时间作为随机种子 for (i = 1; i <= 10; ++i) { line = rand() % 25; col = rand() % 80; print_text(line, col, text); Sleep(1000); // 暂停1000ms } return 0; } // 打印n个空格 void print_spaces(int n) { int i; for (i = 1; i <= n; ++i) printf(" "); } // 打印n行空白行 void print_blank_lines(int n) { int i; for (i = 1; i <= n; ++i) printf("\n"); } // 在第line行第col列打印一段文本 void print_text(int line, int col, char text[]) { print_blank_lines(line - 1); // 打印(line-1)行空行 print_spaces(col - 1); // 打印(col-1)列空格 printf("%s", text); // 在第line行、col列输出text中字符串 }
//随机地在任意行任意列打印出一个“hi,noverbe”
task2
#include <stdlib.h>// 利用局部static变量的特性,计算阶乘 #include <stdio.h> long long fac(int n); // 函数声明 int main() { int i, n; printf("Enter n: "); scanf_s("%d", &n); for (i = 1; i <= n; ++i) printf("%d! = %lld\n", i, fac(i)); system("pause"); return 0; } // 函数定义 long long fac(int n) { static long long p = 1; p = p * n;printf("p = %lld\n", p);
return p; }
task3
#include<stdlib.h> #include <stdio.h> long long func(int n); // 函数声明 int main() { int n; long long f; while (scanf_s("%d", &n) != EOF) { f = func(n); // 函数调用 printf("n = %d, f = %lld\n", n, f); } return 0; } long long func(int n){ int sum = 0; if (n == 0) { sum = 0; } else { sum = 2 * func(n - 1) + 1; } return sum; }
task4
#include<stdlib.h> #include <stdio.h> int func(int n, int m); int main() { int n, m; while (scanf_s("%d%d", &n, &m) != EOF) printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m)); return 0; } int func(int n, int m) { int sum = 0; if (m == 0) { sum = 1; } else if(m == 1) { sum = n; } else if (m == n) { sum = 1; } else if (m > n) { sum = 0; } else { sum = func(n - 1, m) + func(n - 1, m - 1); } return sum; }
task5
#include<stdlib.h> #include <stdio.h> int mul(int n, int m); int main() { int n, m; while (scanf_s("%d%d", &n, &m) != EOF) printf("%d * %d = %d\n", n, m, mul(n, m)); return 0; } int mul(int n, int m) { int sum = 0; if (m == 0) { sum = 0; } else if(m == 1) { sum = n; } else { sum = mul(n, m - 1) + n; } return sum; }
task6
#include<stdlib.h> #include <stdio.h> void hanoi(unsigned int n, char from, char temp, char to); void moveplate(unsigned int n, char from, char to); int count = 0; int main() { unsigned int n; while (scanf_s("%u", &n) != EOF) { hanoi(n, 'A', 'B', 'C'); printf("一共移动了%d次\n", count); count = 0; } } void hanoi(unsigned int n, char from, char temp, char to) { int count = 0; if (n == 1) { moveplate(n, from, to); } else { hanoi(n - 1, from, to, temp); moveplate(n, from, to); hanoi(n - 1, temp, from, to); } } void moveplate(unsigned int n, char from, char to) { count++; printf("%u:%c---->%c\n", n, from, to); }
task7
#include<stdlib.h> #include <stdio.h> #include<math.h> int isprime(int n); int prime(int); int main() { int n; while (scanf_s("%d", &n) != EOF) { int p = 2; int q = n - p; int flag = 1; while (flag) { if (isprime(q) * isprime(p) == 1) { printf("%d = %d + %d\n", n, p, q); flag = 0; } else { p += 1; q = n - p; } } } } int isprime(int n) { int flag = 0; for (int i = 2; i <= sqrt(1.0 * n); i++) { if (n % i == 0) { flag = 1; break; } } if (flag) { return 0; } else { return 1; } } int prime(int n) { int i; int a = 0; if (n < 2) printf("%d不是素数\n", n); else { for (i = 2; i < n; i++) { if (n % i == 0) a++; } if (a == 0) printf("%d是素数\n", n); else printf("%d不是素数\n", n); } return 0; }
task8
#include<stdlib.h> #include <stdio.h> #include<math.h> #include <stdio.h> long fun(long s); // 函数声明 int main() { long s, t; printf("Enter a number: "); while (scanf_s("%ld", &s) != EOF) { t = fun(s); // 函数调用 printf("new number is: %ld\n\n", t); printf("Enter a number: "); } return 0; } long fun(long s) { int x = s; int m [100]; int i = -1; int result = 0; while (x != 0) { if (x % 2 == 1) { i++; m [i] = x % 10; } x = x / 10; } for (int j = i; j >= 0; j--) { result = result * 10 + m[j]; } return result; }
总结;
1.注意task8中的i 的初始化值为 -1;这样i++写在数组之前,不会让遍历出错。
标签:int,sum,long,char,实验,printf,include From: https://www.cnblogs.com/shenyuhao/p/16849972.html