如何找到共同因素? [英] How to find common factor?

查看:77
本文介绍了如何找到共同因素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hy,我正在编写一个程序来查找常见因素我的程序正确找到因素,并找到常见因素但输出其中一些因素。任何人都可以帮我找到所有常见因素吗?



我的尝试:



Hy, i am writing a program to find "Common factors" my program finds factors correctly and also find common factors but do output some of them. Can anyone help me to find all the common factors?

What I have tried:

#include<iostream>
using namespace std;
int main (){
	int a,i,factors_1[100],memory_1;
	cin>> a;
	cout<<"Factors Are:" << endl;
	memory_1=0;
	for(i=1 ; i<=a; i++){
		if(a%i==0){
			factors_1[memory_1]=i;
			memory_1++;
		}
	}
	for(int z=0;z<memory_1;z++){
		cout<< factors_1[z] << endl;
	}
	int m,j,factors_2[100],memory_2;
	cin>> m;
	cout<< "Factors Are:" << endl;
	memory_2=0;
	for(j=1; j<=m; j++){
		if(m%j==0){
			factors_2[memory_2]=j;
			memory_2++;
		}
	}
	for(int q=0;q<memory_2 ;q++){
		cout<< factors_2[q] << endl;
	}
	int w,common[100],v;
	v=0;
	for(w=0;w<memory_1;w++){
		if(factors_1[w] == factors_2[w]){
			common[v] = factors_1[v] ;
			v++;
		}
	}
	cout<< "Common Factors Are" << endl;
	for(int o=0;o<v;o++){
		cout<< common[o] << endl;
	}
	return 0;
}

推荐答案

通常你会分析最大公约数的两个(或更多)数字(或因素),而不是所有。你确定要打印所有吗?



如果你想要所有,一种方法是先确定最大公约数(见最大公约数 - 维基百科 [ ^ ])然后只发布该数字的所有因子(除数)。
Typically you analyze two (or more) numbers for the greatest common divisor (or factor), not all of them. Are you sure you want to print out all?

If you want all, one way would be to first determine the greatest common divisor (see Greatest common divisor - Wikipedia[^] ) and then just post all factors (divisors) of that number.


注意:单词 factor 通常是指构成整数的素数因素;你的程序正在寻找除数。

Note: the word factorusually refer to prime factors composing an integer; Your program is rather searching for divisors.
引用:

什么是debuuger?

what is debuuger?

调试器通常是与IDE集成,详细信息因IDE而异。您可能会为您的IDE找到tutos。

-----

您的问题是您的程序只有在相同的位置才能找到公约数。

-----

如果你不明白你的代码在做什么或为什么它做了什么,答案是调试器。 br />
使用调试器查看代码正在执行的操作。它允许你逐行执行第1行并在执行时检查变量,它是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做的比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

The debugger is usually integrated with your IDE, details vary with the IDE. You will probably find tutos for your IDE.
-----
Your problem is that your program find common divisors only if they are at same position.
-----
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这是缺陷:

Here is the flaw:
引用:

int w,common [100],v;

v = 0;

for(w = 0; w< ; memory_1; w ++){

if(factors_1 [w] == factors_2 [w]){

common [v] = factors_1 [v];

v ++;

}

}

int w,common[100],v;
v=0;
for(w=0;w<memory_1;w++){
if(factors_1[w] == factors_2[w]){
common[v] = factors_1[v] ;
v++;
}
}

你必须使用两个嵌套循环才能找到所有常见因素,例如

You have to use two nested loops in order to find all the common factors, e.g.

int w1,w2,common[100],v;
v=0;
for(w1=0;w1<memory_1;w1++)
{
  for(w2=0;w2<memory_2;w2++)
  {
    if(factors_1[w1] == factors_2[w2])
    {
      common[v] = factors_1[w1] ;
      v++;
      break;
    }
  }
}





请注意,你是使用 C ++ vector 类(以及字符串 one)可用。例如,您可以编写:



Please note, you are using C++: the vector class (and the string one) is available. You might write, for instance:

#include <iostream>
#include <string>
#include <vector>
using namespace std;


vector <int> find_factors(int n)
{
  vector <int> factor;

  for (int i=1; i<=n; ++i)
  {
    if ( (n % i) == 0)
      factor.push_back(i);
  }
  return factor;
}

vector <int> find_common(const vector<int> &va, const vector<int> & vb)
{
  vector <int> vcommon;

  for (size_t m=0; m<va.size(); ++m)
  {
    for (size_t n=0; n<vb.size(); ++n)
    {
      if ( va[m] == vb[n])
      {
        vcommon.push_back(va[m]);
        break;
      }
    }
  }
  return vcommon;
}

void show_vector( const string & header, const vector <int> & v)
{
  cout << header << endl;
  for (size_t n=0; n<v.size(); ++n)
    cout << v[n] << endl;
}

int main()
{
  int a,b;
  vector <int> fa, fb, fcommon;

  cin >> a;
  fa = find_factors(a);
  show_vector("factors are:", fa);

  cin >> b;
  fb = find_factors(b);
  show_vector("factors are:", fb);

  fcommon = find_common( fa, fb);
  show_vector("common factors are:", fcommon);
}


这篇关于如何找到共同因素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆