首页 > 其他分享 >实验3

实验3

时间:2022-11-08 21:56:42浏览次数:35  
标签:return int long 实验 func printf include

1.

#include<stdio.h>
long fun(long s);
int main(){
long s,t;
printf("enter a number:");
while(scanf_s("%ld",&s)!=EOF){
t=fun(s);
printf("new number is:%ld\n\n",t);
printf("enter a number:");
}
return 0;
}
long fun(long s)
{
int n=1,m,t=0;
while(s>0)
{
m=s%10;
if(m%2!=0)
{
t=t+n*m;
n=n*10;
}
s=s/10;
}
return t;
}

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;

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

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) {

    if (n == m)

        return 1;

    else if (m == 1)

        return n;

    else if (m == 0)

        return 1;

    else if (n < m)

        return 0;

    else

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

 

 

}

5. #include <stdio.h>

#include<stdlib.h>

int mul(int n, int m);

int main() {

    int n, m;

    while (scanf_s("%d%d", &n, &m) != EOF)

        printf("%d * %d = %d\n", n, m, mul(n, m));

    return 0;

}

 

int mul(int n, int m) {

    if (n == 0 || m == 0)

        return 0;

    else if (n == 1)

        return m;

    else

        return mul(n - 1, m) + m;

}

6. #include<stdio.h>

int a = 0;void hanoi(int i, char one, char two, char three);

int main() {int n;

 

    printf("请输入盘子数:");

    while(scanf_s("%d", &n)!=EOF)

        {

    hanoi(n, 'A', 'B', 'C');

 

    printf("共需要%d步\n", a);

    printf("请输入盘子数:");

        }

    return 0;

}

void hanoi(int i, char one, char two, char three) {

    void move(int x, int y);

 

    if (i == 1) {

        move(one, three);

    }

    else {

        hanoi(i - 1, one, three, two);

        move(one, three);

        hanoi(i - 1, two, one, three);

    }

}

void move(int x, int y) {

    a++;

    printf("%c-->%c\n", x, y);

 

}

7. #include<stdio.h>

#include<stdlib.h>

int isprime(int n);

int main() {

    int i, n;

    for (i = 2; i <= 20; i = i + 2)

    {

        for (n = 2; n <=i/2; n++)

        {

            if (isprime(n) && isprime(i - n))

            {

                printf("%d=%d+%d\n", i, n, i - n);

                break;

            }

            }

    }

    return 0;

}

int isprime(int n) {

    int i = 2;

    if (n < 2)

        return 0;

    for (i; i <=n/2; i++)

    {

        if (n % i == 0)

            return 0;

    }

}

8. #include<stdio.h>

long fun(long s);

int main(){

    long s,t;

    printf("enter a number:");

    while(scanf_s("%ld",&s)!=EOF){

        t=fun(s);

        printf("new number is:%ld\n\n",t);

        printf("enter a number:");

    }

    return 0;

}

long fun(long s)

{

int n=1,m,t=0;

while(s>0)

{

    m=s%10;

    if(m%2!=0)

    {

        t=t+n*m;

        n=n*10;

    }

    s=s/10;

}

return t;

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

标签:return,int,long,实验,func,printf,include
From: https://www.cnblogs.com/wcf8/p/16871350.html

相关文章

  • 实验环境安装配置
    ......
  • 实验2:Open vSwitch虚拟交换机实践
    实验2:OpenvSwitch虚拟交换机实践一、实验目的1.能够对OpenvSwitch进行基本操作;2.能够通过命令行终端使用OVS命令操作OpenvSwitch交换机,管理流表;3.能够通过Mininet的......
  • 实验4
    任务五#include<iostream>#include"vectorInt.hpp"voidtest(){usingnamespacestd;intn;cin>>n;vectorIntx1(n);for(autoi=0;i<n;i......
  • 实验6:开源控制器实践——RYU
    实验6:开源控制器实践——RYU一、实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。二、实验......
  • 实验4:开源控制器实践——OpenDaylight
    实验4:开源控制器实践——OpenDaylight一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境......
  • 实验4 类与数组、指针
    实验任务5:#include<iostream>#include"vectorInt.h"usingnamespacestd;voidtest(){usingnamespacestd;intn;cin>>n;vectorIntx1(n);......
  • 实验四 类与数组、指针
    实验五:task5.cpp#include<iostream>#include"vectorInt.hpp"voidtest(){usingnamespacestd;intn;cin>>n;vectorIntx1(n);for(autoi......
  • 实验一———个人项目:任务2
    一、回顾你过去将近三年的经历1、当初报考的时候,是真正喜欢计算机这个专业吗?我本人从初中开始就很喜欢计算机专业,一是我姐就是计算机专业的,二是我觉得敲代码真的很帅,以至......
  • 实验3
     #include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明......
  • 实验4 类与数组、指针
    实验五vectorint.h#pragmaonce#include<iostream>usingnamespacestd;classvectorInt{public: ints,*p; vectorInt(intm) { p=newint[m]; s......