讨论 / 神牛们,看一下我的程序吧!
lsw1997 2010-10-05 03:17:00
点我顶贴 收藏 删除

我是80分呀,各位神牛拜托看下,别贴标程!

#include<iostream>

using namespace std;

int a[1000],n,s;

int work1()

{ if(a[n-1]==0){s--;return 0;}

if(a[n-1]<-1){cout<<a[n-1]<<'x';return 0;}

if(a[n-1]=-1){cout<<"-x";return 0;}

if(a[n-1]>1){cout<<'+'<<a[n-1]<<'x';return 0;}

if(a[n-1]=1){cout<<'+'<<'x';return 0;}

return 0;

}

int work()

{ if(a[0]==0){s--;}

else{ if(a[0]!=1&&a[0]!=-1){cout<<a[0]<<'x'<<'^'<<s;s--;}

if(a[0]==1){cout<<'x'<<'^'<<s;s--;}

if(a[0]==-1){cout<<"-x"<<'^'<<s;s--;} }

for(int i=1;i<n-1;i++)

{

if(a[i]==0){s--; continue;}

else

{ if(a[i]==1){cout<<'+'<<'x'<<'^'<<s;s--;continue;}

if(a[i]>0&&a[i]!=1){cout<<'+'<<a[i]<<'x'<<'^'<<s;s--;continue;}

if(a[i]==-1){cout<<'-'<<'x'<<'^'<<s;s--;continue;}

if(a[i]<0&&a[i]!=1){cout<<a[i]<<'x'<<'^'<<s;s--;}

}

}work1();

if(a[n]==0) return 0;

if(a[n]>0){cout<<'+'<<a[n]; return 0;}

if(a[n]<0){cout<<a[n];return 0;}

return 0;

}

int main()

{

cin>>n;

s=n;

for(int i=0;i<=n;i++)

{

cin>>a[i];

}

work();

return 0;

}

得分: 80分

测试结果3: 测试结果错误.错误结果为:-x^99-7x^50+100x^2-x+8

正确结果应为:-x^99-7x^50+100x^2+x+8

测试结果10: 测试结果错误.错误结果为:-x^99+5x^98-4x^97-2x^96+x^95-5x^94+3x^93+4x^92-x^91+4x^90-5x^89-5x^88+4x^87-x^86+3x^85+2x^84-x^83-2x^82+3x^81-4x^80+x^79+x^78-5x^77-2x^76-x^75+2x^74+x^73-4x^72-x^71-x^70-2x^69-x^68+5x^67-3x^66+4x^65+x^64+2x^63+2x^62+2x^61-2x^60-x^59+3x^58-x^57-2x^56+5x^54-4x^53-3x^52-x^51+3x^50+2x^49-5x^48-3x^47-3x^46-3x^45+2x^44+2x^42-3x^41+x^40+5x^39+2x^37-2x^36-3x^34-3x^33-3x^32-5x^31-4x^30-4x^29-3x^28+3x^27+x^26+x^25+2x^24+2x^23-x^22-5x^20-4x^19-3x^17-5x^16-2x^14-2x^13-2x^12+4x^11-x^10-x^9+2x^8-5x^7+4x^6+x^5-5x^4+2x^3+x^2-x-3

正确结果应为:-x^99+5x^98-4x^97-2x^96+x^95-5x^94+3x^93+4x^92-x^91+4x^90-5x^89-5x^88+4x^87-x^86+3x^85+2x^84-x^83-2x^82+3x^81-4x^80+x^79+x^78-5x^77-2x^76-x^75+2x^74+x^73-4x^72-x^71-x^70-2x^69-x^68+5x^67-3x^66+4x^65+x^64+2x^63+2x^62+2x^61-2x^60-x^59+3x^58-x^57-2x^56+5x^54-4x^53-3x^52-x^51+3x^50+2x^49-5x^48-3x^47-3x^46-3x^45+2x^44+2x^42-3x^41+x^40+5x^39+2x^37-2x^36-3x^34-3x^33-3x^32-5x^31-4x^30-4x^29-3x^28+3x^27+x^26+x^25+2x^24+2x^23-x^22-5x^20-4x^19-3x^17-5x^16-2x^14-2x^13-2x^12+4x^11-x^10-x^9+2x^8-5x^7+4x^6+x^5-5x^4+2x^3+x^2+4x-3

#1 forgot@2010-07-04 23:52:00
回复 删除
你的情况考虑不周全

根据你的程序,我大改了一下(你的程序太冗杂了):

#include <iostream> //TRW 2010年03月12日 13:20:40

using namespace std;

int main()

{

int i,n,x,t;

cin>>n;

x=n;

cin>>t;

if ((t!=1)&&(t!=-1)&&t)

cout<<t<<"x^"<<x;

else if ((t==-1)||(t==1))

if (t==1)

cout<<"x^"<<x;

else cout<<"-x^"<<x;

x--;

for (i=1;i<=n;i++)

{cin>>t;

if (x==0)

{if (t!=0)

{if ((t!=1)&&(t!=-1))

{if (t>0)

{cout<<'+'<<t;x--;continue;}

if (t<0)

{cout<<t;x--;continue;}

}

if (t==1)

{cout<<'+'<<t;x--;continue;}

if (t==-1)

{cout<<t;x--;continue;}

}

}

if (x==1)

{if ((t!=1)&&(t!=-1))

{if (t==0)

{x--;continue;}

if (t>0)

{cout<<'+'<<t<<'x';x--;continue;}

if (t<0)

{cout<<t<<'x';x--;continue;}

}

else {if (t==1)

{cout<<"+x";x--;continue;}

if (t==-1)

{cout<<"-x";x--;continue;}

}

}

if (x!=1)

{if ((t!=1)&&(t!=-1))

{if (t==0)

{x--;continue;}

if (t>0)

{cout<<'+'<<t<<"x^"<<x;x--;continue;}

if (t<0)

{cout<<t<<"x^"<<x;x--;continue;}

}

else {if (t==1)

{cout<<"+x^"<<x;x--;continue;}

if (t==-1)

{cout<<"-x^"<<x;x--;continue;}

}

}

x--;

}

return 0;

}

#2 noip2012@2010-07-05 00:31:00
回复 删除
我不是神牛

这个程序比较长,而且是PASCAL的,不过个人认为条理还比较清楚

如果您能看懂就看吧

var

a:array[0..100] of integer;

st,st1,ans:ansistring;

i,max:integer;

begin

readln(max);

for i:=max downto 0 do read(a[i]);

readln;

ans:='';

if a[max]<>0 then

begin

str(a[max],st);

str(max,st1);

if a[max]=1 then

ans:=ans+'x^'+st1

else

if a[max]=-1 then

ans:=ans+'-x^'+st1

else

ans:=ans+st+'x^'+st1;

end;

for i:=max-1 downto 2 do

if a[i]<>0 then

begin

str(a[i],st);

str(i,st1);

if a[i]>0 then

if a[i]=1 then

if ans='' then

ans:=ans+'x^'+st1

else

ans:=ans+'+x^'+st1

else

if ans='' then

ans:=ans+st+'x^'+st1

else

ans:=ans+'+'+st+'x^'+st1

else

if a[i]=-1 then

ans:=ans+'-x^'+st1

else

ans:=ans+st+'x^'+st1;

end;

if a[1]<>0 then

begin

str(a[1],st);

if a[1]>0 then

if a[1]=1 then

if ans='' then

ans:=ans+'x'

else

ans:=ans+'+x'

else

if ans='' then

ans:=ans+st+'x'

else

ans:=ans+'+'+st+'x'

else

if a[1]=-1 then

ans:=ans+'-x'

else

ans:=ans+st+'x';

end;

if a[0]<>0 then

begin

str(a[0],st);

if ans='' then

ans:=ans+st

else

if a[0]>0 then

ans:=ans+'+'+st

else

ans:=ans+st;

end;

if ans='' then ans:='0';

writeln(ans);

end.

#3 lsw1997@2010-07-05 00:55:00
回复 删除
回复 沙发forgot 的帖子

童仁伟,偶已ac了,不过,分给你吧!

#4 wanghao1996@2010-08-07 21:33:00
回复 删除
童仁伟你的程序太冗杂了

#include<iostream>

using namespace std;

int main(void)

{ int n;

cin>>n;

for(int i=1;i<=n+1;i++)

{int x;

cin>>x;

if(i!=1&&x!=0)

if(x>0)cout<<'+';

if(x==-1)cout<<'-';

if(x!=-1&&x!=0&&x!=1)cout<<x;

if(i==n+1&&x==1)cout<<1;

if(i==n+1&&x==-1)cout<<1;

if(i!=n+1&&x!=0&&i!=n)cout<<"x^"<<n-i+1;

if(i==n&&x!=0)cout<<'x';}

return 0;

}

#5 Ce-Peter@2010-10-05 03:17:00
回复 删除
第十题答案有错?看看我的Pascal

Program poly;

Var

n,i:byte;

x:array[0..100] of integer;

Procedure wt(j:byte);

Begin

If (j<>0) And (j<>1) And (j<>n) Then

Begin

If (Abs(x[j])<>1) And (Abs(x[j])<>0) Then

If x[j]<0 Then Write(x[j],'x^',j) Else Write('+',x[j],'x^',j);

If Abs(x[j])=1 Then If x[j]<0 Then Write('-x^',j) Else Write('+x^',j);

End;

If (j=0) And (x[j]<>0) Then If x[j]<0 Then Write(x[j]) Else Write('+',x[j]);

If (j=1) And (Abs(x[j])=1) Then

If x[j]>0 Then Write('+x') Else Write('-x');

If (j=1) And (x[j]<>0) And (Abs(x[j])<>1) Then Write(x[j],'x');

If (j=n) And (Abs(x[j])=1) Then

If x[j]>0 Then Write('x^',j) Else Write('-x^',j);

If (j=n) And (x[j]<>0) And (Abs(x[j])<>1) Then Write(x[j],'x^',j);

End;

Begin

read(n);

For i:=n Downto 0 Do Read(x[i]);

For i:=n Downto 0 Do wt(i);

End.

测试结果错误.错误结果为:-x^99+5x^98-4x^97-2x^96+x^95-5x^94+3x^93+4x^92-x^91+4x^90-5x^89-5x^88+4x^87-x^86+3x^85+2x^84-x^83-2x^82+3x^81-4x^80+x^79+x^78-5x^77-2x^76-x^75+2x^74+x^73-4x^72-x^71-x^70-2x^69-x^68+5x^67-3x^66+4x^65+x^64+2x^63+2x^62+2x^61-2x^60-x^59+3x^58-x^57-2x^56+5x^54-4x^53-3x^52-x^51+3x^50+2x^49-5x^48-3x^47-3x^46-3x^45+2x^44+2x^42-3x^41+x^40+5x^39+2x^37-2x^36-3x^34-3x^33-3x^32-5x^31-4x^30-4x^29-3x^28+3x^27+x^26+x^25+2x^24+2x^23-x^22-5x^20-4x^19-3x^17-5x^16-2x^14-2x^13-2x^12+4x^11-x^10-x^9+2x^8-5x^7+4x^6+x^5-5x^4+2x^3+x^24x-3

正确结果应为:-x^99+5x^98-4x^97-2x^96+x^95-5x^94+3x^93+4x^92-x^91+4x^90-5x^89-5x^88+4x^87-x^86+3x^85+2x^84-x^83-2x^82+3x^81-4x^80+x^79+x^78-5x^77-2x^76-x^75+2x^74+x^73-4x^72-x^71-x^70-2x^69-x^68+5x^67-3x^66+4x^65+x^64+2x^63+2x^62+2x^61-2x^60-x^59+3x^58-x^57-2x^56+5x^54-4x^53-3x^52-x^51+3x^50+2x^49-5x^48-3x^47-3x^46-3x^45+2x^44+2x^42-3x^41+x^40+5x^39+2x^37-2x^36-3x^34-3x^33-3x^32-5x^31-4x^30-4x^29-3x^28+3x^27+x^26+x^25+2x^24+2x^23-x^22-5x^20-4x^19-3x^17-5x^16-2x^14-2x^13-2x^12+4x^11-x^10-x^9+2x^8-5x^7+4x^6+x^5-5x^4+2x^3+x^2+4x-3

查看更多回复
提交回复