如何从循环生成的整数中删除前导零并将其存储为数组?

时间:2022-11-29 20:55:59

I have a for loop generating integers.

我有一个for循环生成整数。

For instance:

for (int i=300; i>200; i--)
    {(somefunction)*i=n;
    cout<<n;
    }

This produces an output on the screen like this:

这会在屏幕上产生如下输出:

f=00000000000100023;

I want to store the 100023 part of this number (i.e just ignore all the zeros before the non zero numbers start but then keeping the zeros which follow) as an array.

我想存储这个数字的100023部分(即在非零数字开始之前忽略所有零,但随后保持后面的零)作为数组。

Like this:

array[0]=1;
array[1]=0;
array[2]=0;
array[3]=0;
array[4]=2;
array[5]=3;

How would I go about achieving this?

我将如何实现这一目标?

5 个解决方案

#1


This is a mish-mash of answers, because they are all there, I just don't think you're seeing the solution.

这是一个混合的答案,因为他们都在那里,我只是觉得你没有看到解决方案。

First off, if they are integers Bill's answer along with the other answers are great, save some of them skip out on the "store in array" part. Also, as pointed out in a comment on your question, this part is a duplicate.

首先,如果他们是整数,比尔的答案和其他答案都是很好的,除了他们中的一些人跳过“存储阵列”部分。另外,正如您对问题的评论所指出的,这部分是重复的。

But with your new code, the solution I had in mind was John's solution. You just need to figure out how to ignore leading zero's, which is easy:

但是使用您的新代码,我想到的解决方案是John的解决方案。你只需要弄清楚如何忽略前导零,这很容易:

std::vector<int> digits;
bool inNumber = false;

for (int i=300; i>200; i--)    
{
    int value = (somefunction) * i;

    if (value != 0)
    {
        inNumber = true; // its not zero, so we have entered the number
    }

    if (inNumber)
    {
        // this code cannot execute until we hit the first non-zero number
        digits.push_back(value);
    }
}

Basically, just don't start pushing until you've reached the actual number.

基本上,只是在你达到实际数字之前不要开始推动。

#2


In light of the edited question, my original answer (below) isn't the best. If you absolutely have to have the output in an array instead of a vector, you can start with GMan's answer then transfer the resulting bytes to an array. You could do the same with JohnFx's answer once you find the first non-zero digit in his result.

根据编辑过的问题,我原来的答案(下面)并不是最好的。如果你绝对必须在数组而不是向量中输出,你可以从GMan的答案开始,然后将结果字节传递给数组。一旦找到结果中的第一个非零数字,您就可以对JohnFx的答案做同样的事情。


I'm assuming f is of type int, in which case it doesn't store the leading zeroes.

我假设f是int类型,在这种情况下它不存储前导零。

int f = 100023;

To start you need to find the required length of the array. You can do that by taking the log (base 10) of f. You can import the cmath library to use the log10 function.

首先,您需要找到所需的数组长度。您可以通过获取f的日志(基数为10)来实现。您可以导入cmath库以使用log10函数。

int length = log10(f);
int array[length];

length should now be 6.

长度现在应该是6。

Next you can strip each digit from f and store it in the array using a loop and the modulus (%) operator.

接下来,您可以从f中删除每个数字,并使用循环和模数(%)运算符将其存储在数组中。

for(int i=length-1; i >= 0; --i)
{
    array[i] = f % 10;
    f = f / 10;
}

Each time through the loop, the modulus takes the last digit by returning the remainder from division by 10. The next line divides f by 10 to get ready for the next iteration of the loop.

每次通过循环时,模数通过将除数除以10得到最后一位数。下一行将f除以10以准备下一次循环迭代。

#3


The straightforward way would be

直截了当的方式

std::vector<int> vec;
while(MyInt > 0)
{
  vec.push_back(MyInt%10);
  MyInt /= 10;
}

which stores the decimals in reverse order (vector used to simplify my code).

它以相反的顺序存储小数(用于简化我的代码的向量)。

#4


Hang on a second. If you wrote the code generating the integers, why bother parsing it back into an array?

稍等一下。如果您编写生成整数的代码,为什么还要将其解析回数组?

Why not just jam the integers into an array in your loop?

为什么不将整数堵塞到循环中的数组中呢?

int array[100];

for (int i=300; i>200; i--)    
{
    array[i]= (somefunction)*i;    
}

#5


Since the leading zeros are not kept because it represents the same number

由于前导零没有保留,因为它代表相同的数字

See: convert an integer number into an array

请参阅:将整数转换为数组

#1


This is a mish-mash of answers, because they are all there, I just don't think you're seeing the solution.

这是一个混合的答案,因为他们都在那里,我只是觉得你没有看到解决方案。

First off, if they are integers Bill's answer along with the other answers are great, save some of them skip out on the "store in array" part. Also, as pointed out in a comment on your question, this part is a duplicate.

首先,如果他们是整数,比尔的答案和其他答案都是很好的,除了他们中的一些人跳过“存储阵列”部分。另外,正如您对问题的评论所指出的,这部分是重复的。

But with your new code, the solution I had in mind was John's solution. You just need to figure out how to ignore leading zero's, which is easy:

但是使用您的新代码,我想到的解决方案是John的解决方案。你只需要弄清楚如何忽略前导零,这很容易:

std::vector<int> digits;
bool inNumber = false;

for (int i=300; i>200; i--)    
{
    int value = (somefunction) * i;

    if (value != 0)
    {
        inNumber = true; // its not zero, so we have entered the number
    }

    if (inNumber)
    {
        // this code cannot execute until we hit the first non-zero number
        digits.push_back(value);
    }
}

Basically, just don't start pushing until you've reached the actual number.

基本上,只是在你达到实际数字之前不要开始推动。

#2


In light of the edited question, my original answer (below) isn't the best. If you absolutely have to have the output in an array instead of a vector, you can start with GMan's answer then transfer the resulting bytes to an array. You could do the same with JohnFx's answer once you find the first non-zero digit in his result.

根据编辑过的问题,我原来的答案(下面)并不是最好的。如果你绝对必须在数组而不是向量中输出,你可以从GMan的答案开始,然后将结果字节传递给数组。一旦找到结果中的第一个非零数字,您就可以对JohnFx的答案做同样的事情。


I'm assuming f is of type int, in which case it doesn't store the leading zeroes.

我假设f是int类型,在这种情况下它不存储前导零。

int f = 100023;

To start you need to find the required length of the array. You can do that by taking the log (base 10) of f. You can import the cmath library to use the log10 function.

首先,您需要找到所需的数组长度。您可以通过获取f的日志(基数为10)来实现。您可以导入cmath库以使用log10函数。

int length = log10(f);
int array[length];

length should now be 6.

长度现在应该是6。

Next you can strip each digit from f and store it in the array using a loop and the modulus (%) operator.

接下来,您可以从f中删除每个数字,并使用循环和模数(%)运算符将其存储在数组中。

for(int i=length-1; i >= 0; --i)
{
    array[i] = f % 10;
    f = f / 10;
}

Each time through the loop, the modulus takes the last digit by returning the remainder from division by 10. The next line divides f by 10 to get ready for the next iteration of the loop.

每次通过循环时,模数通过将除数除以10得到最后一位数。下一行将f除以10以准备下一次循环迭代。

#3


The straightforward way would be

直截了当的方式

std::vector<int> vec;
while(MyInt > 0)
{
  vec.push_back(MyInt%10);
  MyInt /= 10;
}

which stores the decimals in reverse order (vector used to simplify my code).

它以相反的顺序存储小数(用于简化我的代码的向量)。

#4


Hang on a second. If you wrote the code generating the integers, why bother parsing it back into an array?

稍等一下。如果您编写生成整数的代码,为什么还要将其解析回数组?

Why not just jam the integers into an array in your loop?

为什么不将整数堵塞到循环中的数组中呢?

int array[100];

for (int i=300; i>200; i--)    
{
    array[i]= (somefunction)*i;    
}

#5


Since the leading zeros are not kept because it represents the same number

由于前导零没有保留,因为它代表相同的数字

See: convert an integer number into an array

请参阅:将整数转换为数组