繁体   English   中英

将2位数字的数组转换为整数(C ++)

[英]Converting an array of 2 digit numbers into an integer (C++)

是否可以采用填充2位数字的数组,例如

[10,11,12,13,...]

并将列表中的每个元素乘以100 ^(数组中的位置)并对结果求和,以便:

mysteryFunction[10,11,12] //The function performs 10*100^0 + 11*100^1 + 12*100^3

= 121110

并且

mysteryFunction[10,11,12,13]

= 13121110

当我不知道数组中的元素数量?

(是的,订单的反向是有意的,但不是100%必要的,以防万一你错过了第一次数字总是2位数

只是为了解决问题的一些背景:这是为了尝试改进我对RSA加密程序的尝试,此时我将数组的每个成员乘以100 ^(数字的位置)每次写出来意味着我用来加密的每个单词必须是一定的长度。

例如,加密“ab”我已将其转换为数组[10,11]但需要将其转换为1110才能通过RSA算法。 我需要调整我的代码,如果我然后想要使用三个字母的单词,再用一个四个字母的单词等,我相信你会同意这不是理想的。 我的代码与行业标准完全不同,但我很乐意上传它,如果有人想看到它(我已经在Haskell中管理过,如果有人想看到的话)。 我认为背景信息是必要的,以至于我不会从人们那里得到数百个贬值的人认为我试图欺骗他们为我做作业。 非常感谢你的帮助,我真的很感激!

编辑:谢谢你的所有答案! 他们完美地回答了我提出的问题但是我在将它们纳入我当前的程序时遇到了问题,如果我发布我的代码到目前为止你能帮忙吗? 当我试图提供答案时,我收到了一条错误消息(我无法投票,因为我没有足够的声誉,抱歉我还没有接受任何答案)。

#include <iostream>
#include <string>
#include <math.h>

int returnVal (char x)
{
    return (int) x;
}

unsigned long long modExp(unsigned long long b, unsigned long long e, unsigned long long m)
{
unsigned long long remainder;
int x = 1;

while (e != 0)
{
remainder = e % 2;
e= e/2;

if (remainder == 1)
x = (x * b) % m;
b= (b * b) % m;
}
return x;
}

int main()
{
    unsigned long long p = 80001;
    unsigned long long q = 70021;
    int e = 7;
    unsigned long long n = p * q;
    std::string foo = "ab";
    for (int i = 0; i < foo.length(); i++);

    {
        std::cout << modExp (returnVal((foo[0]) - 87) + returnVal (foo[1] -87) * 100, e, n);
    }
}

如果要使用纯C样式数组,则必须单独知道条目数。 使用这种方法,您的神秘功能可能定义如下:

unsigned mysteryFunction(unsigned numbers[], size_t n)
{
  unsigned result = 0;
  unsigned factor = 1;

  for (size_t i = 0; i < n; ++i)
  {
    result += factor * numbers[i];
    factor *= 100;
  }

  return result;
}

您可以使用以下代码测试此代码:

#include <iostream>

int main()
{
  unsigned ar[] = {10, 11, 12, 13};

  std::cout << mysteryFunction(ar, 4) << "\n";
  return 0;
}

另一方面,如果要使用STL的矢量类,则不需要单独使用该尺寸。 代码本身不需要太多更改。

另请注意,内置整数类型无法处理非常大的数字,因此您可能需要查看任意精度数字库,如GMP

编辑:这是函数的一个版本,它接受一个std::string并使用字符的ASCII值减去87作为数字:

unsigned mysteryFunction(const std::string& input)
{
  unsigned result = 0;
  unsigned factor = 1;

  for (size_t i = 0; i < input.size(); ++i)
  {
    result += factor * (input[i] - 87);
    factor *= 100;
  }

  return result;
}

测试代码变为:

#include <iostream>
#include <string>

int main()
{
  std::string myString = "abcde";
  std::cout << mysteryFunction(myString) << "\n";
  return 0;
}

该程序打印: 1413121110

我想建议以下解决方案。

您可以使用标头<numeric>声明的标准算法std::accumulate

例如

#include <iostream>
#include <numeric>


int main() 
{
    unsigned int a[] = { 10, 11, 12, 13 };

    unsigned long long i = 1;

    unsigned long long s = 
        std::accumulate( std::begin( a ), std::end( a ), 0ull,
            [&]( unsigned long long acc, unsigned int x )
            { 
                return ( acc += x * i, i *= 100, acc );
            } );

    std::cout << "s = " << s << std::endl;          

    return 0;
}

输出是

s = 13121110

使用基于范围的语句可以完成相同的操作

#include <iostream>
#include <numeric>


int main() 
{
    unsigned int a[] = { 10, 11, 12, 13 };

    unsigned long long i = 1;

    unsigned long long s = 0; 

    for ( unsigned int x : a )
    { 
        s += x * i; i *= 100;
    } 

    std::cout << "s = " << s << std::endl;          

    return 0;
}

你也可以写一个单独的函数

unsigned long long mysteryFunction( const unsigned int a[], size_t n )
{
   unsigned long long s = 0;
   unsigned long long i = 1;

   for ( size_t k = 0; k < n; k++ )
   {
      s += a[k] * i; i *= 100;
   }

   return s;
} 

还要考虑使用std::string而不是整数来保持加密结果。

正如benedek所提到的,这是一个通过std :: vector使用动态数组的实现。

unsigned mystery(std::vector<unsigned> vect)
{
    unsigned result = 0;
    unsigned factor = 1;

    for (auto& item : vect)
    {
        result += factor * item;
        factor *= 100;
    }

    return result;
}

void main(void)
{
    std::vector<unsigned> ar;
    ar.push_back(10);
    ar.push_back(11);
    ar.push_back(12);
    ar.push_back(13);

    std::cout << mystery(ar);
}    

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM