| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
请教我已经考虑到:
1.-X在最先
2.没有X
3.1+-X
4.当结果是负数时候,我是这样处理的:printf("%d",(int)(answer-0.99999999))
我是用一个函数计算两边的X系数和常系数,分别存入A[][0],A[][1]
然后比较两边
if(A[0][0](第一个零代表是左边半边,1代表右边)==A[1][0])
if(A[1][0]==A[1][1])IDENTITY;
else if(A[1][0]!=A[1][1])IMPOSIBBLE
可是现在还是WA,不知道是不是我遗漏了什么,或者什么地方表述错误,希望高手帮助,我已经卡在这里太长了.
程序如下
#include <stdio.h>
char str1[300],str2[300],str[300];
int a[2][2],d[2];
void find(char *str,int i)
{
int p=0,p1=0,num,kind;
while(str[p])
{
num=0;
kind=1;
while(str[p1]=='+'||str[p1]=='-')p1++;//跳过所有的运算符号
while(str[p1]!='+'&&str[p1]!='-'&&str[p1]!='\0')//寻找下个运算符号出现前的系数和X
{
if(str[p1]=='x')
{
if(p1-1>=0)//如果X前面没有系数或者前面超界,则NUM=1;
{
if(!(str[p1-1]<='9'&&str[p1-1]>='0'))num=1;
}
else if(p1-1<0)num=1;
p1++;
kind=0;
break;
}
num=num*10+str[p1]-'0';
p1++;
}
if((str[p]=='+'&&str[p+1]=='-')||(str[p]=='-'&&str[p+1]=='+'))
{
d[kind]=-1;
p=p1;
p1++;
}
else if(str[p]=='+'||str[p]!='-')
{
d[kind]=1;
p=p1;
p1++;
}
else if(str[p]=='-')
{
d[kind]=-1;
p=p1;
p1++;
}
a[kind][i]+=num*d[kind];
}
}
void main()
{
int n,i,p,p1,p2,real;
float answer;
scanf("%d",&n);
getchar();
for(i=0;i<n;i++)
{
a[0][0]=a[1][0]=a[0][1]=a[1][1]=d[0]=d[1]=0;//X的系数是A[0][0]和A[0][1],常系数是A[1][0]和A[1][1]
gets(str);
p=p1=p2=0;
real=0;
while(str[p])
{
if(str[p]=='=')
{
str1[p1]='\0';
real=1;
p++;
continue;
}
if(real==0)str1[p1++]=str[p++];
if(real==1)str2[p2++]=str[p++];
}
str2[p2]='\0';
find(str1,0);
find(str2,1);
if(a[0][0]==a[0][1])
{
if(a[1][0]==a[1][1])printf("IDENTITY\n");
if(a[1][0]!=a[1][1])printf("IMPOSSIBLE\n");
}
else
{
answer=(float)(a[1][1]-a[1][0])/(a[0][0]-a[0][1]);
if(answer<0)printf("%d\n",(int)(answer-0.99999999999));
else printf("%d\n",(int)answer);
}
}
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator