首页 > 其他分享 >实验3

实验3

时间:2023-10-30 22:23:58浏览次数:23  
标签:return int long char 实验 func include

一、实验任务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, November~";
 
 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中字符串
}

该程序的目的是在命令行窗口中以随机位置输出字符串"hi, November~",每次输出后暂停1秒。

二、实验任务2

#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! = %lld\n", i, fac(i));
  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>

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

 与我想的一致

static关键字的作用包括限定变量或函数的作用范围、保留变量的值和扩展变量的生命周期。

三、实验任务3

 

#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 func(int n)
{
	if(n==0)
	   return 0;
	else
	   return (func(n-1)+1)*2-1;
}

 

  

 四、实验任务4

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 (n < m)
        return 0; 

    int num= 1; 
    int deno= 1; 

    for (int i = 0; i < m; i++) 
        num *= n - i;

    for (int i = 1; i <= m; i++)
        deno*= i;

    return num/ deno; 
}

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 (n < m)
        return 0;
    if (m == 0 || n == m)
        return 1;

    return func(n - 1, m - 1) + func(n - 1, m);
}

 五、实验任务5

#include <stdio.h>
#include <stdlib.h>

int hanoi(int n, char x, char y, char z, int *counter) 
{
    int move(char, int, char);
    if (n == 1) 
	{
        move(x, 1, z);
        (*counter)++;
    } 
	else 
	{
        hanoi(n - 1, x, z, y, counter);
        move(x, n, z);
        (*counter)++;
        hanoi(n - 1, y, x, z, counter);
    }
    return 0;
}

int move(char get, int n, char put)
{
    static int k = 1;
    printf("%d: %c --> %c\n", n, get, put);
    k++;
    return 0;
}

int main()
{
    system("color CF");
    int hanoi(int, char, char, char, int *);
    int n, counter;
    while (scanf("%d", &n) != EOF)
	{
        counter = 0;
        hanoi(n, 'A', 'B', 'C', &counter);
        printf("\n一共移动了%d次\n\n", counter);
    }
    return 0;
}

 六、实验任务6

 

#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) {
    long result = 0; 
    long multiplier = 1; 

    while (s > 0) {
        long digit = s % 10; 
        if (digit % 2 != 0) {
            result = result + digit * multiplier; 
            multiplier *= 10; 
        }
        s /= 10; 
    }

    return result;
}

 

 

 六、实验任务6

#include <stdio.h>
#include <stdlib.h>
int fuck(long n ,long m);
int main(void)
{
	for (int i=0;;i++){	
		int a=i*i;
		int b=i*i*i;
	    if(fuck(a,b)==1){
	    	printf("%d",i);
	    	break;
	    }
    }
	return 0;
	
} 
int fuck(long n,long m){
	
	long temp = n;
    int Di= 0;
    while (temp > 0) {
        temp /= 10;
        Di++;
    }
   int a[Di];
    for (int i = Di - 1; i >= 0; i--) {
        a[i] = n% 10;
        n/= 10;
    }
    
	long temp2 = m;
    int Di2= 0;
    while (temp2 > 0) {
        temp2 /= 10;
        Di2++;
    }
    int b[Di2];
    for (int i = Di2 - 1; i >= 0; i--) {
        b[i] = m% 10;
        m/= 10;
    }
    
    int v= Di+Di2;
    int c[v];
    int i, j, k;
    i = j = k = 0;

    while (i < Di){
        c[k++] = a[i++];
    }
    while (j<Di2){
    	c[k++] = b[j++];
	}
	
	int cao[10]={0};
    for(int i=0;i<v;i++){
    	cao[c[i]]++;
	}
	for(int i=0;i<=9;i++)
	{
		if(cao[i]!=1)
		    return 0;
	}
	return 1;
}

 

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

相关文章

  • 实验3 C语言函数应用编程
    实验任务11#include<stdio.h>2#include<math.h>3#include<stdlib.h>4#include<time.h>5#include<windows.h>6#defineN807voidprint_text(intline,intcol,chartext[]);8voidprint_spaces(intn);9voidpr......
  • 实验三
    实验三任务一代码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN806voidprint_text(intline,intcol,chartext[]);7voidprint_spaces(intn);8voidprint_blank_lines(intn);9......
  • 实验三
    实验1每隔100ms在随机显示字符串 实验2 一致 实验3    实验4   实验5   实验六    实验七  #include<stdio.h>intfunc(intn2,intn3);intmain(){ intn=0; intflag=0; while(1){ intn2,n3; n2=n*n;......
  • 实验三 计算机九班周天意202383290419
    一、实验目的1.能正确使用c语法规则定义、声明、调用函数2.能正确编写递归函数3.针对具体问题场景,能合理抽象出独立的功能模块,正确定义函数并使用,使得代码更具可读性、可维护性4.针对具体问题场景,能正确、合理使用全局变量和局部static变量,解决实际问题二、实验准备实验前......
  • 实验3
    task11#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN806voidprint_text(intline,intcol,chartext[]);//函数声明7voidprint_spaces(intn);//函数声明8voidprint_blank_l......
  • 实验3 C语言函数应用编程
    一、实验目的能正确使用c语法规则定义、声明、调用函数能正确编写递归函数针对具体问题场景,能合理抽象出独立的功能模块,正确定义函数并使用,使得代码更具可读性、可维护性针对具体问题场景,能正确、合理使用全局变量和局部static变量,解决实际问题二、实验准备函数定义、声......
  • 实验三
    #include<stdio.h>inti=0;voidhanoi(unsignedintn,charfrom,chartemp,charto);voidmoveplate(unsignedintn,charfrom,charto);intmain(){unsignedintn;while(scanf("%u",&n)!=EOF){i=0;......
  • 实验3_C语言函数应用编程
    1.task_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_blank_lines(intn)......
  • 实验3
    实验1代码1#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_blank_lines(intn)......
  • 实验三
    实验一结果,会在随机的某一行(<25)某一列出现输出实验二结果:实现阶乘,static保证每一次循环中p的值会随之而改变,相当于我们只初始化一次p这个变量、实验三#include<stdio.h>#include<stdio.h>longlongfunc(intn);//函数声明intmain(){intn;longlongf;......