WIki on what is Strobogrammatic number: https://en.wikipedia.org/wiki/Strobogrammatic_number
#include <iostream>
#include <string>
using namespace std;
bool valid(char a, char b) {
switch(a) {
case '0':
return b == '0';
case '1':
return b == '1';
case '8':
return b == '8';
case '6':
return b == '9';
case '9':
return b == '6';
}
}
bool isStrobogrammatic(string num) {
if(num.size() == 0) return true;
int i = 0;
int j = num.size() -1;
while(i <= j) {
char a = num[i];
char b = num[j];
if(!valid(a, b)) return false;
i++;
j--;
}
return true;
}
int main(void) {
cout << isStrobogrammatic("619") << endl;
}
本文深入探讨斯特罗格马蒂克数的概念,并提供了一个简单的C++程序实例来验证给定字符串是否为斯特罗格马蒂克数。通过定义有效的字符对和一个递归检查函数,我们能够高效地识别这些独特的数字,它们在翻转时仍保持不变。
1558

被折叠的 条评论
为什么被折叠?



