如何使这个代码更有效

How can this code be made more efficient?

本文关键字:有效 代码 何使这      更新时间:2023-10-16

我运行这段代码并输入值" p ", "Q"answers"101",它似乎开始挂起。我想知道你们中的一些人使用什么样的编码和思维来帮助优化代码,使其尽可能快地运行,以及你们会对我的代码进行哪些更改。我认为主要问题在于"toSequencePos"子例程,因为它需要太长时间才能执行。

提前感谢。

[编辑]此代码是对BIO 2011的回答,可以在这里找到(问题1 A)

#include <iostream>
using namespace std;
int toNumber(char letter1)
{
    long long int position;
    switch (letter1)
    {
        case 'A': position = 1; break;
        case 'B': position = 2; break;
        case 'C': position = 3; break;
        case 'D': position = 4; break;
        case 'E': position = 5; break;
        case 'F': position = 6; break;
        case 'G': position = 7; break;
        case 'H': position = 8; break;
        case 'I': position = 9; break;
        case 'J': position = 10; break;
        case 'K': position = 11; break;
        case 'L': position = 12; break;
        case 'M': position = 13; break;
        case 'N': position = 14; break;
        case 'O': position = 15; break;
        case 'P': position = 16; break;
        case 'Q': position = 17; break;
        case 'R': position = 18; break;
        case 'S': position = 19; break;
        case 'T': position = 20; break;
        case 'U': position = 21; break;
        case 'V': position = 22; break;
        case 'W': position = 23; break;
        case 'X': position = 24; break;
        case 'Y': position = 25; break;
        case 'Z': position = 26; break;
        default:  position = 0; break;
    }
    return position;
}
int toLetter(long long int finalPosition)
{
    char letter;
    switch (finalPosition)
    {
        case 1: letter = 'A'; break;
        case 2: letter = 'B'; break;
        case 3: letter = 'C'; break;
        case 4: letter = 'D'; break;
        case 5: letter = 'E'; break;
        case 6: letter = 'F'; break;
        case 7: letter = 'G'; break;
        case 8: letter = 'H'; break;
        case 9: letter = 'I'; break;
        case 10: letter = 'J'; break;
        case 11: letter = 'K'; break;
        case 12: letter = 'L'; break;
        case 13: letter = 'M'; break;
        case 14: letter = 'N'; break;
        case 15: letter = 'O'; break;
        case 16: letter = 'P'; break;
        case 17: letter = 'Q'; break;
        case 18: letter = 'R'; break;
        case 19: letter = 'S'; break;
        case 20: letter = 'T'; break;
        case 21: letter = 'U'; break;
        case 22: letter = 'V'; break;
        case 23: letter = 'W'; break;
        case 24: letter = 'X'; break;
        case 25: letter = 'Y'; break;
        case 26: letter = 'Z'; break;
    }
    return letter;
}
int toSequencePos(long long int n1, long long int letterPos1, long long int letterPos2)
{
    long long int finalPosition = 0;
    for(long long int x = 1; x <= n1 - 2; x++)
    {
        finalPosition = letterPos1 + letterPos2;
        letterPos1 = letterPos2;
        letterPos2 = finalPosition;
    }
    while (finalPosition > 26)
    {
        finalPosition = finalPosition - 26;
    }
    return finalPosition;
}
int main()
{
    char letter1;
    char letter2;
    long long int letterPos1 = 0;
    long long int letterPos2 = 0;
    long long int sequenceLetterPos = 0;
    long long int n1;
    char finalAnswer;
    cout << "Please enter your first letter: ";
    cin >> letter1;
    letterPos1 = toNumber(letter1);
    cout << "Please enter your second letter: ";
    cin >> letter2;
    letterPos2 = toNumber(letter2);
    cout << "Please enter the position number that you wish to find in this sequence";
    cin >> n1;
    sequenceLetterPos = toSequencePos(n1, letterPos1, letterPos2);
    finalAnswer = toLetter(sequenceLetterPos);
    cout << "The letter in position " << n1 << " is " << finalAnswer << endl;
}

以下是一些优化点

int toNumber(char letter1)
{
    long long int position = lettter1 - 'A' + 1;    
    return position;
}
int toLetter(long long int finalPosition)
{
    char letter = 'A' + finalPosition - 1;
    return letter;
}
toSequencePos函数
// instead of following thing
while (finalPosition > 26)
{
    finalPosition = finalPosition - 26;
}
// use following
finalPosition = finalPosition % 26;

更新::

如果系统的字符编码方案有点非顺序,则上述解决方案将不起作用。即EBCDIC, BCD(字符编码)等

在这种情况下,你必须维护一个哈希映射&一个数组中。哈希映射包含char作为键&int作为值。char阵列就足够了。下面给出一个独立于编码的实现

#include <stdio.h>
#include <string.h>
#define NUMBER_OF_CHARACTER 26
char int2charMapping[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
//
//  suppose we spare 8 bit for character representation
//
unsigned int char2IntMapping[256];
void initiateTheChar2IntMapping()
{
    memset(char2IntMapping, 0, sizeof(char2IntMapping));
    char2IntMapping['A'] = 1;
    char2IntMapping['B'] = 2;
    char2IntMapping['C'] = 3;
    char2IntMapping['D'] = 4;
    char2IntMapping['E'] = 5;
    char2IntMapping['F'] = 6;
    char2IntMapping['G'] = 7;
    char2IntMapping['H'] = 8;
    char2IntMapping['I'] = 9;
    char2IntMapping['J'] = 10;
    char2IntMapping['K'] = 11;
    char2IntMapping['L'] = 12;
    char2IntMapping['M'] = 13;
    char2IntMapping['N'] = 14;
    char2IntMapping['O'] = 15;
    char2IntMapping['P'] = 16;
    char2IntMapping['Q'] = 17;
    char2IntMapping['R'] = 18;
    char2IntMapping['S'] = 19;
    char2IntMapping['T'] = 20;
    char2IntMapping['U'] = 21;
    char2IntMapping['V'] = 22;
    char2IntMapping['W'] = 23;
    char2IntMapping['X'] = 24;
    char2IntMapping['Y'] = 25;
    char2IntMapping['Z'] = 26;
}

char toLetter(long long int index)
{
    if(index > 0 && index < 27)
    {
        return int2charMapping[index-1];
    }
    return '';
}
int toNumber(char letter)
{
    return char2IntMapping[letter];
}

int main()
{
    //initiate the mapping
    initiateTheChar2IntMapping();
    printf("--> %cn", toLetter(26));
    printf("~~> %dn", toNumber('A'));
    return 0;
}

您实际上是在将字母映射到您自己选择的数字。您可以使用映射数据结构来做到这一点,但是要有效得多。HTH