给定非空的int类型数组,数组中的元素只有一个出现一次,其余都出现两次,找出这个唯一元素。
不知道为什么会发生下面这个错误?
Runtime Error:[ERROR] A Not allowed system call: runid:410352 callid:13
辅助解释:
A Not allowed system call: runid:410352 :使用了系统禁止的操作系统调用,看看是否越权访问了文件或进程等资源
#include <iostream>
using namespace std;
int search(int arr[], int len)
{
for (int i = 0; i < len; i++)
{
int num = 0;
for (int j = 0; j < len; j++)
{
if (arr[i] == arr[j])
{
num++;
}
}
if (num == 1)
{
return i;
}
}
}
int main()
{
int arr[1000];
int len ;
cin >> len;
for (int i = 0; i < len; i++)
{
cin >> arr[i];
}
int num = search(arr, len);
cout << arr[num] << endl;
system("pause");
return 0;
}
很久没有写过c++代码了,这种基本功能实现固然占分,其次还要考虑时间复杂度,发生错误的代码行是否这里“ int len ;
cin >> len;” 建议单步调试看看,
search函数看上去没有多大问题
#include <iostream>
using namespace std;
int main() {
int len, in, ans = 0;
cin >> len;
while(len--) {
cin >> in;
ans ^= in;
}
cout << ans;
return 0;
}
根据^异或运算符的特性(归零律, 恒等律, 交换律, 结合律, 自反, 两个相同的数进行异或操作会变成零, 零异或任何数就是被异或的那个数, 就算两个异或的数中间有别的数, 最后这两个数还是会被抵消掉)如果不懂异或, 可以去百度 https://baike.baidu.com/item/%E5%BC%82%E6%88%96/10993677?fr=aladdin, 本题不用开数组.
你的代码没有错, 是因为评测环境不支持system调用, 去掉system("pause");这一行即可.
望采纳