Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

~

Posted by uestc254136 at 2009-04-29 15:53:57 on Problem 2000
此题打表法才能过 
先开始直接法思路清晰但是超时 
代码如下 
#include <iostream>
using namespace std;
int main()
{
	//freopen ("in.txt", "r", stdin);
	int i,j,sum=0,m,result=0;
	while(scanf("%d" , &m) != 0)
	{
		sum=0,result=0;
		for(i = 1; i<m; i ++)
		{
			sum = sum + i;
			if(sum < m)		result = i*i + result;
			if(sum == m )		
			{
				result = i*i + result;
				printf("%d %d\n" , m ,result);
				break;
			}
			if(sum > m )
			{
				result = result + (m + i - sum)*i;
				printf("%d %d\n" , m ,result);
				break;
			}
		}
	}
	return 0;
}



打表法#include <iostream>
using namespace std;
int main()
{
	//freopen ("in.txt", "r", stdin);
	int i,j,sum=0,m,result=0,a[160][3];
	for(i = 1; i<= 160; i ++)
	{
	
		sum = sum + i;
		a[i][1] = sum;
		result = result + i * i;
		a[i][2] = result;
	}
	while(scanf("%d" , &m) && m != 0)
	{
		for(i = 1; i<= 160; i ++)
		{
			if(m == a[i][1])	
			{
				printf("%d %d\n" , m , a[i][2]);
				break;
			}
			if(a[i][1] < m && m < a[i+1][1]) 
			{
				printf("%d %d\n" , m , a[i][2] + (m - a[i][1]) * (i+1));
				break;
			}
		}
	}
	return 0;
}

	




Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator