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

高手帮我看下代码·本机测试数据都没错,提交上去就是wa 谢谢

Posted by lnmm at 2007-09-03 13:26:11 on Problem 1032
我的MSN/Email: mars1021@163.com
做法:

求出以2起始的最大连续自然数序列之和sum,使得sum的值不超过输入数n, 
然后分情况讨论: 
设此最大序列为2、3、……、w,则: 
1.若剩余值(n-sum)等于w,则最后输出序列为:3、4、……、w、w+2,即将原最大序列每项加1,再将最后剩余的一个1加到最后一项上。 
2.若剩余值(n-sum)小于w,则从序列的最大项i开始,从大到小依次将每项加1,直到剩余值用完。


#include<stdio.h>
void main()
{
	int n,i,j;
	int a[20000];
	int sum;
	scanf("%d",&n);
	sum=a[1]=2;
	for(i=2;sum<=n;i++)
	{
		a[i]=a[i-1]+1;
		sum=sum+a[i];
	}
	i--;
	sum=sum-a[i];
	i--;
	if(a[i]==n-sum)
	{
		for(j=1;j<=i;j++)
			a[j]++;
		    a[j]++;      
	}
  else	if(a[i]>n-sum)
		for(j=1;j<=n-sum;j++)
			a[i-j+1]++;       			
	  			
  for(j=1;j<=i-1;j++)
	  printf("%d ",a[j]);
 printf("%d ",a[j+1]);
}

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