返回信息流遇到一个编程题目,出现了问题,希望麻烦大家看看,帮忙找出错误。
杭电ACM月赛题目及源程序
题目:
The Diophantine Equation
Time Limit: 1000/500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1142 Accepted Submission(s): 149
Problem Description
We will consider a linear Diaphonic equation here and you are to find out whether the equation is solvable in non-negative integers or not.
Easy, is not it?
Input
There will be multiple cases. Each case will consist of a single line expressing the Diophantine equation we want to solve. The equation will be in the form ax + by = c. Here a and b are two positive integers expressing the co-efficient of two variables x and y.
There are spaces between:
1. “ax” and ‘+’
2. ‘+’ and “by”
3. “by” and ‘=’
4. ‘=’ and “c”
c is another integer that express the result of ax + by. -1000000<c<1000000. All other integers are positive and less than 100000. Note that, if a=1 then ‘ax’ will be represented as ‘x’ and same for by.
Output
You should output a single line containing either the word “Yes.” or “No.” for each input cases resulting either the equation is solvable in non-negative integers or not. An equation is solvable in non-negative integers if for non-negative integer value of x and y the equation is true. There should be a blank line after each test cases. Please have a look at the sample input-output for further clarification.
Sample Input
2x + 3y = 1015x + 35y = 67x + y = 0
Sample Output
Yes.No.Yes.HINT: The first equation is true for x = 2, y = 2. So, we get, 2*2 + 3*2=10.Therefore, the output should be “Yes.”
我的程序:
#include<stdio.h>
void main()
{
int a=0,b=0,c=0;
int i,j,l=0;
int flag=0,x;
char f,z[1000];
while(1)
{
f=getchar();
z[l]=f;
if(z[l]==10)
break;
l++;
}
for(i=0;i<l;i++)
{
if(z[i]=='x'&&i==0)
a=1;
if(z[i]=='x'&&i!=0)
{
for(j=0;j<i;j++)
{
a=a*10+(z[j]-48);
}
}
if(z[i]=='x')
x=i;
}
for(i=0;i<l;i++)
{
if(z[i]=='y'&&z[i-1]==' ')
b=1;
if(z[i]=='y'&&z[i-1]!=' ')
{
for(j=x+4;j<i;j++)
{
b=b*10+(z[j]-48);
}
}
}
for(i=0;i<l;i++)
{
if(z[i]=='='&&z[i+1]==' ')
{
if(z[i+2]!='-')
{
for(j=i+2;j<l;j++)
{
c=c*10+(z[j]-48);
}
}
if(z[i+2]=='-')
{
for(j=i+3;j<l;j++)
{
c=c*10+(z[j]-48);
}
c=-c;
}
}
}
if(c>=0)
{
for(i=0;i<=c/a;i=i++)
{
for(j=0;j<=(c-a*i)/b;j=j++)
{
if(a*i+b*j==c)
{
flag=1;
goto last;
}
}
}
}
last:
if(flag==1)
printf("Yes.\n\n");
else
printf("No.\n\n");
}
麻烦看看,有什么错误?比赛系统提示是“time limit exceeded”。
这是一条镜像帖。来源:北邮人论坛 / cpp / #35685同步于 2010/2/5
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
求助:一道杭电ACM月赛题目
Rvtea
2010/2/5镜像同步1 回复
订阅后,新回复会通过你的通知中心匿名送达。
1 条回复