1.实验任务1
#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, April~"; srand(time(0)); for (i = 1; i <= 10; ++i) { line = rand() % 25; col = rand() % 80; print_text(line, col, text); Sleep(1000); } return 0; } void print_spaces(int n) { int i; for (i = 1; i <= n; ++i) printf(" "); } void print_blank_lines(int n) { int i; for (i = 1; i <= n; ++i) printf("\n"); } void print_text(int line, int col, char text[]) { print_blank_lines(line - 1); print_spaces(col - 1); printf("%s", text); }
这个程序实现的功能是随机在某行某列打印出所给字符
2.实验任务2
#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)); return 0; } long long fac(int n) { static long long p = 1; p = p * n; printf("p = %lld\n", p); return p; }
#include <stdio.h> int func(int, int); int main() { int k = 4, m = 1, p1, p2; p1 = func(k, m); p2 = func(k, m); printf("%d, %d\n", p1, p2); return 0; } int func(int a, int b) { static int m = 0, i = 2; i += m + 1; m = i + a + b; return m; }
3.实验任务3
#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) { long long int sum; if (n == 1) sum = 1; else sum = (func(n - 1) + 1) * 2 - 1; return sum; }
4.实验任务4
#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 x; x = 0; if (n >= m) { if (m > 1) x = func(n - 1, m - 1) + func(n - 1, m); else { if (m == 0) x = 1; else x = n; } } else x = 0; return x; }
5.实验任务5
#include <stdio.h> double mypow(int x, int y); int main() { int x, y; double ans; while (scanf_s("%d%d", &x, &y) != EOF) { ans = mypow(x, y); printf("%d的%d次方: %g\n\n", x, y, ans); } return 0; } double mypow(int x, int y) { int t; t = 1; double ans; ans = 1; if(y>=0) { while(t<=y) { ans = ans * x; t = t + 1; } } else { while(t<=-y) { ans = ans / x; t = t + 1; } } return ans; }
#include <stdio.h> double mypow(int x, int y); int main() { int x, y; double ans; while (scanf_s("%d%d", &x, &y) != EOF) { ans = mypow(x, y); printf("%d的%d次方: %g\n\n", x, y, ans); } return 0; } double mypow(int x, int y) { double ans; if (y == 0) ans = 1; if (y > 0) ans = x * mypow(x, y - 1); if (y < 0) ans = 1 / mypow(x, -y); return ans; }
6.实验任务6
#include<stdio.h> int Hanoita(int n, char A, char B, char C); int cishu(int n); int main() { double sum; int n; while (scanf_s("%d", &n) != EOF) { Hanoita(n, 'A', 'B', 'C'); sum = cishu(n); printf("一共移动了%.0f次。\n", sum); } return 0; } int Hanoita(int n, char A, char B, char C) { if (1 == n) printf("%d:%c->%c\n",n, A, C); else { Hanoita(n - 1, A, C, B); printf("%d:%c->%c\n", n, A, C); Hanoita(n - 1, B, A, C); } return 0; } int cishu(int n) { double sum; int t; sum = 1; t = 1; while (t <= n) { sum = sum * 2; t = t + 1; } sum = sum - 1; return sum; }
7.实验任务7
#include<stdio.h> int is_prime(int n); int main() { int n; n = 4; while (n < 21) { int i, a1, a2; a1 = 0; a2 = 0; i = n - 1; while (a1 * a2 == 0) { i = i - 1; a1 = 0; a2 = 0; a1 = is_prime(i); a2 = is_prime(n - i); } printf("%d = %d + %d\n", n, i, n - i); n = n + 2; } return 0; } int is_prime(int n) { int t, a; t = 2; a = 1; while(t<n) { if (n % t == 0) a = 0; t = t + 1; } return a; }
8.实验任务8
#include <stdio.h> #include <math.h> long func(long s); int main() { long s, t; printf("Enter a number: "); while (scanf_s("%ld", &s) != EOF) { t = func(s); printf("new number is: %ld\n\n", t); printf("Enter a number: "); } return 0; } long func(long s) { long t, a; t = 0; a = 0; while(s>0) { a = s % 10; if (a % 2 == 1) t = 10 * t + a; s = s / 10; } long x, y; x = 0; y = 0; while(t>0) { x = t % 10; y = y * 10 + x; t = t / 10; } return y; }
标签:return,函数,int,编程,long,实验,func,ans,include From: https://www.cnblogs.com/lwh442/p/17290691.html