数量成对交换的第三大和最小数字

时间:2022-10-11 19:59:15

This is the code for finding 3rd largest and 3rd smallest number.

这是查找第3个和第3个最小数字的代码。

t=no of test cases

t =没有测试用例

num=user input number

num =用户输入号码

if num is a single digit number it will print impossible.

如果num是一位数字,则打印不可能。

if the num is 123 then it should store 123,132,213,231,312,321.

如果num是123那么它应该存储123,132,213,231,312,321。

Of these the 3rd from the front is 213 and from back is 231.

其中前面的第3个是213,后面是231。

The problem of my code is that when i input

我的代码问题是我输入的时候

123 it gives random number with 112,211,133,311...etc

123它给出随机数112,211,133,311 ......等

which i don't want.

这是我不想要的。

I want a 3 digit number containing 1,2 and 3.

我想要一个包含1,2和3的3位数字。

if the num is 1234 it should have 4321,2134,3124...Not 1143,2211.

如果数字是1234,它应该有4321,2134,3124 ...不是1143,2211。

The problem method is solve(int num). In this method i have converted integer num to string num and then stored it in the string treeset which i will again convert it into integer treeset so to find 3rd largest and 3rd smallest number.

问题方法是solve(int num)。在这个方法中,我已经将整数num转换为字符串num,然后将其存储在字符串树集中,我将再次将其转换为整数树集,以便找到第3个最大和第3个最小数字。

iam not sure about primefact method(it gives factorial{length of a number ) whether it has use or not...

我不确定primefact方法(它给出因子{数字的长度)是否有用...

public class ThirdSmallestLargest {
public static void main(String[] args) {
    Scanner in=new Scanner(System.in);
    ThirdSmallestLargest tsl=new ThirdSmallestLargest();
    int t,num;
    t=in.nextInt();
    while(t!=0){
        num=in.nextInt();
        tsl.match(num);
        t--;
    }
}
//---------------------Method for finding zero in a number
private void match(int num) {
    int length=(int) (Math.log10(num)+ 1);
    if(length==1){
        System.out.println("Not possible");
    }
    else{
           String s = String.valueOf(num);
           if (s.indexOf('0')<0){
               solve(num);
           }  
    }
}  
//---------------------method for distinct jumble number
 //NOT DONE CONVERT STRING TREESET TO INTEGER TREESET
private void solve(int num) {
        int length=(int) (Math.log10(num)+ 1);
       TreeSet <String> tsstr=new TreeSet<>();
       Iterator<String> itr=tsstr.iterator();
       //Integer.toString(num);
       char[] chars = Integer.toString(num).toCharArray();
      StringBuilder sb = new StringBuilder(); // doesnt work with 666,7979 types of number
       Random random =  new Random();  
       while (tsstr.size() <fact(num)){
       for (int i = 0; i < length; i++) {                                 
       char c = chars[random.nextInt(chars.length)];
           sb.append(c);

        }           
        String output = sb.toString();
        sb.setLength(0);//set the length of the char

       // String input=sb.toString();
        //System.out.println(output);
       // if(output.contentEquals("1"))
      // tsstr.add(Integer.toString(num));

        tsstr.add(output);   
    }
       System.out.println(tsstr);


}

private int fact(int num) {
    int length=(int) (Math.log10(num)+ 1);
    int i,fact=1;
    for(i=1;i<=length;i++){
       fact=fact*i;
    }
    return fact;
 }
}

2 个解决方案

#1


2  

First, sort the digits.

首先,对数字进行排序。

Let's say input is 111222333444 after sorting. That is the lowest number.

假设排序后输入为111222333444。这是最低的数字。

Next number is found by swapping last 3 with first 4, i.e. 111222334344. Said another way, you move the last second-highest digit one position to the right. That is the second-lowest number.

通过将前3个与前4个交换,即111222334344,找到下一个数字。换句话说,将最后一个最高位数向右移动一个位置。这是第二低的数字。

Now there are two cases:

现在有两种情况:

  • If there were more than one of the highest digit (4), just to it again, i.e. move the last 3 one more to the right: 111222334434.
  • 如果有一个以上的最高位数(4),只需再次,即将最后一个向右移动一个:111222334434。

  • If there was only one 4, the 3 is at end and can't move to the right, e.g. 11122233341112223343, so we move it back, then move the last third-highest digit (2) one position to the right: 111222334311122233341112232334
  • 如果只有一个4,则3在结尾并且不能向右移动,例如1112223334→1112223343,所以我们将其移回,然后向右移动最后一个第三高位(2)一个位置:1112223343→1112223334→1112232334

You've now found the third lowest number. To recap:
111222333444111222334344111222334434
111222333411122233431112232334

你现在找到了第三低的数字。总结:111222333444→111222334344→111222334434 1112223334→1112223343→1112232334

You can skip the middle step. If there is more than one 4, move last 3 two positions right. Otherwise, move last 2 one position right.

你可以跳过中间步骤。如果有多个4,则向右移动最后3个两个位置。否则,向右移动最后2个位置。

For third-highest number you do the same, except reversed:
444333222111444333221121 (last 2 moves two positions right)
44433322214443323221 (last 3 moves one position right)

对于第三高的数字,你做同样的,除了逆转:444333222111→444333221121(最后2个向右移动两个位置)4443332221→4443323221(最后3个向右移动一个位置)

I'll leave it to you to write the code for it.

我会留给你写代码。

#2


1  

Some suggestions to get you going:

一些建议,让你去:

  1. Separate your code into the different "functions" you need, like: creating all permutation of all possible combinations of your numbers
  2. 将您的代码分成您需要的不同“功能”,例如:创建所有可能的数字组合的所有排列

  3. Getting them sorted
  4. 让他们排序

In other words: first create a list of all possible numbers; then simply use one of the many ways of getting that list sorted; and then you just decided which "indexes" in that sorted lists are of interst to you.

换句话说:首先创建所有可能数字的列表;然后简单地使用多种方法之一来获取该列表;然后你就决定了那些排序列表中的哪些“索引”对你来说是合适的。

And: choose better abstractions respectively: don't move "jump" from one level to another. Stick with them.

并且:分别选择更好的抽象:不要将“跳跃”从一个级别移动到另一个级别。坚持下去。

Meaning: initially, consider your user input to be a string, not a number (of course, you then have to check that this string only contains digits).

含义:最初,将您的用户输入视为字符串,而不是数字(当然,您必须检查此字符串是否仅包含数字)。

You see, if you have a string, you can directly ask for its length; no need to call log() to figure the numbers of chars in a string. Then, you can easily fetch the individual digits from your string - and you get an array of characters. Now you can easily create permutations of those characters; building strings. Finally, you turn all strings into ints for sorting.

你看,如果你有一个字符串,你可以直接询问它的长度;无需调用log()来计算字符串中的字符数。然后,您可以轻松地从字符串中获取单个数字 - 并获得一个字符数组。现在,您可以轻松创建这些字符的排列;建立字符串。最后,将所有字符串转换为整数以进行排序。

But the point is: all of the above steps should be their own distinct functions. Built small methods (which all of them are perfect for being tested on their own) ... and then compose your solution out of that.

但问题是:以上所有步骤都应该是他们自己独特的功能。构建小方法(所有这些方法都非常适合自己进行测试)......然后从中构建解决方案。

#1


2  

First, sort the digits.

首先,对数字进行排序。

Let's say input is 111222333444 after sorting. That is the lowest number.

假设排序后输入为111222333444。这是最低的数字。

Next number is found by swapping last 3 with first 4, i.e. 111222334344. Said another way, you move the last second-highest digit one position to the right. That is the second-lowest number.

通过将前3个与前4个交换,即111222334344,找到下一个数字。换句话说,将最后一个最高位数向右移动一个位置。这是第二低的数字。

Now there are two cases:

现在有两种情况:

  • If there were more than one of the highest digit (4), just to it again, i.e. move the last 3 one more to the right: 111222334434.
  • 如果有一个以上的最高位数(4),只需再次,即将最后一个向右移动一个:111222334434。

  • If there was only one 4, the 3 is at end and can't move to the right, e.g. 11122233341112223343, so we move it back, then move the last third-highest digit (2) one position to the right: 111222334311122233341112232334
  • 如果只有一个4,则3在结尾并且不能向右移动,例如1112223334→1112223343,所以我们将其移回,然后向右移动最后一个第三高位(2)一个位置:1112223343→1112223334→1112232334

You've now found the third lowest number. To recap:
111222333444111222334344111222334434
111222333411122233431112232334

你现在找到了第三低的数字。总结:111222333444→111222334344→111222334434 1112223334→1112223343→1112232334

You can skip the middle step. If there is more than one 4, move last 3 two positions right. Otherwise, move last 2 one position right.

你可以跳过中间步骤。如果有多个4,则向右移动最后3个两个位置。否则,向右移动最后2个位置。

For third-highest number you do the same, except reversed:
444333222111444333221121 (last 2 moves two positions right)
44433322214443323221 (last 3 moves one position right)

对于第三高的数字,你做同样的,除了逆转:444333222111→444333221121(最后2个向右移动两个位置)4443332221→4443323221(最后3个向右移动一个位置)

I'll leave it to you to write the code for it.

我会留给你写代码。

#2


1  

Some suggestions to get you going:

一些建议,让你去:

  1. Separate your code into the different "functions" you need, like: creating all permutation of all possible combinations of your numbers
  2. 将您的代码分成您需要的不同“功能”,例如:创建所有可能的数字组合的所有排列

  3. Getting them sorted
  4. 让他们排序

In other words: first create a list of all possible numbers; then simply use one of the many ways of getting that list sorted; and then you just decided which "indexes" in that sorted lists are of interst to you.

换句话说:首先创建所有可能数字的列表;然后简单地使用多种方法之一来获取该列表;然后你就决定了那些排序列表中的哪些“索引”对你来说是合适的。

And: choose better abstractions respectively: don't move "jump" from one level to another. Stick with them.

并且:分别选择更好的抽象:不要将“跳跃”从一个级别移动到另一个级别。坚持下去。

Meaning: initially, consider your user input to be a string, not a number (of course, you then have to check that this string only contains digits).

含义:最初,将您的用户输入视为字符串,而不是数字(当然,您必须检查此字符串是否仅包含数字)。

You see, if you have a string, you can directly ask for its length; no need to call log() to figure the numbers of chars in a string. Then, you can easily fetch the individual digits from your string - and you get an array of characters. Now you can easily create permutations of those characters; building strings. Finally, you turn all strings into ints for sorting.

你看,如果你有一个字符串,你可以直接询问它的长度;无需调用log()来计算字符串中的字符数。然后,您可以轻松地从字符串中获取单个数字 - 并获得一个字符数组。现在,您可以轻松创建这些字符的排列;建立字符串。最后,将所有字符串转换为整数以进行排序。

But the point is: all of the above steps should be their own distinct functions. Built small methods (which all of them are perfect for being tested on their own) ... and then compose your solution out of that.

但问题是:以上所有步骤都应该是他们自己独特的功能。构建小方法(所有这些方法都非常适合自己进行测试)......然后从中构建解决方案。