首页 > 编程语言 >实验3 函数应用编程

实验3 函数应用编程

时间:2023-04-05 19:44:39浏览次数:49  
标签:return 函数 int 编程 long 实验 func ans include

1.实验任务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, 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); 
}

这个程序实现的功能是随机在某行某列打印出所给字符

2.实验任务2

#include <stdio.h>
long long fac(int n); 

int main() {
    int i, n;

    printf("Enter n: ");
    scanf_s("%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;

    p = p * n;
    printf("p = %lld\n", p);
    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;
}

 

 3.实验任务3

#include <stdio.h>
long long func(int n);

int main() {
    int n;
    long long f;

    while (scanf_s("%d", &n) != EOF) {
        f = func(n);
        printf("n = %d, f = %lld\n", n, f);
    }

    return 0;
}
long long func(int n)
{
    long long int sum;
    if (n == 1)
        sum = 1;
    else
        sum = (func(n - 1) + 1) * 2 - 1;
    return sum;
}

 

 4.实验任务4

#include <stdio.h>
int func(int n, int m);

int main() {
    int n, m;

    while (scanf_s("%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 x;
    x = 0;
    if (n >= m)
    {
        if (m > 1)
            x = func(n - 1, m - 1) + func(n - 1, m);
        else
        {
            if (m == 0)
                x = 1;
            else
                x = n;
        }
    }
    else
        x = 0;
    return x;
}

 

 5.实验任务5

#include <stdio.h>
double mypow(int x, int y);

int main() {
    int x, y;
    double ans;

    while (scanf_s("%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 t;
    t = 1;
    double ans;
    ans = 1;
    if(y>=0)
    {
        while(t<=y)
        {
            ans = ans * x;
            t = t + 1;
        }
    }
    else
    {
        while(t<=-y)
        {
            ans = ans / x;
            t = t + 1;
        }
    }
    return ans;
}
#include <stdio.h>
double mypow(int x, int y);

int main() {
    int x, y;
    double ans;

    while (scanf_s("%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;
    if (y == 0)
        ans = 1;
    if (y > 0)
        ans = x * mypow(x, y - 1);
    if (y < 0)
        ans = 1 / mypow(x, -y);
    return ans;
}

 

 

 

 6.实验任务6

#include<stdio.h>
int Hanoita(int n, char A, char B, char C);
int cishu(int n);
int main()
{
    double sum;
    int n;
    while (scanf_s("%d", &n) != EOF) 
    {
        Hanoita(n, 'A', 'B', 'C');
        sum = cishu(n);
        printf("一共移动了%.0f次。\n", sum);
    }
    return 0;
}
int Hanoita(int n, char A, char B, char C)
{
    if (1 == n)
        printf("%d:%c->%c\n",n, A, C);
    else
    {
        Hanoita(n - 1, A, C, B);
        printf("%d:%c->%c\n", n, A, C);
        Hanoita(n - 1, B, A, C);
    }
    return 0;
}
int cishu(int n)
{
    double sum;
    int t;
    sum = 1;
    t = 1;
    while (t <= n) 
    {
        sum = sum * 2;
        t = t + 1;
    }
    sum = sum - 1;
    return sum;
}

 

 7.实验任务7

#include<stdio.h>
int is_prime(int n);

int main()
{
    int n;
    n = 4;
    while (n < 21)
    {
        int i, a1, a2;
        a1 = 0;
        a2 = 0;
        i = n - 1;
        while (a1 * a2 == 0)
        {
            i = i - 1;
            a1 = 0;
            a2 = 0;
            a1 = is_prime(i);
            a2 = is_prime(n - i);
        }
        printf("%d = %d + %d\n", n, i, n - i);
        n = n + 2;
    }

    return 0;
}
int is_prime(int n)
{
    int t, a;
    t = 2;
    a = 1;
    while(t<n)
    {
        if (n % t == 0)
            a = 0;
        t = t + 1;
    }
    return a;
}

 

 8.实验任务8

#include <stdio.h>
#include <math.h>
long func(long s);

int main() {

    long s, t;

    printf("Enter a number: ");
    while (scanf_s("%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 t, a;
    t = 0;
    a = 0;
    while(s>0)
    {
        a = s % 10;
        if (a % 2 == 1)
            t = 10 * t + a;
        s = s / 10;
    }
    long x, y;
    x = 0;
    y = 0;
    while(t>0)
    {
        x = t % 10;
        y = y * 10 + x;
        t = t / 10;
    }
    return y;
}

 

标签:return,函数,int,编程,long,实验,func,ans,include
From: https://www.cnblogs.com/lwh442/p/17290691.html

相关文章

  • 实验三
    实验任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_spaces(intn);voidprint_blank_lines(intn);intmain(){intline,col......
  • 实验三
    #include<stdio.h>#include<stdlib.h>#include<time.h>#include<Windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_spaces(intn);voidprint_blank_lines(intn);intmain(){ intline,col,i; chartex......
  • C 库函数
    (一)malloc()说明:C库函数 void*malloc(size_tsize) 分配所需的内存空间,并返回一个指向它的指针。原型:void*malloc(size_tsize)//内存块的大小,以字节为单位。//该函数返回一个指针,指向已分配大小的内存。如果请求失败,则返回NULL。例子:var=shared_ptr_new(malloc......
  • 实验三
    task1#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_spaces(intn);voidprint_blank_lines(intn);intmain(){ intline,col,i; charte......
  • 实验三
    任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_spaces(intn);voidprint_blank_lines(intn);intmain(){......
  • C++函数库——全排列
    全排列,顾名思义,对一个无序数组或者有序数组写出其对应的所有组合,实则为从当前数组顺序开始,排列出所有比当前序列大(默认)或者小的所有组合,所以如果初始为无序数组,则得到的结果并非所有组合1.next_permutation,获取下一个排列结果,及获取比当前序列小的下一个序列1#include<iost......
  • 经控制平面传递寄存器信息实验
    经控制平面传递寄存器信息实验实验目的为了在tofino上实现微突发缓解所需的微突发检测,需要在ingress阶段获取出端口的队列信息。但由于硬件限制,ingress阶段不能直接访问在egress阶段获取的队列长度信息,因此需要一种方法,将每个端口的队列长度是否超过阈值的信息,传递给ingresspip......
  • 实验三
    #include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);//函数声明voidprint_blank_lines(intn);//函数声明intmain(){int......
  • 第四十四篇 vue - 进阶主题 - 渲染函数 & JSX
    渲染函数&JSX在绝大多数情况下,Vue推荐使用模板语法来创建应用。然而在某些使用场景下,我们真的需要用到JavaScript完全的编程能力。这时渲染函数就派上用场了基本用法1、创建VnodesVue提供了一个h()函数用于创建vnodesimport{h}from'vue'constvnode=h(......
  • CUDA编程入门
    一、CUDA编程模型 1.CUDA程序执行流程  2.CUDA核函数kernel  3.CUDA程序层次结构kernel/Grid——>Block——>WARP——>Thread 上图中一个Kernel/Grid包含2×3个Block,一个Block包含3×5个线程     4.CUDA内置变量      二、向......