-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion46.cpp
More file actions
41 lines (38 loc) · 1.12 KB
/
Copy pathquestion46.cpp
File metadata and controls
41 lines (38 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
把数字翻译成字符串
给定一个数字,我们按照如下规则把它翻译成字符串。
0 翻译成 “a”, 1 翻译成 “b”,...., 25 翻译成 “z”。一个数字可能有多个翻译。
例如, 12258 有 5 中不同的翻译,分别是 “bccfi”、“bwfi”、“bczi”、“mcfi”、“mzi”。
请编程实现一个函数,用来计算一个数字有多少种翻译方法。
Xiaobin Tian;
*/
#include<vector>
using namespace::std;
int GetTranslationCout(int number){
if(number < 0)
return 0;
if(number < 10)
return 1;
vector<int> num;
num.push_back(0);
while(number != 0){
num.push_back(number%10);
number = number / 10;
}
int *cout = new int[num.size()];
cout[0] = 1;
cout[1] = 1;
for(int i = 2; i < num.size(); ++i){
int temp = num[i] * 10 + num[i-1];
if(temp <= 25 && temp >= 10){
cout[i] = cout[i-1] + cout[i-2];
}
else{
cout[i] = cout[i-1];
}
}
int result = cout[num.size()-1];
delete[] cout;
return result;
}