如何使这些代码更有效?

时间:2016-11-13 15:29:22

标签: c++ performance fibonacci

我运行此代码并输入值“P”,“Q”和“101”,它似乎开始挂起。我想知道你们有些人使用什么样的编码和思想来帮助优化你的代码,使它尽可能快地运行,以及你对我的代码所做的改变。我认为主要问题在于“toSequencePos”子程序,因为它需要很长时间才能完成。

提前致谢。

[编辑]此代码是对BIO 2011的回答,可以找到here(问题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;
}

2 个答案:

答案 0 :(得分:2)

以下是一些优化点

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;

<强> UPDATE ::

如果系统的字符编码方案有点非顺序,则上述解决方案将无效。即EBCDICBCD (character encoding)

在这种情况下,你必须维护一个哈希映射&amp;一个阵列。哈希映射包含char作为关键字&amp; 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 '\0';
}

int toNumber(char letter)
{
    return char2IntMapping[letter];
}


int main()
{

    //initiate the mapping
    initiateTheChar2IntMapping();

    printf("--> %c\n", toLetter(26));
    printf("~~> %d\n", toNumber('A'));

    return 0;
}

答案 1 :(得分:-1)

您实际上是将字母映射到您自己选择的数字。您可以使用地图数据结构来执行此操作,但更多更有效。 HTH