如何构造重复方程?

发布于 2024-09-27 08:55:22 字数 173 浏览 8 评论 0原文

假设两个整数 x 和 N。

我试图确定如何构造一个算法,该算法将返回重复 N 次的值 x 的整数。

因此,如果 x 为 9 并且 N 为 4,则方程将返回 9999。
如果 x 为 9,N 为 5,则方程将返回 99999。(令人恶心)

我希望这不是完全荒谬或不合适的。 :)

Suppose two integers x and N.

I'm trying to determine how to construct an algorithm that would return an integer of the value x repeated N times.

So if x was 9 and N was 4, the equation would return 9999.
And if x was 9 and N was 5, the equation would return 99999. (ad nauseam)

I hope this isn't completely absurd or out of place on SO. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

那一片橙海, 2024-10-04 08:55:22

这对我有用:(10^N-1)/9*x

This works for me: (10^N-1)/9*x

瑶笙 2024-10-04 08:55:22

请注意,x 是一个整数,并且不一定是 10 进制系统中的 1 位数字。如果 N = 3 且 x = 12 会怎样?那么答案应该是 121212。

​​解决方案如下:我们需要以 10 为底的系统中数字 x 的长度 p。设 p = Floor(lg(x)+1)。我们要搜索的数字是x + x*10^p + x*10^2p + ... + x*10^(N-1)p。即 x * (10^(pN) - 1) / (10^p - 1)。

Note that x is an integer and it does not have to be a 1-digit number in base-10 system. What if N = 3 and x = 12? Then the answer should be 121212.

Here is the solution: we need the length p of the number x in base-10 system. Let p = floor(lg(x)+1). The number we are searching for is x + x*10^p + x*10^2p + ... + x*10^(N-1)p. That is x * (10^(pN) - 1) / (10^p - 1).

注定孤独终老 2024-10-04 08:55:22

这看起来更像是一个编程问题,因为解决方案很大程度上依赖于以 10 为基数的数字系统。执行此操作的算法只是对 N 进行简单的循环,将前一个数字相乘并加上 x。

int foo(int x, int N) {
  int result = 0;
  for(i=0; i<N; i++) {
    result *= 10;
    result += x;
  }
  return result;
}

This seems like more of a programming question, as the solution is heavily dependent on the base-10 number system. The algorithm to do this would just be a simple loop over N which multiplies the previous number and adds a x.

int foo(int x, int N) {
  int result = 0;
  for(i=0; i<N; i++) {
    result *= 10;
    result += x;
  }
  return result;
}
谜兔 2024-10-04 08:55:22

伪代码:

Procedure Construct takes x:integer N:integer
begin
   take a variable Result and initialize with 0;
   For N times Do
   begin
      Result <- Result * 10
      Result <- Result + x
   end
end

C++ 示例:

int main()
{
   const int x = 9, N = 5;
   int Result = 0;
   for(int i = 0; i < N; ++i)
   {
      Result*=10;
      Result+=x;   
   }
   //use result here
}

pseudocode:

Procedure Construct takes x:integer N:integer
begin
   take a variable Result and initialize with 0;
   For N times Do
   begin
      Result <- Result * 10
      Result <- Result + x
   end
end

a C++ example:

int main()
{
   const int x = 9, N = 5;
   int Result = 0;
   for(int i = 0; i < N; ++i)
   {
      Result*=10;
      Result+=x;   
   }
   //use result here
}
半﹌身腐败 2024-10-04 08:55:22

只是为了有点不同,我用这个递归函数制作了一个 JavaScript 小提琴:

function repeating(x, n){
    return (n) ? (x * Math.pow(10,n-1)) + repeating(x, n-1) : 0;
};

Fiddle: http://jsfiddle .net/SZKeb/2/

它只是从 N 向后计算,因此本质上将计算为 9000 + 900 + 90 + 9 + 0 = 9999

Just to be a little different, I've made a JavaScript fiddle with this recursive function:

function repeating(x, n){
    return (n) ? (x * Math.pow(10,n-1)) + repeating(x, n-1) : 0;
};

Fiddle: http://jsfiddle.net/SZKeb/2/

It just works backwards from N, so essentially will calculate as 9000 + 900 + 90 + 9 + 0 = 9999

旧竹 2024-10-04 08:55:22

在 python 中,这非常简单:

def repeat(x, N):
    return int(str(x) * N)

In python, this is super easy:

def repeat(x, N):
    return int(str(x) * N)
触ぅ动初心 2024-10-04 08:55:22

听起来更像是你试图构造一串重复的数字,而不是做实际的数学运算。为什么不执行以下操作(C#)?

using System;
using System.Text;

public int CreateInt(int x, int N)
{
    StringBuilder createdString = new StringBuilder();
    int createdInt;

    for (int i = 0; i < N; i++)
        createdString.Append(x.ToString());

    if (!int.TryParse(createdString.ToString(), out createdInt))
        throw new Exception(string.Format("Value x ({0}) repeated N ({1}) times makes {2}.  This is not a valid integer.", x, N, createdString));

    return createdInt;
}

int createdInt1 = CreateInt(7, 5);  // output: 77777
int createdInt2 = CreateInt(14, 4); // output: 14141414
int createdInt3 = CreateInt(1, 20); // output: throws exception "Value x (1) repeated N (20) times makes 11111111111111111111.  This is not a valid integer."

此示例显示了您需要注意的几件事:

  1. 对于您正在使用的任何语言进行编程,创建的结果是否是有效的整数?
  2. 如果要重复的整数 (x) 是两位数或更高怎么办?

Sounds more like you're trying to construct a string of repeating numbers than doing actual math. Why not do the following (C#)?

using System;
using System.Text;

public int CreateInt(int x, int N)
{
    StringBuilder createdString = new StringBuilder();
    int createdInt;

    for (int i = 0; i < N; i++)
        createdString.Append(x.ToString());

    if (!int.TryParse(createdString.ToString(), out createdInt))
        throw new Exception(string.Format("Value x ({0}) repeated N ({1}) times makes {2}.  This is not a valid integer.", x, N, createdString));

    return createdInt;
}

int createdInt1 = CreateInt(7, 5);  // output: 77777
int createdInt2 = CreateInt(14, 4); // output: 14141414
int createdInt3 = CreateInt(1, 20); // output: throws exception "Value x (1) repeated N (20) times makes 11111111111111111111.  This is not a valid integer."

This sample shows a couple of things you'll wanna watch out for:

  1. Is the created result a valid integer for whatever language you're programming in?
  2. What if the integer to repeat (x) is double digits or higher?
你列表最软的妹 2024-10-04 08:55:22

要遵循的伪代码。其本质是您要数到 n,每次数数时您都会打印出您的 x

for(int i=1; i <=x ; i++)
{
 system.print("n");
}

Pseudo code to follow. The essence of this is you are going to count up to n and every time you count you will print out your x

for(int i=1; i <=x ; i++)
{
 system.print("n");
}
她如夕阳 2024-10-04 08:55:22
x*(10^(floor(log(x)-1)*N))/(10^(floor(log(x)-1)*N))
x*(10^(floor(log(x)-1)*N))/(10^(floor(log(x)-1)*N))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文