我的程序中出现 segsigv 错误的原因是什么?
what can be the cause of segsigv error in my program?
我对编码很陌生,我试图解决一个问题,在这个问题中我们会得到一些测试用例(比如 n)和一个整数(比如 k)。因此,对于每个测试用例,将给出一个新的整数(比如 a),我们必须找到它可以被 k 整除的数字(比如总和)。
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n,k,sum;
scanf("%d",n);
scanf("%d",k);
while(n>0)
{
int a;
scanf("%d",a);
if(a%k==0)
{
sum++;
}
n--
}
printf("%d",sum);
return 0;
}
首先,您在使用scanf
扫描时提供变量的地址:
scanf("%d", &n);
scanf("%d", &k);
scanf("%d", &a);
此外,初始化 sum
:
sum = 0;
其次,这是 C++,因此请改用 cin
和 cout
:
std::cin >> n >> k;
std::cin >> a;
std::cout << sum;
我对编码很陌生,我试图解决一个问题,在这个问题中我们会得到一些测试用例(比如 n)和一个整数(比如 k)。因此,对于每个测试用例,将给出一个新的整数(比如 a),我们必须找到它可以被 k 整除的数字(比如总和)。
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n,k,sum;
scanf("%d",n);
scanf("%d",k);
while(n>0)
{
int a;
scanf("%d",a);
if(a%k==0)
{
sum++;
}
n--
}
printf("%d",sum);
return 0;
}
首先,您在使用scanf
扫描时提供变量的地址:
scanf("%d", &n);
scanf("%d", &k);
scanf("%d", &a);
此外,初始化 sum
:
sum = 0;
其次,这是 C++,因此请改用 cin
和 cout
:
std::cin >> n >> k;
std::cin >> a;
std::cout << sum;