C++ 这段代码有浮点错误,有人能帮我吗
我面临着这个中国人的问题 我正在使用input.txt文件作为输入 并尝试生成output.txt文件,但显示浮点错误。当我在运行一些特定的输入时,它是工作的,但是对于许多测试用例来说它是不工作的C++ 这段代码有浮点错误,有人能帮我吗,c++,floating-point,C++,Floating Point,我面临着这个中国人的问题 我正在使用input.txt文件作为输入 并尝试生成output.txt文件,但显示浮点错误。当我在运行一些特定的输入时,它是工作的,但是对于许多测试用例来说它是不工作的 #include <bits/stdc++.h> #include <fstream> #include <iostream> using namespace std; int ModInverse(int a, int m) { a = a % m;
#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
using namespace std;
int ModInverse(int a, int m)
{
a = a % m;
for(int x = 1; x < m; x++)
if(((a * x) % m) == 1) return x;
}
int findMinX(int num[], int rem[], int k) {
int prod = 1;
for(int i = 1; i <= k; i++) prod *= num[i];
int result = 0;
for(int j = 1; j <= k; j++) {
int y = prod / num[j];
result = result + rem[j] * ModInverse(y, num[j]) * y;
}
return result % prod;
}
int main() {
ifstream infile;
infile.open("input.txt");
int n;
int num[100];
int rem[100];
infile >> n;
for(int i = 0; i < n; i++) infile >> num[i];
for(int i = 0; i < n; i++) infile >> rem[i];
infile.close();
int k = sizeof(num) / sizeof(num[0]);
ofstream myfile;
myfile.open("output.txt");
myfile << findMinX(num, rem, k);
myfile.close();
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int ModInverse(int a,int m)
{
a=a%m;
对于(int x=1;x>num[i];
对于(inti=0;i>rem[i];
infle.close();
int k=sizeof(num)/sizeof(num[0]);
流文件;
打开(“output.txt”);
myfile这些行:
for(int i=1;ia=a%m;
--返回结果%prod;
--m
和prod
的值是什么?另外,#include
--去掉该行。prod只是一个变量prod=1,就像m=m1*m2*m3*.m(n)。我们需要m的值,所以我用prod代替m表示mx==a(mod m)我正在获取input.txt文件。您是否验证了这些值?我没有看到您在代码中检查它们是否为0
findMinX(num,rem,k)
是可疑的。我希望findMinX(num,rem,n)
。在findMinX
中,我希望循环是从0
到@YogeshSIngh我的回答有什么意义吗?如果您对此有任何疑问,请随时提问。
int ModInverse(int a, int m)
{
a = a % m;
for(int x = 1; x < m; x++)
if(((a * x) % m) == 1) return x;
}
5
1 2 3 4 5
2 3 4 5 6