#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] = "hl,April~"; 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) printff("\n"); } //在第line 行第col列1打印一段文本 void print_text(int line,int col,char text[]){ print_blank_lines(line-1); print_spaces(col-1); printf("%s",text); }
随机打印hi,April
#include<stdio.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! = %11d\n",i,fac(i)); return 0; } long long fac(int n){ static long long p=1; p=p*n; printf("p = %11d\n",p); return p; }
只赋值一次,之后每次调用函数时保留上次调用函数的值
#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=%11d\n",n,f); } return 0; } long long func(int n) { long long int f; if(n==1) f=1; else f=2*func(n-1)+1; return f; }
#include <stdio.h> int func(int n, int m); int ans=0;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(n==m) ans=1; if(m==0) ans=1; if(n<m) ans=0; if(n>m) { if( m !=0) ans = func(n-1,m)+func(n-1,m-1); else ans=1; } return ans; }
#include <stdio.h> double mypow(int x, int y); int main() { int x, y; double ans; while(scanf("%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 i; double ans=1.0; if(y>=0) { for(i=1;i<=y;i++) ans=ans*x; return ans;
else { for(i=1;(i+y)<=0;i++) ans=ans/x; return ans; } }
#include <stdio.h> double mypow(int x, int y); int main() { int x, y; double ans; while(scanf("%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=1; if(y==0) ans=1; if(y>0) ans=ans*x*mypow(x,y-1); if(y<0) ans=ans/x*mypow(x,y+1);
return ans; }
#include<stdio.h> #include<stdlib.h> void hanoi(unsigned int n,char from, char temp, char to); void moveplate(unsigned int nth,char from,char to); int main(){ unsigned int n; while(scanf("%u",&n) !=EOF) hanoi(n,'A','B','C'); return 0; } void hanoi(unsigned n,char from, char temp, char to){ 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 nth,char from,char to){ printf("%u:%c-->%c\n",nth,from,to); }
#include <stdio.h> int is_prime(int n); int main(){ int i,j,k; for(i=2;i<=10;i++){ for(j=2;j<=i;j++){ if(is_prime(2*i - j) == 1 && is_prime(j) == 1) { printf("%d = %d + %d\n", 2*i, j, 2*i - j); break; } } } } int is_prime(int n) { int i; for( i = 2; i < n; i++) { if(n % i == 0) return 0; } return 1; }
#include <stdio.h> #include <stdlib.h> #include <math.h> long func(long s); int main(){ long s,t,i,m,n,k; printf("Enter a number:"); while(scanf("%ld",&s)!=EOF){ t=func(s); printf("new number is:%ld\n\n",t); printf("Enter a number:"); } system("pause"); return 0; } long func(long s){long i,n,k,m=0; for(i=s,k=0;i>0;k++){ n=i%10; i=i/10; if ((n%2==0)||n==0) k--; else m=m+n*(pow(10,k));} return m;}
标签:int,long,char,实验,func,ans,include From: https://www.cnblogs.com/cybcyb/p/17290057.html