如何在java中创建具有正有理数的calkin-wilf树? [英] How can I make a calkin-wilf tree of positive rational numbers in java?

查看:108
本文介绍了如何在java中创建具有正有理数的calkin-wilf树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想列举1到输入的有理数。

序列应如下所示:

没有重复:

1/1 ,1 / 2,2 / 1,1 / 3,3 / 2,2 / 3,3 / 1,1 / 4,4 / 3,3 / 5,5 / 2,2 / 5,5 / 3,3 / 4,4 / 1,1 / 5,···

输入:

10

输出:

1 - > 1/1

2 - > 1/2

3 - > 2/1

4 - > 1/3

5 - > 3/2

6 - > 2/3

7-- > 3/1

8 - > 1/4

9 - > 4/3

10 - > 3/5

我试了差不多7天但没有成功



我尝试了什么: < br $>


I want to enumerate rational numbers from 1 to input.
Sequence should look like this:
without repitition:
1/1,1/2,2/1,1/3,3/2,2/3,3/1,1/4,4/3,3/5,5/2,2/5,5/3,3/4,4/1,1/5, · · ·
Input:
10
Output:
1-->1/1
2-->1/2
3-->2/1
4-->1/3
5-->3/2
6-->2/3
7-->3/1
8-->1/4
9-->4/3
10-->3/5
I tried almost 7 days but didn't succeed

What I have tried:

public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int k=1;
int j=2;
int temp=2, temp1=1, temp2=2;
System.out.print("Enter Domain(1-N):")
int n=sc.nextInt();
for(int i=1; i<=n; i++)
{
					//Nominator Logic
					if(i==1)
					{
						System.out.println(i+" ----> "+1);
					}
					else
					{
						System.out.println(i+" ----> "+j);
						if(j>1)
						{
							j--;
						}
						else
						{
							j=temp+1;
							temp=temp+1;
						}
					}
					//end Nominator Logic
					
					//Denominator Logic
					if(i==1)
					{
						System.out.println(i+" ----> "+1);
					}
					else if(i<19 && i!=5 && i!=11)
					{
						System.out.println(i+" ----> "+temp1);
						temp1=temp1+1;
						temp2=temp1-1;
					}
					else if(i==5 || i==11)
					{
						temp1=temp1-temp2;
						System.out.println(i+" ----> "+temp1);
					}
					
					
				}
				
}
}

推荐答案

据我所知,你的算法是错误的,只有一个完整的重写可以解决你的问题,没有一点点修复使它工作。

首先研究树,它是如何构建的: Calkin-Wilf树 - 维基百科 [ ^ ]

仅限节点的值依赖于父模式直到根,它不依赖于左边或右边的节点。

所以你的任务是从位置编号中找到树中节点的位置。然后找到根目录的路径,从路径中,你将推断它的值。

建议只是因为我们不做你的HomeWork。

-----

建议:学会正确缩进代码,显示其结构,有助于阅读和理解。它还有助于发现结构错误。

可以看到代码末尾有一个额外的'}'。

As far as I can see, your algorithm is just wrong and only a complete rewrite can solve your problem, there is no little fix to make it work.
Firstly study the tree, how it is build: Calkin–Wilf tree - Wikipedia[^]
The value of a node only depend on parent modes until the root, it does not depend on nodes on left or right.
So your task is to find the position of a node in tree from its position number. Then find the path to the root, From the path, you will deduce it value.
Advices only because we do not do your HomeWork.
-----
Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
One can see that there is an extra '}' at the end of code.
public static void main(String[] args){
  Scanner sc=new Scanner(System.in);
  int k=1;
  int j=2;
  int temp=2, temp1=1, temp2=2;
  System.out.print("Enter Domain(1-N):")
  int n=sc.nextInt();
  for(int i=1; i<=n; i++)
  {
    //Nominator Logic
    if(i==1)
    {
      System.out.println(i+" ----> "+1);
    }
    else
    {
      System.out.println(i+" ----> "+j);
      if(j>1)
      {
        j--;
      }
      else
      {
        j=temp+1;
        temp=temp+1;
      }
    }
    //end Nominator Logic

    //Denominator Logic
    if(i==1)
    {
      System.out.println(i+" ----> "+1);
    }
    else if(i<19 && i!=5 && i!=11)
    {
      System.out.println(i+" ----> "+temp1);
      temp1=temp1+1;
      temp2=temp1-1;
    }
    else if(i==5 || i==11)
    {
      temp1=temp1-temp2;
      System.out.println(i+" ----> "+temp1);
    }
  }
}
} // extra } here



专业程序员的编辑器具有此功能以及其他功能,例如括号匹配和语法高亮。

Notepad++主页 [ ^ ]

ultraedit [ ^ ]


这篇关于如何在java中创建具有正有理数的calkin-wilf树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆