TASK1
#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);
}
功能:每隔一秒随机打印一个“hi,april”,位置由函数决定。
TASK2.1.1
#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;
p = p * n;
return p;
}
TASK2.1.2
#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;
}
TASK2.2
#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;
}
TASK3
#include <stdio.h>
#include<stdlib.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);
}
system("pause");
return 0;
}
// 函数定义
long long func(int n)
{
long long f;
if(n == 1){
f = 1;
}
else
{
f = 2*func(n-1)+1;
}
return f;
}
TASK4
迭代法:
#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){
int s,a=1,b=1,c=1,d;
if(m==0){
s=1;
}
else if(m>n){
s=0;
}
else{
for(d=n-m;n>0;){
a=a*n--;
if(m>0)
b=b*m--;
if(d>0)
c=c*d;
}
s=a/(b*c);
}
return s;
}
递归法:
#include<stdio.h>
#include<stdlib.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));
system("pause");
return 0;
}
int func(int n,int m)
{
int ans;
if(n<m)
ans=0;
else if(m==n)
ans=1;
else if(m==1)
ans=n;
else if(m==0)
ans=1;
else
ans=func(n-1,m)+func(n-1,m-1);
return ans;
}
TASK5
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void plate(char from,char to,char temp ,unsigned int n,int *p);
void trans(char from,char to,unsigned int n,int *p);
int main()
{
unsigned int n;
while(scanf("%d",&n)!=EOF)
{
int s=0;
plate('A','C','B',n,&s);
printf("\n一共移动了%d次\n\n",s);
}
system("pause");
return 0;
}
void plate(char from,char to,char temp ,unsigned int n,int *p)
{
if(n==1)
trans(from,to,n,p);
else
{
plate(from,temp,to,n-1,p);
trans(from,to,n,p);
plate(temp,to,from,n-1,p);
}
}
void trans(char from,char to,unsigned int n,int *p)
{
printf("%d: %c --> %c\n",n,from,to);
*p=*p+1;
}
TASK6
#include <stdio.h>
#include <math.h>
long func(long s);
int main() {
long s, t;
printf("Enter a number: ");
while (scanf("%ld", &s) != EOF) {
t = func(s);
printf("new number is: %ld\n\n", t);
printf("Enter a number: ");
}
return 0;
}
long func(long s){
int i=0,n,m=1;
while(s){
n=s%10;
if(n%2!=0){
i=i+n*m;
m=m*10;
}
s=s/10;
}
return i;
}
标签:int,long,char,实验,func,printf,include
From: https://www.cnblogs.com/xhzwd/p/18164588