我正在翻轉一串數字。編譯時我沒有收到任何錯誤或警告,但是在我輸入數字后,彈出一個錯誤視窗,我無法理解其中的單詞。有人能幫我嗎?
我的目的是:看看,在一個數字的區間內,有多少個數字在旋轉 180 度時是相同的?(例如8888、6699、90088006)
環境:作業系統:Windows 10、64位
IDE:Visual Studio 社區 2022
錯誤視窗:

代碼:
#include <iostream>
#include <string>
using namespace std;
int total = 0;
void numf(int n, string c) {
if (!(c.find('3') == 0 and c.find('4') == 0 and c.find('7') == 0 and
c.find('2') == 0 and c.find('5') == 0)) {
// reverse
string c2;
for (int i = 0; i < sizeof(c) / sizeof(c[0]); i ) {
c2[i] = c[sizeof(c) / sizeof(c[0]) - i - 1];
}
for (int i = 0; i < sizeof(c) / sizeof(c[0]); i ) {
if (c2[i] == '6') {
c2[i] = '9';
} else if (c2[i] == '9') {
c2[i] = '6';
}
}
if (c2 == c) {
total ;
}
}
return;
}
int main() {
int num, num2;
cin >> num >> num2;
for (int i = num; i <= num2; i ) {
string newnum = to_string(i);
numf(i, newnum);
}
cout << total;
return 0;
}
uj5u.com熱心網友回復:
對于初學者來說,這個 if 陳述句
if (!(c.find('3') == 0 and c.find('4') == 0 and c.find('7') == 0 and
c.find('2') == 0 and c.find('5') == 0)) {
沒有意義。
您似乎正在嘗試排除包含所列數字之一的數字。
在這種情況下,你應該寫
if ( c.find_first_of( "34725" ) == std::string::npos )
{
//...
該類std::string不是陣列。所以像下面這樣的回圈中的運算式sizeof(c) / sizeof(c[0])也沒有意義。
for (int i = 0; i < sizeof(c) / sizeof(c[0]); i ) {
c2[i] = c[sizeof(c) / sizeof(c[0]) - i - 1];
}
而且物件c2是空的。所以你可能不會使用像c2[i].
你可以只寫而不是回圈
std::string c2( c.rbegin(), c.rend() );
這個不正確的 for 回圈
for (int i = 0; i < sizeof(c) / sizeof(c[0]); i ) {
if (c2[i] == '6') {
c2[i] = '9';
} else if (c2[i] == '9') {
c2[i] = '6';
}
}
可以針對以下基于范圍的 for 回圈進行更改
for ( auto &ch : c2 )
{
if ( ch == '6')
{
ch = '9';
}
else if ( ch == '9' )
{
ch = '6';
}
}
注意函式中沒有使用第一個函式引數。
total在函式中使用全域變數也是一個壞主意。bool如果函式具有回傳型別并true在數字滿足要求時回傳,那就更好了。
如果我正確理解了分配,那么我將撰寫如下所示的程式。
#include <iostream>
#include <string>
#include <utility>
#include <algorithm>
bool numf( const std::string &s )
{
if ( s.find_first_of( "34725" ) == std::string::npos )
{
std::string s2( s.rbegin(), s.rend() );
for ( auto &c :s2 )
{
if (c == '9')
{
c = '6';
}
else if (c == '6')
{
c = '9';
}
}
return s == s2;
}
else
{
return false;
}
}
int main()
{
unsigned int num1 = 0, num2 = 0;
std::cin >> num1 >> num2;
std::tie( num1, num2 ) = std::minmax( { num1, num2 } );
unsigned int total = 0;
for (; num1 <= num2; num1)
{
total = numf( std::to_string( num1 ) );
}
std::cout << "total = " << total << '\n';
}
程式輸出可能看起來像
60 99
total = 3
uj5u.com熱心網友回復:
你有有趣的任務。以防萬一您對解決任務的其他方法感到好奇,而不僅僅是如何修復您的代碼,為此我為您撰寫了自己的解決方案。
我的解決方案是使用小型轉換表"01xxxx9x86"來確定數字的 180 度旋轉,x這里表示數字不可旋轉。
如您所見,我包含1為可旋轉的,與您在代碼中所做的相同,但在某些型別的字體中1旋轉 180 度看起來不一樣。x您可以通過在我的代碼轉換表中用符號標記它來將其從可旋轉物件中排除。
在線嘗試!
#include <cstdint>
#include <string>
#include <iostream>
uint64_t RotateCount(uint64_t begin, uint64_t end) {
uint64_t cnt = 0;
char constexpr conv[] = "01xxxx9x86";
for (uint64_t n = begin; n < end; n) {
std::string const s = std::to_string(n);
bool matched = true;
for (size_t i = 0; i < (s.size() 1) / 2; i)
if (conv[s[i] - '0'] != s[s.size() - 1 - i]) {
matched = false;
break;
}
if (matched)
cnt;
}
return cnt;
}
int main() {
uint64_t begin = 1'000'000, end = 9'000'000;
std::cout << "Count: " << RotateCount(begin, end)
<< std::endl;
}
輸入范圍:
1'000'000 ... 9'000'000
輸出:
Count: 225
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/460384.html
