有多少个基数 b,使得数字的基数 b 表示以 1 开头?

huangapple go评论80阅读模式
英文:

How many bases b are there such that the base b representation of number starts with a 1?

问题

以下是翻译好的内容:

问题陈述:

问题陈述-: Altaf最近学习了有关数字进制,并对此产生了浓厚的兴趣。

Altaf了解到,对于大于十的进制,需要引入新的数字符号,并且惯例是使用英文字母表的前几个字母。例如,在16进制中,数字为0123456789ABCDEF。Altaf认为这是不可持续的;英文字母表只有26个字母,所以这种方案只能在基数36之前工作。但这对于Altaf来说不是问题,因为Altaf非常有创造力,可以在需要时发明新的数字符号。(Altaf非常有创造力。)

Altaf还注意到,在二进制中,所有正整数都以数字1开头!然而,这是唯一成立的进制。因此,Altaf自然会想:给定某个整数N,有多少个进制b满足N的b进制表示以1开头?

输入格式:
输入的第一行包含一个整数T,表示测试用例的数量。接下来是T个测试用例的描述。
每个测试用例包括一行,其中包含一个十进制整数N。

输出格式:
对于每个测试用例,输出一行,其中包含进制数b的数量,如果有无限多个进制,则输出INFINITY。

约束:
1 <= T <= 10^5
0 <= N < 10^12

示例输入:

4

6

9

11

24

示例输出:

4

7

8

14

解释:

在第一个测试用例中,6在2、4、5和6进制中的开头都是1:610 = 1102 = 124 = 115 = 106。

我在尝试使用Java实现这个,但在某一点上,我的循环不起作用,它只会采用第一个值,然后退出循环!谢谢

我的代码:

import java.util.*;

public class MyClass {
    public static void main(String args[]) {
      
        Scanner sc = new Scanner(System.in);
        long n,i,j,k,m;
        long count=0,rem1,index;
        long rem[];
        rem = new long[(int)100];
        int t = sc.nextInt();
        for(i=1;i<=t;i++)
        {
            
            n = sc.nextInt();
            j=2;
            while(j<=n)
            {

            // for(j=2;j<=n;j++)
            // {
                index=0;
                m = j;
                while(n>0)
                {
                    rem1 = n%m;
                    rem[(int)index++] = rem1;
                    n = (long) (n / m);
                }
                // for(k=index-1;k>=0;k--)
                // {
                    if(rem[1]==1)
                    {
                        count++;
                    }
                // }
                
                j++;
            }
            System.out.println(count);
                            
            // }
        }
        
      
    }
} 
英文:

The problem statement is :

Problem Statement-: Altaf has recently learned about number bases and is becoming fascinated.

Altaf learned that for bases greater than ten, new digit symbols need to be introduced, and that the convention is to use the first few letters of the English alphabet. For example, in base 16, the digits are 0123456789ABCDEF. Altaf thought that this is unsustainable; the English alphabet only has 26 letters, so this scheme can only work up to base 36. But this is no problem for Altaf, because Altaf is very creative and can just invent new digit symbols when she needs them. (Altaf is very creative.)

Altaf also noticed that in base two, all positive integers start with the digit 1! However, this is the only base where this is true. So naturally, Altaf wonders: Given some integer N, how many bases b are there such that the base-b representation of N starts with a 1?

Input Format :
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
Each test case consists of one line containing a single integer N (in base ten).
Output Format :
For each test case, output a single line containing the number of bases b, or INFINITY if there are an infinite number of them.
Constraints:
1 <= T <= 10^5
0 <= N < 10^12
Sample Input

 4

 6

 9

 11

 24

Sample Output:

 4

 7

 8

 14

Explanation:

In the first test case, 6 has a leading digit 1 in bases 2, 4, 5 and 6: 610 = 1102 = 124 = 115 = 106.

I trying this in java , But at some point my loop is not working it only takes the first value and after that it will come out of the loop!! Thank you

My code :

import java.util.*;

public class MyClass {
    public static void main(String args[]) {
      
        Scanner sc = new Scanner(System.in);
        long n,i,j,k,m;
        long count=0,rem1,index;
        long rem[];
        rem = new long[(int)100];
        int t = sc.nextInt();
        for(i=1;i&lt;=t;i++)
        {
            
            n = sc.nextInt();
            j=2;
            while(j&lt;=n)
            {

            // for(j=2;j&lt;=n;j++)
            // {
                index=0;
                m = j;
                while(n&gt;0)
                {
                    rem1 = n%m;
                    rem[(int)index++] = rem1;
                    n = (long) (n / m);
                }
                // for(k=index-1;k&gt;=0;k--)
                // {
                    if(rem[1]==1)
                    {
                        count++;
                    }
                // }
                
                j++;
            }
            System.out.println(count);
                            
            // }
        }
        
      
    }
} 

答案1

得分: 2

代码部分不要翻译,只返回翻译好的部分:

我不确定我理解循环中的逻辑(而且根据您自己的承认,那里有一个问题)。

循环的逻辑(即“有多少个基数以数字 1 开头表示数字 N”,可以大大简化。

第一步是找到表示数字 N 所需的基数 B 的最高幂次。这由 logb(n) 给出,截断为最近的整数。Java 没有内置的带有可变底数的对数函数,但可以通过计算 log(n)/log(b) 来获得此结果。
然后,您需要找到这个位置上的数字。这可以通过使用整数除法将 N 除以 Bpower 来计算。
从那里开始,您只需要检查结果是否为 1,如果是,则记录下来。

把所有这些结合起来,您最终会得到类似这样的代码:

private static int howManyBasesStartWithOne(int num) {
    int count = 0;
    for (int i = 2; i <= num; ++i) {
        int highestBase = (int) (Math.log(num) / Math.log(i));
        int leadingDigit = num / (int) Math.pow(i, highestBase);
        if (leadingDigit == 1) {
            ++count;
        }
    }
    return count;
}
英文:

I'm not sure I follow the logic in the loop (and, by your own admission, there's a problem there).

The logic of the loop (i.e., "how many bases represent the number N with a representation starting by 1"), can be greatly simplified.

The first step is finding the highest power of the base B required to represent the number N. This is given by log<sub>b</sub>(n), truncated to the nearest integer. Java doesn't have a built-in log function with a variable base, but you can get this result by calculating log(n)/log(b).<br/>
Then, you need to find the digit in this position. This can be calculated by dividing N by B<sup>power</sup> using integer division.<br/>
From there on, you just need to check if the result is 1, and if so, record it.

Put it all together and you'll end up with something like this:

private static int howManyBasesStartWithOne(int num) {
    int count = 0;
    for (int i = 2; i &lt;= num; ++i) {
        int highestBase = (int) (Math.log(num) / Math.log(i));
        int leadingDigit = num / (int) Math.pow(i, highestBase);
        if (leadingDigit == 1) {
            ++count;
        }
    }
    return count;
}

huangapple
  • 本文由 发表于 2020年9月9日 19:41:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63810921.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定