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

help about cog-wheels

Posted by ACInzaghi at 2007-09-05 13:36:25 on Problem 1395
this is my code. I always get the run time error, but after check, I still find nothing wrong and the test sequence can be passed. 
#include <stdio.h>
#include <math.h>
int common_divisor(int a,int b)
{
	int c;
	c = a % b;
	while(c)
	{
		a = b;
		b = c;
		c = a % b;
	}
	return b;
	
}
int main(void)
{	
	int scenarios=0,i=0,j=0,l=0,no_cog=0,cog[20]={0},ratio[10]={0},ratio_raw[10]={0},min=101,no_ratio=0;
	while(scanf("%d",&scenarios))
	{
		for (l=0;l<scenarios;l++)
		{
			scanf("%d",&no_cog);
			if (no_cog>20)
			{
				break;
			}
			for (i=0;i<no_cog;i++)
			{
				scanf("%d",&cog[i]);
				if (cog[i]>100||cog[i]<5)
				{
					return EOF;
				}
				min=(min>cog[i]?cog[i]:min);
			}
			scanf("%d",&no_ratio);
			for (i=0;i<no_ratio;i++)
			{
				scanf("%d %d",&ratio_raw[i*2],&ratio_raw[i*2+1]);
				if (ratio_raw[i*2]!=0&&ratio_raw[i*2+1]!=0)
				{
					ratio[i*2]=ratio_raw[i*2]/common_divisor(ratio_raw[i*2],ratio_raw[i*2+1]);
					ratio[i*2+1]=ratio_raw[i*2+1]/common_divisor(ratio_raw[i*2],ratio_raw[i*2+1]);
				}
			}
			for(i=0;i<no_cog;i++)
			{
				cog[i]=cog[i]/min;
			}
			printf("Scenario #%d:\n",l+1);
			for (j=0;j<no_ratio;j++)
			{
				if (ratio_raw[j*2+1]!=0&&ratio_raw[j*2]!=0)
				{
					for (i=0;i<no_cog;i++)
					{
						if (cog[i]==1)
							continue;
						else
						{
								while (ratio[j*2+1]%cog[i]==0)
									ratio[j*2+1]=ratio[j*2+1]/cog[i];
								while (ratio[j*2]%cog[i]==0)
									ratio[j*2]=ratio[j*2]/cog[i];	
						}
					}
				}
				else
				{
					goto label;
				}				
label:			if (ratio[j*2]==1 && ratio[j*2+1]==1)
					printf("Gear ratio %d:%d can be realized.\n",ratio_raw[j*2],ratio_raw[j*2+1]);
				else
					printf("Gear ratio %d:%d cannot be realized.\n",ratio_raw[j*2],ratio_raw[j*2+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