如何使这个代码更有效
How can this code be made more efficient?
我运行这段代码并输入值" 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
相关文章:
- GCC对可能有效的代码抛出init list生存期警告
- 这是什么代码?为什么它有效?C++
- 为什么此代码片段有效?如何取消引用空点?
- 使用C++字谜代码仅在有时有效
- 为什么以下代码在在线 ide(gcc 7.2.0) 上有效,但在 ubuntu 上出现错误?
- 为什么第二个代码有效而第一个代码无效?
- 代码是否有效.如果我想显示第一个元素?如果不是,那么 s.begin() 会返回什么?
- 代码 C++ 以添加前缀关键字以使其有效
- 为什么这个简短的模板代码片段有效
- 如何有效地处理Teamcenter ITK C++代码中的错误
- 这是有效的C++代码吗?(在声明范围之外使用本地类型)
- 以有效的方式从 Node .js运行 C 或C++代码
- 使用std ::函数时运行时异常.他们为什么不指向有效的代码
- 为什么认为参考文献在C 底漆第5个中没有变化,以下代码有效
- (C++)比较两段代码,一段有效,一段无效,无法找出区别
- 为什么这段代码在Visual Studio中有效,但在gcc中失败
- 通用lambda,继承和尾随返回类型:此有效代码
- 正在使用 std::time 和 std::srand 有效代码
- 用于确定两个数字中哪个较大的有效代码
- 有效代码无法通过visual studio 2015编译(std函数错误?)