2014-03-04 48 views
0

是否可以采用填充2位数字的数组将2位数字的数组转换为整数(C++)

[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); 
    } 
} 
+0

您将问题标记为C++,因此请提供您所需的具体C++代码。 'mysteryFunction [10,11,12]'不是C++函数。用'mysteryFunction(10,11,12)'代替意味着元素的数量是已知的。你的数据在哪里,以什么格式? – iavr

+0

对不起,我用它作为我想要的伪代码示例,如果我可以提供具体的C++格式,我不会问这个问题。如果我再次提出问题,我会更加清楚。最后我不完全了解你的问题。 – MichaelRad

+0

你得到的错误是什么?你的代码与你所要求的代码有点不同......我只在'main'循环中得到一个关于无符号/有符号比较的警告。另外,注意,你可能想在你的if(余数== 1)之后放置花括号。并缩进你的代码! – benedek

回答

2

如果你想使用简单的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的vector类,你不会单独需要这个尺寸。代码本身不需要太多的改变。

另请注意,内置整数类型无法处理非常大的数字,因此您可能需要查看任意精度数字库,如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

+0

非常感谢!我会放弃这一点并让你知道。 – MichaelRad

+0

它的工作原理很完美,但我遇到了一些问题,将其纳入我的程序中,我已经在我的问题中提供了详细信息,您能否帮我一把吗?再次感谢,如果不是,那很好 – MichaelRad

0

我想建议下面的溶胶utions。

你可以使用标准算法std::accumulate在头部声明<numeric>

例如

#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 

同样可以使用基于for语句

的范围内进行
#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而不是整数号,以保持加密的结果。

0

由于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); 
}