簡體   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