HDU1215 与1406类似,但是要注意超时(cout 和 printf的区别!!!)

AC版本:

#include <iostream>
#include <cmath>
using namespace std;

int fuc(int a)
{
	int sum = 1;
	int b = sqrt(1.0 * a);
	for(int i = 2; i <= b; i++)
	{
		if(a % i == 0)
		{
			sum += i;
			int t = a / i;
			if(t != i)
				sum += t;
		}
	}
	return sum;
}
int main()
{
	int t; 
	int n;
	cin >> t;
	while(t--)
	{
		scanf("%d", &n);
		printf("%d\n", fuc(n)); //用cout的会超时
	}
	return 0;
}

而如果是用cout的话会超时!!!

#include <iostream>
#include <cmath>
using namespace std;

int fuc(int a)
{
	int sum = 1;
	int b = sqrt(1.0 * a);
	for(int i = 2; i <= b; i++)
	{
		if(a % i == 0)
		{
			sum += i;
			int t = a / i;
			if(t != i)
				sum += t;
		}
	}
	return sum;
}
int main()
{
	int t; 
	int n;
	cin >> t;
	while(t--)
	{
		scanf("%d", &n);
		cout << fuc(n) << endl;
	}
	return 0;
}


你可能感兴趣的:(HDU1215 与1406类似,但是要注意超时(cout 和 printf的区别!!!))