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

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

本文关键字:整数 C++ 转换 2位 数字 数组      更新时间:2023-10-16

是否可以采用一个充满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。

EDIT:这是一个接受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

正如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);
}    

我想提出以下解决方案。

您可以使用标头<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而不是整数来保持加密的结果。