#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); } 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); }
利用随机数改变下个字符串出现的位置
实验二
#include <stdio.h> #include <stdlib.h> long long fac(int n); int main() { int i, n; printf("Enter n: "); scanf("%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; printf("p = %lld\n", p); p = p * n; return p; }
#include <stdio.h> #include <stdlib.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); system("pause"); return 0; } int func(int a, int b) { static int m = 0, i = 2; i += m + 1; m = i + a + b; return m; }
预期8 17
用于声明一个内存不会被收回的地址了,内存中的数据在程序运行时都不改变。
实验三
#include <stdio.h> long long func(int n); int main() { int n; long long f; while (scanf("%d", &n) != EOF) { f = func(n); printf("n = %d, f = %lld\n", n, f); } return 0; } long long fun(int n); long long func(int n) { return fun(n)-1; } long long fun(int n) { if(n==0) return 1; return 2*fun(n-1); }
实验四
#include <stdio.h> int func(int n, int m); int main() { int n, m; while(scanf("%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) { if(m==n) return 1; else if(m==0) return 1; else if(n<0) return 0; else return func(n-1,m)+func(n-1,m-1); }
实验五
#include <stdio.h>
int mul(int n, int m);
int main()
{
int n, m;
while(scanf("%d%d", &n, &m) != EOF)
printf("%d * %d = %d\n", n, m, mul(n, m));
return 0;
}
int mul(int n, int m)
{
if (n==0)
return 0;
return m+mul(n-1,m);
}
实验六
#include <stdio.h> #include <stdlib.h> int m=0; void move(int n,char a,char c) { printf("第%d个盘子,从%c移动到%c\n",n,a,c); m++; } void hanoi(int n,char a,char b,char c) { if (n==1) move(n,a,c); else { hanoi(n-1,a,c,b); move(n,a,c); hanoi(n-1,b,a,c); } } int main() { int n; char a='A',b='B',c='C'; while(scanf("%d",&n)!=EOF) { hanoi(n,a,b,c); printf("共移动了%d次\n",m); } system("pause"); return 0; }
实验七
#include <stdio.h> #include <stdlib.h> int is_prime(int); int main() { int sum,a,b; for (sum=2;sum<=20;sum=sum+2) { for(a=1;a<=17;a++) { if(is_prime(a)) { for(b=1;b<=17;b++) { if(is_prime(b)&&a<=b) { if(sum==a+b) { printf("%d =%d + %d",sum,a,b); printf("\n"); } } } } } } system("pause"); } int is_prime(int n) { int i; if (n <= 1) return 0; for (i = 2; i < n; i++) if (n % i == 0) return 0; return 1; }
实验八
#include <stdio.h> long fun(long s); int main() { long s, t; printf("Enter a number: "); while (scanf("%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[100]; long a; int i=0,b; for(;s!=0;s=s/10) { a=s%10; if(a%2!=0) { x[i]=a; i++; } } for(b=i-1;b>=0;b--) s=s*10+x[b]; return s; }标签:语句,return,int,编程,long,C语言,func,printf,include From: https://www.cnblogs.com/ws040529/p/16850680.html