分解质因子、求最大公约数/最小公倍数、索引的实现示例

分解质因子

void main()
{
	int i,n;
	cin>> n;

	cout << n << "=";
	for(i=2;i<n;i++)
	{
		while (n != i)
		{
			if (n%i==0)
			{
				cout << i << "*";
				n=n/i;
			}
			else
				break;
		}
	}
	cout << n << endl;
}

求最大公约、最小公倍数:

//分解质因子,求取最大公约数和最小公倍数
	int A[100];
	int ii,jj;
	for (ii=0;ii<100;ii++)
	{
		A[ii]=0;
	}
	ii=0;
	int i,n,m;
	cin>> n;
	cin>> m;

	cout << n << "=";
	for(i=2;i<=n&&i<=m;i++)
	{
		while (i<=n && i<=m)
		{
			if (m%i==0 && n%i==0)
			{
				cout << i << "*";
				A[ii]=i;
				ii++;
				n=n/i;
				m=m/i;
			}
			else
				break;
		}
	}

	cout << endl;

	int comm_div=1;
	for (jj=0;jj<100;jj++)
	{
		if(A[jj]!=0)
			comm_div*=A[jj];
		else
			break;
	}
	cout << "the common divisor is" << comm_div << endl;
	cout << "the common multiple is"<< comm_div*m*n << endl;

完全数:

int div[1000];
	for(int i=0;i<1000;i++)
		div[i]=0;

	int sum,count;
	for (int m=1;m<=1000;m++)
	{
		count=0;
		sum=0;
		for (int n=1; n<m; n++ )
		{
			if (m%n==0)
			{
				div[count]=n;
				count++;
			}
		}
		for (int p=0;p<count;p++)
		{
			sum+=div[p];
		}
		if (sum==m)
		{
			cout << m << endl;
		}
	}

索引小结:

const int a = 1;
	cout << &a <<endl;
	//a++;
	//cout << &1 << endl;
	const char * a1[] = {"123456", "1"};
	const char * a2[] = {"2", "2"};
	a1[1] = "xxxx";
	
	cout << a1[0] << endl;
	std::cout << a1[1] << std::endl;
	cout << a2[0] << endl;
	std::cout << a2[1] << std::endl;



 

你可能感兴趣的:(ini,div)