-2
我的代码如下。这是C++代码。 此代码在两个数字中进行通用划分。 例如,如果输入是18,24,则输出是2,2,2,3,3。 但我只想输出2和3。 我无法修复它。我怎样才能解决这个问题?感谢您的帮助...为了避免在C++中重复数组中的数字
#include<iostream>
using namespace std;
class Ratio {
public:
Ratio(int numerator, int denumerator) {
num=numerator;
den=denumerator;
}
void commondivisor() {
int arr[20];
int arr2[20];
int c=0;
int c2=0;
for (int q = 2; num != 1; q ++)
{
if (num % q == 0)
{
num /= q;
arr[c]=q;
q --;
c++;
}
}
cout << endl;
for (int w = 2; den != 1; w ++)
{
if (den % w == 0)
{
den /= w;
arr2[c2]=w;
w --;
c2++;
}
}
for (int i=0; i<c; i++) {
for (int j=0; j<c2; j++) {
if (arr[i]==arr2[j])
cout<<arr2[j];
}
}
}
private:
int num;
int den;
};
int main() {
int a;
int b;
cin >> a;
cin >> b;
Ratio nesne(a,b);
nesne.commondivisor();
return 0;
}
您是否在插入前尝试找到它? http://en.cppreference.com/w/cpp/algorithm/find – willll
是的,但没有 – frkn6161