关于n和输出hello的个数和world个数的关系

public static void MYSTERY(int n) {
        if (n==0)
            System.out.println("hello");

        if(n==1)
            System.out.println("world");

        if(n>=2){
            MYSTERY(n-2);
            MYSTERY(n-2);
            System.out.println("hello");
            System.out.println("world");

        }
    }

关于n和输出hello的个数和world个数的关系

迭代函数,因为区分了n==0和n==1,所以次数需要根据n的奇偶来区分,其差别就是最后一次输出的是hello还是world
if(n>=2)这里,循环2次迭代,而且参数每次都-2,所以两次迭代的总次数就是 2的(n/2)-1次方(因为最后一次执行的是n==0或者n==1,所以需要减去1),语句中的打印语句也就执行2的(n/2)次方-1。而if(n==0)和if(n==1)根据n的奇偶性决定执行那一条,但是总次数都是2的(n/2)次方。注意,这里的n/2取整,比如,n=5,n/2的结果是2。当n=0或者n=1时,单独输出hello或者world,其总次数是2的(n/2).
所以:
当n为偶数时:每次迭代最后的执行语句为if(n==0),所以hello的次数需要 +2的(n/2)次方
hello的总次数是:2的(n/2)次方+2的(n/2)次方-1
world的总次数是:2的(n/2)次方-1
当n为奇数时:每次迭代最后的执行语句为if(n==1),所以world的次数需要 +2的(n/2)次方
hello的总次数是:2的(n/2)次方-1
world的总次数是:2的(n/2)次方+2的(n/2)次方-1

img

9行,10行重复了,只需要一个吧

img


int h, w;
void MYSTERY(int n) {
    if (n == 0)
    { 
        //printf("hello ");
        h++;
    }
    if (n == 1)
    {
        //printf("world ");
        w++;
    }
    if (n >= 2) {
        MYSTERY(n - 2);
        MYSTERY(n - 2);
        //printf("hello1 ");
        h++;
        //printf("world1 ");
        w++;

    }
}

int main()
{
    int num = 0;
    h = 0;
    w = 0;
    for (num = 0; num <= 20; num++)
    {
        printf("%d",num);
        MYSTERY(num);
        printf(" h=%d,w=%d", h,w);
        printf("\n");
        h = 0;
        w = 0;
    }
}

就是前面一项乘以2+1

测试代码C

#include<stdio.h>
int h,w;
void MYSTERY(int n) {
    if (n==0) {
//        printf("hello");
        printf("0;");
        h++;
    }
    if(n==1) {
//        printf("world");
        printf("1;");
        w++;
    }
    if(n>=2) {
        printf("step1(%d)->",n-2);
        MYSTERY(n-2);
        printf("\n");
        printf("step2(%d)->",n-2);
        MYSTERY(n-2);
//        printf("hello");
        printf("h++;");
        h++;
//        printf("world");
        printf("w++;");
        w++;
    }
}

int main() {
    int n;
    for(n=0; n<12; n++) {
        h=0;
        w=0;
        MYSTERY(n);
        printf("\nn=%d,hello=%d,world=%d\n\n",n,h,w);
    }
    return 0;
}

设int num=2^(n/2);
设int h为hello的个数
设int w为world的个数

当n为偶数时,n和“hello”及“world”的关系是:
输入n, 输出的h个数为: num×h+(num-1)×h
输出的w个数为: (num-1)×w
当n为奇数时,n和“hello”及“world”的关系是:
输入n, 输出的h个数为: (num-1)×h
输出的w个数为: num×w+(num-1)×w

推理原理,我这边上传了一张自己写的推理图,供参考理解。祝事业家庭幸福美满,望博主采纳!!

img

import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个数字n:");
        int n = sc.nextInt();
        Demo03.MYSTERY(n);
    }

    public static void MYSTERY(int n) {
        if (n == 0)
            System.out.println("hello");

        if (n == 1)
            System.out.println("world");

        if (n >= 2) {
            MYSTERY(n - 2);
            MYSTERY(n - 2);
            System.out.println("hello");
            System.out.println("world");

        }
    }
}


就一个简单的小递归

其实我还是没有搞懂你的问题是什么?或者你要不给代码块加上{}会不会更容易看明白一点

//n=0时:hello,n=1时:world
//n=2时:hello \n hello \n world n=3时:word \n hello \n world 
//n>=4时:hello \n world
//\n:换行
public static void MYSTERY(int n) {
        if (n==0)
            System.out.println("hello");
 
        if(n==1)
            System.out.println("world");
 
        if(n>=2){
            MYSTERY(n-2);
            MYSTERY(n-2);
            System.out.println("hello");
            System.out.println("world");
 
        }
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632