Kali_linux @ 2023-04-06 23:33:10
我问了下chatgpt怎么写这道题
他给出的代码是
#include <iostream>
#include <string>
#include <algorithm>
#include <unordered_set>
using namespace std;
int n;
string m;
bool is_palindrome(const string &s) {
int n = s.size();
for (int i = 0; i < n / 2; ++i) {
if (s[i] != s[n - i - 1]) {
return false;
}
}
return true;
}
string add(const string &a, const string &b) {
string res;
int carry = 0;
int n = a.size(), m = b.size();
for (int i = 0; i < max(n, m); ++i) {
int x = i < n ? a[n - i - 1] - '0' : 0;
int y = i < m ? b[m - i - 1] - '0' : 0;
int z = x + y + carry;
res.push_back(z % n + '0');
carry = z / n;
}
if (carry) {
res.push_back(carry + '0');
}
reverse(res.begin(), res.end());
return res;
}
int bfs() {
unordered_set<string> visited;
visited.insert(m);
int step = 0;
while (!visited.empty() && step < 30) {
unordered_set<string> next;
for (const string &s : visited) {
if (is_palindrome(s)) {
return step;
}
string t = s;
reverse(t.begin(), t.end());
string u = add(s, t);
next.insert(u);
}
visited = move(next);
++step;
}
return -1;
}
int main() {
cin >> n >> m;
cout << (bfs() == -1 ? "Impossible!" : "STEP=" + to_string(bfs())) << endl;
return 0;
}
结果似乎WA了
by dengchengzhuo @ 2023-04-06 23:39:20
说实话,能跑出来就不错了 (bushi
by ud2_ @ 2023-04-07 00:07:22
@@ -1,12 +1,20 @@
#include <algorithm>
+#include <cctype>
#include <iostream>
#include <string>
-#include <unordered_set>
using namespace std;
int n;
string m;
+char to_digit(int value) {
+ return value < 10 ? '0' + value : 'A' + (value - 10);
+}
+
+int from_digit(char digit) {
+ return isdigit(digit) ? digit - '0' : 10 + (toupper(digit) - 'A');
+}
+
bool is_palindrome(const string& s) {
int n = s.size();
for (int i = 0; i < n / 2; ++i) {
@@ -22,35 +30,29 @@
int carry = 0;
int n = a.size(), m = b.size();
for (int i = 0; i < max(n, m); ++i) {
- int x = i < n ? a[n - i - 1] - '0' : 0;
- int y = i < m ? b[m - i - 1] - '0' : 0;
+ int x = i < n ? from_digit(a[n - i - 1]) : 0;
+ int y = i < m ? from_digit(b[m - i - 1]) : 0;
int z = x + y + carry;
- res.push_back(z % n + '0');
- carry = z / n;
+ res.push_back(to_digit(z % ::n));
+ carry = z / ::n;
}
if (carry) {
- res.push_back(carry + '0');
+ res.push_back(to_digit(carry));
}
reverse(res.begin(), res.end());
return res;
}
int bfs() {
- unordered_set<string> visited;
- visited.insert(m);
+ string s = m;
int step = 0;
- while (!visited.empty() && step < 30) {
- unordered_set<string> next;
- for (const string& s : visited) {
- if (is_palindrome(s)) {
- return step;
- }
- string t = s;
- reverse(t.begin(), t.end());
- string u = add(s, t);
- next.insert(u);
+ while (step < 30) {
+ if (is_palindrome(s)) {
+ return step;
}
- visited = move(next);
+ string t = s;
+ reverse(t.begin(), t.end());
+ s = add(s, t);
++step;
}
return -1;
和正解差得不大(
by qifan_maker @ 2023-04-07 07:01:33
@ud2_ 您这个对比的是手打还是什么工具啊,一直在找这种工具。
by Imken @ 2023-04-07 07:42:13
@qifan_maker
Linux 下的 diff
。
上文的指令应该是 diff -u <a> <b>
。
by liangbowen @ 2023-04-07 08:27:13
别把chatgpt想象得太厉害,一般这种题都是需要反复提示chatgpt哪里错了的
by QAQ__ @ 2023-04-07 09:12:13
@Kali_linux ChatGPT 不行不是众所周知的吗?
@qifan_maker 好像有个东西叫 Difftastic?
by 654179_qwq @ 2023-04-07 12:23:41
@Kali_linux 用 gpt4 .