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

AC代码和TLE代码,求助

Posted by sdau_085111 at 2011-01-14 22:59:32 on Problem 2509
不知道为什么TLE,时间复杂度高?还是逻辑有问题?!高手帮忙
AC代码
/* Author : yan
 * Question : POJ 2509 Peter's smokes
 * Data && Time : Friday, January 14 2011 10:21 PM
 * Compiler : gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
*/
#include<stdio.h>

int main()
{
	freopen("input","r",stdin);
	int n,k;
	int ans;
	while(scanf("%d %d",&n,&k)!=EOF)
	{
		ans=n;

		while(n>=k)
		{
			ans+=n/k;
			n=n/k+n%k;
		}

		printf("%d\n",ans);
	}
	return 0;
}

TLE代码
/* Author : yan
 * Question : POJ 2509 Peter's smokes
 * Data && Time : Friday, January 14 2011 10:21 PM
 * Compiler : gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
*/
#include<stdio.h>

int main()
{
	freopen("input","r",stdin);
	int n,k;
	int ans;
	while(scanf("%d %d",&n,&k)!=EOF)
	{
		ans=n;

		while(n>=k)
		{
			n-=k;
			ans++;
			n++;
		}
		printf("%d\n",ans);
	}
	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