死循环求助

P1207 [USACO1.2] 双重回文数 Dual Palindromes

@[Harry_Haiyun](/user/977778) ``` int left = 0,right = n.length() - 1; while (left <= right) { if (n[left] != n[right]) return 0; } ``` 你这里左右相等,不直接死循环了,left和right都没进行操作,判断不对
by 编码落寞 @ 2023-08-04 11:03:59


@[编码落寞](/user/557751) 不过全[WA](https://baike.baidu.com/item/wa/85949054?fromModule=lemma_search-box)了 [#$include<bits/stdc++.h>$](https://www.luogu.com.cn/record/118950844) ``` using namespace std; const int maxn = 1e6+9; string change(int n,int s) { if (s == 10) { queue<int>st; while (n) { st.push(n % s); n /= s; } string str = ""; while (!st.empty()) { str += st.front(); st.pop(); } return str; } stack<int>st; while (n) { st.push(n % s); n /= s; } string str = ""; while (!st.empty()) { str += st.top(); st.pop(); } return str; } int stoi(string s) { int plus = 1,sum = 0; for (int i = 0; i < s.length(); i++) { sum += plus * (s[i] + '0'); plus *= 10; } return sum; } string to_string(int s) { string str = ""; while (s) { str += (s % 10 + '0'); s /= 10; } return str; } bool isP(string n,int s) { n = change(stoi(n),s); int left = 0,right = n.length() - 1; while (left <= right) { if (n[left] != n[right]) return 0; left++; right--; } return 1; } int main() { int n,s; cin >> n >> s; while (n) { int cnt = 0; bool flag = 0; for (int i = 2; i <= 10; i++) { if (isP(to_string(s),i)) cnt++; if (cnt == 2) { flag = 1; break; } } if (flag) { cout << s << '\n'; n--; } s++; } return 0; }
by __Harry_Haiyun__ @ 2023-08-04 11:57:13


@[Harry_Haiyun](/user/977778) 起始需要加一,题目描述的是大于。
by 编码落寞 @ 2023-08-04 13:18:19


@[Harry_Haiyun](/user/977778) ``` #include <bits/stdc++.h> #define MOD 998244353 #define mod 1000000007 #define ll long long #define ull unsigned long long #define ui unsigned int using namespace std; const int maxn = 1e6+9; string change(int n,int s) { //cout<<n<<endl; if (s == 10) { queue<int>st; while (n) { st.push(n % s); n /= s; } string str = ""; while (!st.empty()) { str += st.front(); st.pop(); } return str; } stack<int>st; while (n) { st.push(n % s); n /= s; } string str = ""; while (!st.empty()) { str += st.top(); st.pop(); } return str; } int stoi(string s) { int plus = 1,sum = 0; for (int i = 0; i < s.length(); i++) { sum += plus * (s[i] + '0'); plus *= 10; } return sum; } string to_string(int s) { string str = ""; while (s) { str += (s % 10 + '0'); s /= 10; } return str; } bool isP(string n,int s) { n = change(std::stoi(n),s); int left = 0,right = n.length() - 1; while (left <= right) { if (n[left] != n[right]) return 0; left++; right--; } return 1; } int main() { int n,s; cin >> n >> s; s++; while (n) { int cnt = 0; bool flag = 0; for (int i = 2; i <= 10; i++) { if (isP(std::to_string(s),i)) cnt++; if (cnt == 2) { flag = 1; break; } } if (flag) { cout << s << '\n'; n--; } s++; } return 0; } ```
by 编码落寞 @ 2023-08-04 13:19:09


@[编码落寞](/user/557751) 也是不行,我开的C++98
by __Harry_Haiyun__ @ 2023-08-04 13:39:27


@[Harry_Haiyun](/user/977778) 确实,我测试用的c++14。 具体原因不清楚
by 编码落寞 @ 2023-08-04 14:27:08


用C++11就能解决了。```stoi()```和```to_string()```只在C++11里面,已AC,此贴结。
by __Harry_Haiyun__ @ 2023-08-04 15:59:29


|