首页 > 其他分享 >实验三

实验三

时间:2024-04-22 20:36:36浏览次数:18  
标签:return int long char 实验 func include

点击查看代码
#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);  // 暂停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中字符串
}

作用是每秒在25x80的区域内随机产生一个hi, April~

2.11

点击查看代码
// 利用局部static变量的特性,计算阶乘

#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;
}

2.12

点击查看代码
#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;
}

2.2

点击查看代码
#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;
}

static的作用是使变量只赋值一次。

点击查看代码
#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)
{
	if(n==0)
		return 0;
	if(n>=1)
		return 2*func(n-1)+1;



}

4.1(迭代)

点击查看代码
#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 s1=1,s2=1;
	int i,j;
	if(n<m)
		return 0;
    if(n==m||m==0)
		return 1;
	if(n>m)
	{
	for(i=n;i>=n-m+1;i--)
		s1*=i;
	for(j=1;j<=m;j++)
		s2*=j;
	return s1/s2;
	}


}

4.2(递归)

点击查看代码
#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==0||m==n)
		return 1;
	if(m>n)
		return 0;
	if(n>m)
		return func(n-1,m)+func(n-1,m-1);
}

点击查看代码
#include <stdio.h>
#include<stdlib.h>
void hanio(int n,char from,char temp,char to); 
void moveplate(int n,char from,char to);
static int k=0;
int main() 
{
	char A,B,C;
	int n;
	while(scanf("%d",&n)!=EOF)
	{
   hanio(n,'A','B','C');
   printf("一共移动了%d次\n",k);
	system("pause");
	}
    return 0;
}
void moveplate(int n,char from,char to)
{
	printf("%d : %c --> %c\n",n,from,to);
	k++;
}
void hanio(int n,char from,char temp,char to)
{
	if(n==1)
		moveplate(n,from,to);
	if(n>1)
	{
	hanio(n-1,from,to,temp);
	moveplate(n,from,to);
	hanio(n-1,temp,from,to);
	}
}

点击查看代码
#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,t=1,k=0;
	while(s!=0)
	{
		i=s%10;
	if(i%2!=0)
	{
		k+=t*i;
		t *= 10;
	}
	s /= 10;
	}
	return k;

}

标签:return,int,long,char,实验,func,include
From: https://www.cnblogs.com/wuxi123/p/18151450

相关文章

  • 实验
    1:在离线的环境中导入镜像在无法访问外网的情况下,通过将docker镜像导出为一个包,然后导入到另外的一台计算机上面,从而实现了不用访问外网就能拉取镜像了#将镜像输出到这个tar包[root@cleint~]#dockersave-ocentos.tarcentos#通过第三方的工具将这个tar包传输到其他机器......
  • 今天又是写前端的一天--web实验二
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>LoginPage</title><......
  • 实验3 C语言函数应用编程
    实验任务11#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN8067voidprint_text(intline,intcol,chartext[]);//函数声明8voidprint_spaces(intn);//函数声明9voidprint......
  • 实验5 循环结构程序设计(while、do-while语句的应用)
    c语言程序设计——实验报告五实验项目名称:实验项目类型:验证性实验日期:一、实验目的二、实验硬、软件环境Windows计算机、Devc6.0三、实验内容及步骤实验内容:编写程序:(1)用while语句计算自然数列1,2,3……,n的和,n的值在程序执行时输入。(2)用do-while语句计算n的阶乘(3)......
  • pta实验总结
    刚开始接触java,感到有些吃力,可能是因为C语言基础不牢的缘故,我的Java水平还有待提高。pta1:7-1设计一个风扇Fan类分数4入门作者殷伟凤单位浙江传媒学院设计一个名为Fan的类表示一个风扇。这个类包括:1.三个名为SlOW、MEDIUM和FAST,其值为1、2和3常量表示风扇的速度。2.一......
  • c语言程序设计——实验报告六
    c语言程序设计——实验报告六实验项目名称:实验项目类型:验证性实验日期:一、实验目的熟练掌握三种循环语句并能正确运用;能够用循环实现一些常用算法,如穷举法,迭代法,递推法等;进一步学习程序调试;了解中国算法,百钱买百鸡。二、实验硬、软件环境Windows计算机、Devc6.0......
  • 实验1-----keep健身运动APP
    一、对比分析墨刀、Axure、Mockplus等原型设计工具的各自的适用领域及优缺点。Axure优点:1.有强大的编辑功能,使得制作素材库会更便捷一点。2.更快的复制粘贴,素材库和原型库会多一些。3.可以项目共享,使得同事间可以同步工作,并保留所有工作历史,并可以随时到处历史版本的项目文......
  • 第八周实验
    与2252309合作结对编程四则混合运算#include<stdio.h>#include<stdlib.h>#include<time.h>intscore=0;intadd(inta,intb){ intc=a+b; if(c<1000)returnc; elsereturn-1;}intsubstract(inta,intb){ intc=a-b; if(c>=0&&......
  • C语言程序设计-实验报告6
    实验项目名称:实验6循环结构程序设计(for语句的应用)实验项目类型:验证性实验日期:2024年4月15日一、实验目的1.熟练掌握三种循环语句并能正确运用;2.能够用循环实现一些常用算法,如穷举法,迭代法,递推法等;3.进一步学习程序调试;4.了解中国算法,百钱买百鸡。二、实验硬、软件环境......
  • C语言程序设计-实验报告5
    实验项目名称:实验5循环结构程序设计(while、do-while语句的应用)实验项目类型:验证性实验日期:2024年4月11日一、实验目的1.熟练掌握三种循环语句并能正确运用;2.能够用循环实现一些常用算法,如穷举法,迭代法,递推法等;3.进一步学习程序调试;4.了解中国算法,百钱买百鸡。二、实验......