我的代码有什么问题?它显示了不需要的结果
What's wrong with my code? It shows an unwanted result
#include <bits/stdc++.h>
#define ll unsigned long long int
using namespace std;
ll solve(ll arr[], ll n, ll b, ll x, ll pos) {
if(n == b)
return n;
ll idx = pos - 1;
for(ll i = n -1; i >= idx; --i) {
arr[i + 1] = arr[i];
}
arr[idx] = x;
return (n + 1);
}
signed main() {
ll t;
cin >> t;
while(t--) {
ll n;
cin >> n;
ll arr[n];
ll b;
cin >> b;
for(ll i = 0; i < b; ++i) {
cin >> arr[i];
}
ll x, pos;
cin >> x >> pos;
cout << solve(arr, n, b, x, pos) << endl;
}
cout << endl;
}
输入:
1 // testcases
5 // capacity of array
3 // number of elements to be inserting
3 4 7 // array elements
8 // element to be inserting
2 // position of the element
输出:
18446744073709551615
这是错误的。每次我 运行 这段代码,它都会像这样生成一个新的随机大数。我希望输出为 [3, 8, 4, 7]
请解释这是怎么回事,以及如何解决这个问题。基本上,每当我 运行 程序时,它都会生成一个不需要的大数字。试了很多方法,问题还是一样
您需要了解如何调试代码。如果您不能用调试器做到这一点,请编写一个函数将相关信息打印到屏幕上,例如:
void paxDebug(const char *desc, ll arr[], ll sz, ll n, ll b, ll x, ll pos) {
cout << desc << "\n";
cout << "n = " << n << " b = " << b << " x = " << x << " pos = " << pos << "\n";
for (ll i = 0; i < sz, ++i)
cout << " arg[" << i << "] = " << arg[i];
cout << "\n==========\n";
}
这将产生如下内容:
<Description>
n = 9 b = 9 x = 9 p = 9
arr[0] = 9 arr[1] = 9 arr[2] = 9 arr[3] = 9 arr[4] = 9
=========
然后在许多不同的地方调用此函数,例如:
ll solve(ll arr[], ll n, ll b, ll x, ll pos) {
paxDebug("start solve", arr[], n, n, b, x, pos);
if(n == b)
return n;
ll idx = pos - 1;
for(ll i = n -1; i >= idx; --i) {
paxDebug("in solve loop", arr[], n, n, b, x, pos);
arr[i + 1] = arr[i];
}
paxDebug("after solve loop", arr[], n, n, b, x, pos);
arr[idx] = x;
paxDebug("just before solve return", arr[], n, n, b, x, pos);
return (n + 1);
}
这应该允许您分析这些变量的值,这对诊断您的问题是一个非常宝贵的帮助。
而且,为了那些将来必须维护您的代码的人(即使是 您 六个月后),请使用更好的变量名:-)
#include <bits/stdc++.h>
#define ll unsigned long long int
using namespace std;
ll solve(ll arr[], ll n, ll b, ll x, ll pos) {
if(n == b)
return n;
ll idx = pos - 1;
for(ll i = n -1; i >= idx; --i) {
arr[i + 1] = arr[i];
}
arr[idx] = x;
return (n + 1);
}
signed main() {
ll t;
cin >> t;
while(t--) {
ll n;
cin >> n;
ll arr[n];
ll b;
cin >> b;
for(ll i = 0; i < b; ++i) {
cin >> arr[i];
}
ll x, pos;
cin >> x >> pos;
cout << solve(arr, n, b, x, pos) << endl;
}
cout << endl;
}
输入:
1 // testcases
5 // capacity of array
3 // number of elements to be inserting
3 4 7 // array elements
8 // element to be inserting
2 // position of the element
输出:
18446744073709551615
这是错误的。每次我 运行 这段代码,它都会像这样生成一个新的随机大数。我希望输出为 [3, 8, 4, 7]
请解释这是怎么回事,以及如何解决这个问题。基本上,每当我 运行 程序时,它都会生成一个不需要的大数字。试了很多方法,问题还是一样
您需要了解如何调试代码。如果您不能用调试器做到这一点,请编写一个函数将相关信息打印到屏幕上,例如:
void paxDebug(const char *desc, ll arr[], ll sz, ll n, ll b, ll x, ll pos) {
cout << desc << "\n";
cout << "n = " << n << " b = " << b << " x = " << x << " pos = " << pos << "\n";
for (ll i = 0; i < sz, ++i)
cout << " arg[" << i << "] = " << arg[i];
cout << "\n==========\n";
}
这将产生如下内容:
<Description>
n = 9 b = 9 x = 9 p = 9
arr[0] = 9 arr[1] = 9 arr[2] = 9 arr[3] = 9 arr[4] = 9
=========
然后在许多不同的地方调用此函数,例如:
ll solve(ll arr[], ll n, ll b, ll x, ll pos) {
paxDebug("start solve", arr[], n, n, b, x, pos);
if(n == b)
return n;
ll idx = pos - 1;
for(ll i = n -1; i >= idx; --i) {
paxDebug("in solve loop", arr[], n, n, b, x, pos);
arr[i + 1] = arr[i];
}
paxDebug("after solve loop", arr[], n, n, b, x, pos);
arr[idx] = x;
paxDebug("just before solve return", arr[], n, n, b, x, pos);
return (n + 1);
}
这应该允许您分析这些变量的值,这对诊断您的问题是一个非常宝贵的帮助。
而且,为了那些将来必须维护您的代码的人(即使是 您 六个月后),请使用更好的变量名:-)