I have this problem: 题目描述…
- User
I have this problem: 题目描述 你是一名著名的密码破译专家,在一次野外探险活动中,意外发现了一份被前人遗弃的地图,它指向了一个隐藏在城市地下的神秘金库。这个金库被一个复杂的数字密码所保护,而密码的线索则藏在一段文字信息中。
为了打开这个金库,你需要:
(1)从这段文字信息中找出所有表示数字的英文单词,这些数字是15以内(含15)的非负整数k(0<=k<=15)。注意:这些英文单词中的某些字母可能是大写的,例如"zero"、"One"、"fiFTEen"等。
(2)将每个英文单词所表示的数字平方后对 100 取模,生成一系列两位数,不足两位的要在前面补0,例如00, 21, 25, 49。
(3)将这些两位数按数值大小从小到大排序,然后拼接成一个新数(例如,若三个数为32,11,73,则新数为113273),这个数即为开启金库的密码。如果新数开头是0,则去除这些0后的数是金库的密码。如果新数各位全都为0,则最终需保留一个0。
输入描述 输入共两行。第一行是一个数字n(0<n<=1000),代表文字信息中单词的个数。
第二行共有n个单词(单词之间用空格分隔),其中包含的表示数字的单词数量不超过9个(数字值可能有重复)。
100%的数据满足0<n<=1000,包含的表示数字的英文单词数量<=9。
输出描述 输出共一行,代表按操作要求得到的密码。
样例输入 8 The apple price is five point Two two
样例输出 40425
注释 样例解释:样例输入中包含三个英文单词:five Two two,按要求进行操作后得到的数字分别为25,04,04,先排序再拼接得到040425,最后去除开头的0,即为40425。
表示数字0至15的英文单词为:"zero"、"one"、"two", "three"、"four"、"five"、"six"、"seven"、"eight"、"nine"、"ten"、"eleven"、"twelve"、"thirteen"、"fourteen"、"fifteen" And I'm trying to solve it with this code, but it's not working and is giving the wrong answer. Why is it so? Fix it with the least amount of changes, and only talk about logic incorrectness, instead of code style. #include #include #include #include using namespace std;
struct exchange { string word;
int number; };int main() { exchange lists[] = { {"zero", 0}, {"one", 1}, {"two", 2}, {"three", 3}, {"four", 4}, {"five", 5}, {"six", 6}, {"seven", 7}, {"eight", 8}, {"nine", 9}, {"ten", 10}, {"eleven", 11}, {"twelve", 12}, {"thirteen", 13}, {"fourteen", 14}, {"fifteen", 15}, };
int n; string sentence; long int sum[9] = {0}; int a = 0;
cin>>n; cin.ignore(); getline(cin,sentence);
for (int i = 0; i < sentence.length(); i++) { sentence[i] = tolower(sentence[i]); }
string extract; stringstream ss(sentence); //initializing stringstream ss(sentence) after reading the input into sentence
while(ss >> extract){ for(int j = 0; j < 15; j++){ if(extract == lists[j].word){ sum[a] = ((lists[j].number) * (lists[j].number))%100; a++; } } }
for(int i = 0; i < a-1; i++){ for(int j = 0; j <a-1-i; j++){ if(sum[j] > sum[j+1]){ int tmp = sum[j]; sum[j] = sum[j+1]; sum[j+1] = tmp; } } }
bool firstNumberPrinted = false; for (int i = 0; i < a; i++) { if (sum[i] == 0) { continue; }
if (!firstNumberPrinted) {cout<<sum[i]; firstNumberPrinted = true; } else{ if(sum[i] >= 10){ cout << sum[i]; } if (sum[i] < 10) { cout <<'0'<< sum[i]; } } }
if (!firstNumberPrinted) { cout <<'0'; }cout<<endl; return 0; }
- Assistant
The issue in your code lies in the loop where you compare each extracted word with the entries in your
listsarray. Specifically, yourforloop usesj < 15, which means it only iterates fromj = 0toj = 14, missing the last element of thelistsarray, which is"fifteen"at index 15.Here's the problematic loop:
for (int j = 0; j < 15; j++) { if (extract == lists[j].word) { sum[a] = ((lists[j].number) * (lists[j].number)) % 100; a++; } }Because you have 16 elements in your
listsarray (from index 0 to 15), the loop should iterate up toj = 15to include"fifteen". By only going up toj < 15, you're excluding the last number, which causes incorrect results whenever the input contains the word"fifteen".Minimal Fix:
Change the loop condition to
j < 16to include all elements in thelistsarray: