79 条题解
- 
  2hxtis2b LV 10 @ 2016-10-23 10:42:38 首项的负号是个巨坑 
 c++
 #include<iostream>
 #include<cmath>
 #include<cstdio>
 #pragma warning (disable:4996)
 #define MAXN 500
 #define nN 8
 using namespace std;
 int main()
 {
 int nA[MAXN], i;
 bool bCan, bZero;
 while (~scanf("%d", &nA[nN]))
 {
 bZero = true;
 for (i = nN - 1; i >= 0; i--)
 {
 scanf("%d", &nA[i]);
 if (nA[i] != 0)
 bZero = false;
 }
 bCan = false;
 if (bZero)
 cout << 0 << endl;
 else
 {
 for (i = nN; i >= 0; i--)
 {
 if (nA[i] == 0)
 continue;
 if (nA[i] < 0)
 {
 if (!bCan)
 cout << "-";
 else
 cout << " - ";
 bCan = true;
 nA[i] = abs(nA[i]);
 }
 else
 {
 if (bCan)
 {
 cout << " + ";
 bCan = true;
 }
 }
 if ((nA[i] != 1) || (i == 0))
 {
 cout << nA[i];
 bCan = true;
 }
 if (i > 1)
 {
 cout << "x^" << i;
 bCan = true;
 }
 else
 {
 if (i == 1)
 {
 cout << "x";
 bCan = true;
 }
 }
 }
 cout << endl;
 }
 }
 return 0;
 }
 
- 
  1@ 2025-03-28 13:09:42c++ 
 #include<iostream>
 #include<cmath>
 #include<cstdio>
 #pragma warning (disable:4996)
 #define MAXN 500
 #define nN 8
 using namespace std;
 int main()
 {
 int nA[MAXN], i;
 bool bCan, bZero;
 while (~scanf("%d", &nA[nN]))
 {
 bZero = true;
 for (i = nN - 1; i >= 0; i--)
 {
 scanf("%d", &nA[i]);
 if (nA[i] != 0)
 bZero = false;
 }
 bCan = false;
 if (bZero)
 cout << 0 << endl;
 else
 {
 for (i = nN; i >= 0; i--)
 {
 if (nA[i] == 0)
 continue;
 if (nA[i] < 0)
 {
 if (!bCan)
 cout << "-";
 else
 cout << " - ";
 bCan = true;
 nA[i] = abs(nA[i]);
 }
 else
 {
 if (bCan)
 {
 cout << " + ";
 bCan = true;
 }
 }
 if ((nA[i] != 1) || (i == 0))
 {
 cout << nA[i];
 bCan = true;
 }
 if (i > 1)
 {
 cout << "x^" << i;
 bCan = true;
 }
 else
 {
 if (i == 1)
 {
 cout << "x";
 bCan = true;
 }
 }
 }
 cout << endl;
 }
 }
 return 0;
 }
- 
  1@ 2015-04-14 20:22:3620+行数AC程序,比较容易理解的,其实注意题目说的双目运算符格式和无多余空格就行了 编译成功 测试数据 #0: Accepted, time = 0 ms, mem = 244 KiB, score = 10 
 测试数据 #1: Accepted, time = 0 ms, mem = 244 KiB, score = 10
 测试数据 #2: Accepted, time = 0 ms, mem = 248 KiB, score = 10
 测试数据 #3: Accepted, time = 0 ms, mem = 248 KiB, score = 10
 测试数据 #4: Accepted, time = 0 ms, mem = 244 KiB, score = 10
 测试数据 #5: Accepted, time = 0 ms, mem = 248 KiB, score = 10
 测试数据 #6: Accepted, time = 0 ms, mem = 248 KiB, score = 10
 测试数据 #7: Accepted, time = 0 ms, mem = 244 KiB, score = 10
 测试数据 #8: Accepted, time = 0 ms, mem = 244 KiB, score = 10
 测试数据 #9: Accepted, time = 0 ms, mem = 248 KiB, score = 10
 Accepted, time = 0 ms, mem = 248 KiB, score = 100#include<stdio.h> 
 int main()
 {
 int flag=0,data;
 for(int i=8;i>=0;i--)
 {
 scanf("%d",&data);
 if(data==0)
 continue;
 if(data*flag==0&&data<0)
 printf("-");
 else if(data<0)
 printf(" - ");
 if(data*flag>0)
 printf(" + ");
 if(data*data-1>0)
 printf("%d",data>0 ? data : -data);
 if(i>1)
 printf("x^%d",i);
 else if (i==1)
 printf("x");
 else if (i==0&&(data==1||data==-1))
 printf("1");
 flag=1;
 }
 if(flag==0)
 printf("0");
 return 0;
 }
- 
  0@ 2015-08-07 12:13:53分离每项来从左到右依次处理就行了,唯一要注意的就是全零时要输出0。 
 #include <iostream>
 //#include <fstream>
 #include <cmath>
 using namespace std;
 int main()
 {
 //ifstream cin("p1258.in",ios::in);
 //ofstream cout("p1258.out",ios::out);
 const int Max_top=8;
 int ratio[Max_top+1]={0},top=Max_top,is_top=1,is_zero=1;
 for(int i=Max_top;i>=0;cin>>ratio[i--]);
 for(int i=Max_top;i>=0;i--)
 {
 if(!ratio[i]){if(is_top)top--;continue;}else {is_top=0;is_zero=0;}//若系数为0,跳过该项,并处理最高位,处理零项
 if((i!=top)&&(ratio[i]>0))cout<<" + ";//若不为最高项且系数为正,输出" + "(注意符号前后有空格)
 if((ratio[i]<0)&&(i==top))cout<<'-';//若系数为负且为最高位,输出’-‘ (此符号不是双元运算符,前后无空格)
 if((ratio[i]<0)&&(i!=top))cout<<" - ";//若系数为负且不为最高位,输出" - "(此符号是双元运算符,前后有空格)
 if((abs(ratio[i])>1)||(!i))cout<<abs(ratio[i]);//若系数不为1、-1,或系数为0,直接输出系数(不带符号)
 if(abs(i)>0)cout<<'x';//若次数不为0,输出’x'
 if(i>1)cout<<'^'<<i;//若次数不为0、1,输出‘^'+次数
 }
 if(is_zero)cout<<'0';
 //cout<<endl;
 //cin.close();
 //cout.close();
 return 0;
 }
- 
  0@ 2014-11-03 23:53:41来个简洁点的代码,我交了4次才过。。。。。 
 #include<iostream>
 #include<cstdio>
 #include<cstring>
 using namespace std;
 int n,a[102],t=0;
 bool judge0(int a[],int n)
 {
 for(int i=0;i<n;i++)
 if(a[i]!=0) return false;
 return true;
 }
 int main()
 {
 n=8;
 for(int i=0;i<=n;i++) cin>>a[i];
 for(int i=0;i<=n;i++)
 {
 if(a[i]==0) continue;
 if(judge0(a,i)==true)
 {
 if(a[i]<0) cout<<"-";
 if(abs(a[i])!=1) cout<<abs(a[i]);
 }
 else
 {
 if(a[i]<0) cout<<" - ";
 else cout<<" + ";
 if(abs(a[i])!=1) cout<<abs(a[i]);
 }
 if(i<n-1) cout<<"x"<<"^"<<n-i;
 else if(i==n-1) cout<<"x";
 else if(i==n) if(abs(a[i])==1) cout<<abs(a[i]);
 }
 if(judge0(a,9)==true) cout<<0;
 cout<<endl;
 return 0;
 }
- 
  0@ 2013-04-25 12:53:24program P1258; 
 var A:array[0..8] of longint;
 i,j:longint;
 function Pto(s:longint):ansistring;
 begin
 if s<0 then Pto:=' - ' else Pto:=' + ';
 end;
 begin
 for i:=8 downto 0 do read(A[i]);
 readln;
 for j:=8 downto 0 do
 if A[j]<>0 then break;if j=0 then begin 
 writeln(A[0]);
 halt;
 end;if j=1 then begin 
 if A[j]<>0 then begin
 if A[j]<>1 then write(A[j]);
 write('x');
 end;
 if A[0]<>0 then writeln(Pto(A[0]),abs(A[0])) else writeln;
 halt;
 end;if A[j]<>0 then begin 
 if abs(A[j])<>1 then write(A[j])
 else
 if A[j]<0 then write('-');
 write('x^',j);
 end;for i:=j-1 downto 2 do 
 if A[i]<>0 then begin
 write(Pto(A[i]));
 if abs(A[i])<>1 then write(abs(A[i]));
 write('x^',i);
 end;if A[1]<>0 then 
 if A[1]=1 then write(Pto(A[1]),'x') else write(Pto(A[1]),abs(A[1]),'x');if A[0]<>0 then writeln(Pto(A[0]),abs(A[0])) else writeln; 
 end.挺麻烦的题目,交了几遍才过。注意系数为1或-1时的处理。 
- 
  0@ 2012-08-29 19:34:32program p1258; 
 var
 i,t:longint;
 a:array[0..8]of longint;
 begin
 for i:=8 downto 0 do read(a[i]);
 t:=0;
 for i:=8 downto 1 do
 begin
 if a[i]=0 then continue;
 inc(t);
 if t=1 then
 begin
 if(a[i]1)and(a[i]-1)then write(a[i]);
 if a[i]=-1 then write('-');
 end
 else
 begin
 if a[i]>1 then write(' + ',a[i]);
 if a[i]=1 then write(' + ');
 if a[i]1 then write('^',i);
 end;
 if t=0 then
 writeln(a[0])
 else
 begin
 if a[0]>0 then writeln(' + ',a[0]);
 if a[0]
- 
  0@ 2012-08-16 17:54:37晒个C的题解,P党太多了。。。 
 #include
 #include
 int main()
 {
 int i,flag=0;
 for(i=0;i
- 
  0@ 2012-07-24 11:11:01var a:array[0..8] of integer; 
 s1:string;
 s:array[0..8] of string;
 i,j,f,l:integer;
 begin
 f:=8;l:=0;
 for i:=8 downto 0 do begin read(a[i]);s[i]:='';end;
 while a[f]=0 do dec(f);
 while a[l]=0 do inc(l);
 if f=0 then begin write(a[0]);exit;end
 else if f=-1 then begin write(0); exit;end;
 for i:=f downto l do
 begin
 str(abs(a[i]),s1);
 if s1='1' then s1:='';
 if i>1 then s[i]:=s1+'x^'+chr(i+48)
 else if i=1 then s[i]:=s1+'x';
 if i=0 then if s1='' then s[i]:='1';
 end;
 if a[f]>0 then write(s[f]);
 if a[f]0 then write(' + ',s[i]);
 if a[i]
- 
  0@ 2010-04-16 18:08:01var 
 i,j,k,m,n:longint;
 a:array[1..100] of longint;
 procedure sr;
 begin
 m:=8;
 for i:=1 to m+1 do read(a[i]);
 j:=1;
 while (a[j]=0)and(jm+2) do inc(j);
 if j=m+2 then begin writeln(0);halt; end;
 while (m>0)and(a[1]=0) do
 begin
 for i:=1 to m do a[i]:=a;
 dec(m);
 end;
 end;
 begin
 sr;
 for i:=1 to m+1 do begin
 if a[i]0 then begin
 if a[i]>0 then
 if i1 then write('+');
 if a[i]0 then
 begin
 if i=m+1 then begin
 if a[i]0 then write(a[i])
 end
 else
 if a[i]-1 then
 if a[i]1 then
 if im then write(a[i],'x^',m-i+1)
 else write(a[i],'x')
 else
 if im then write('x^',m-i+1)
 else write('x')
 else
 if im then write('-x^',m-i+1)
 else write('-x');
 end;
 end;
 end;
 end.
 秒杀。
 注意只有常数项和都是0的情况
- 
  0@ 2010-04-02 16:45:12编译通过... 
 ├ 测试数据 01:答案正确... 0ms
 ├ 测试数据 02:答案正确... 0ms
 ├ 测试数据 03:答案正确... 0ms
 ├ 测试数据 04:答案正确... 0ms
 ├ 测试数据 05:答案正确... 0ms
 ├ 测试数据 06:答案正确... 0ms
 ├ 测试数据 07:答案正确... 0ms
 ├ 测试数据 08:答案正确... 0ms
 ├ 测试数据 09:答案正确... 0ms
 ├ 测试数据 10:答案正确... 0ms
 ---|---|---|---|---|---|---|---|-
 Accepted 有效得分:100 有效耗时:0msVAR 
 I,J,K:INTEGER;
 A:ARRAY[0..9,1..2]OF INTEGER;BEGIN 
 FOR I:=8 DOWNTO 0 DO READ(A);
 J:=9;
 FOR I:=8 DOWNTO 0 DO IF A0 THEN BEGIN J:=I; BREAK; END;
 IF J=9 THEN BEGIN WRITELN(0); EXIT; END
 ELSE
 BEGIN
 FOR I:=8 DOWNTO 0 DO IF A0 THEN A:=1;
 END;
 FOR I:=8 DOWNTO 0 DO IF A0 THEN BEGIN K:=I; BREAK; END;
 FOR I:=8 DOWNTO 2 DO
 BEGIN
 IF A=0 THEN CONTINUE
 ELSE IF A=1 THEN
 BEGIN
 IF I=K THEN BEGIN IF A=1 THEN WRITE('x^',I) ELSE WRITE(A,'x^',I); END
 ELSE BEGIN IF A=1 THEN WRITE(' + x^',I) ELSE WRITE(' + ',A,'x^',I); END;
 END
 ELSE IF A=-1 THEN
 BEGIN
 IF I=K THEN BEGIN IF A=-1 THEN WRITE('-x^',I) ELSE WRITE(A,'x^',I); END
 ELSE BEGIN IF A=-1 THEN WRITE(' - x^',I) ELSE WRITE(' - ',ABS(A),'x^',I); END;
 END;
 END;
 IF (K=1) OR (K=0) THEN
 BEGIN
 IF K=1 THEN BEGIN IF (A[1,2]=1) AND (A[1,1]=1) THEN WRITE('x')
 ELSE IF (A[1,2]=-1) AND (A[1,1]=-1) THEN WRITE('-x')
 ELSE IF A[1,2]=1 THEN WRITE(A[1,1],'x')
 ELSE IF A=-1 THEN WRITE(A[1,1],'x');
 END
 ELSE IF K=0 THEN WRITELN(A[0,1]);
 END
 ELSE
 BEGIN
 IF (A[1,2]=1) AND (A[1,1]=1) THEN WRITE(' + x')
 ELSE IF (A[1,2]=-1) AND (A[1,1]=-1) THEN WRITE(' - x')
 ELSE IF A[1,2]=1 THEN WRITE(' + ',A[1,1],'x')
 ELSE IF A[1,2]=-1 THEN WRITE(' - ',ABS(A[1,1]),'x');
 IF A[0,1]>0 THEN WRITELN(' + ',ABS(A[0,1])) ELSE IF A[0,1]
- 
  0@ 2010-03-28 21:39:41担心第一个点啊。。 
 全0不是无输出
 而要输出0.
 105行超长代码。。
 还是不贴了吧
 编译通过...
 ├ 测试数据 01:答案正确... 0ms
 ├ 测试数据 02:答案正确... 0ms
 ├ 测试数据 03:答案正确... 0ms
 ├ 测试数据 04:答案正确... 0ms
 ├ 测试数据 05:答案正确... 0ms
 ├ 测试数据 06:答案正确... 0ms
 ├ 测试数据 07:答案正确... 0ms
 ├ 测试数据 08:答案正确... 0ms
 ├ 测试数据 09:答案正确... 0ms
 ├ 测试数据 10:答案正确... 0ms
 ---|---|---|---|---|---|---|---|-
 Accepted 有效得分:100 有效耗时:0ms
- 
  0@ 2010-03-28 17:18:18#include 
 #include
 #include
 #includeint first=0;//判断是否为第一项 
 int coef[9];//系数int out(int power) 
 {
 if(first==0)
 switch(coef[power])
 {
 case -1:printf("-x^%d",power);break;
 case 1:printf("x^%d",power);break;
 case 0:break;
 default:printf("%dx^%d",coef[power],power);break;
 }
 else
 switch(coef[power])
 {
 case -1:printf(" - x^%d",power);break;
 case 1:printf(" + x^%d",power);break;
 case 0:break;
 default:if(coef[power]>0) printf(" + %dx^%d",coef[power],power);
 else printf(" - %dx^%d",-coef[power],power);break;
 }
 if(coef[power]!=0) first=1;
 return 0;
 }
 int main()
 {
 int i;
 int first=0;//判断是否为第一项
 for(i=8;i>=0;i--) scanf("%d",&coef[i]);
 for(i=8;i>1;i--)//非常数项非一次项输出
 {
 out(i);
 }for(i=8;i>1;i--) 
 {
 if(coef[i]!=0) first=1;
 }if(first==0)//一次项 
 switch(coef[1])
 {
 case -1:printf("-x");break;
 case 1:printf("x");break;
 case 0:break;
 default:printf("%dx",coef[1]);break;
 }
 else
 switch(coef[1])
 {
 case -1:printf(" - x");break;
 case 1:printf(" + x");break;
 case 0:break;
 default:if(coef[1]>0) printf(" + %dx",coef[1]);
 else printf(" - %dx",-coef[1]);break;
 }
 if(coef[1]!=0) first=1;if(first==0)//常数项 
 switch(coef[0])
 {
 case -1:printf("-1");break;
 case 1:printf("1");break;
 case 0:printf("0");break;
 default:printf("%d",coef[0]);break;
 }
 else
 switch(coef[0])
 {
 case -1:printf(" - 1");break;
 case 1:printf(" + 1");break;
 case 0:break;
 default:if(coef[0]>0) printf(" + %d",coef[0]);
 else printf(" - %d",-coef[0]);break;
 }
 system("pause");
 return 0;
 }
- 
  0@ 2010-03-25 16:19:02【分析】本题虽然没有考什么高难度的算法,却出的非常绕人,如果所建立的数学模型没有进过严谨的考虑就会因为一个小错误而严重丢分。 
 我们可以把每个元素的输出分解成这几个部分
 (1)开头系数的正负符号
 (2)系数的绝对值
 (3)字母x
 (4)x的次方由此我们可以逐个寻找特殊情况,在逐一输出。值得注意的是如果多项式每一个系数都是0,我们就要结尾时再输出一个0。 Program math; 
 Var
 i,n,k:integer;
 flag:boolean;
 Begin
 readln(n);
 flag:=true;
 for i:=n downto 1 do
 begin
 read(k);
 if k=0 then continue;
 if (not flag)and(k>0) then write('+');
 if k1 then write(abs(k),'x');
 if (i1)and(i0) then write('^',i);
 flag:=false;
 end;
 read(k);
 if k0 then
 if k>0
 then begin if not flag then write('+');write(k);flag:=false;end
 else begin if k
- 
  0@ 2010-03-14 14:53:15program P1258; 
 var
 a:array [0..8] of longint;
 i,j,k:longint;
 begin
 for i:=8 downto 0 do read(a[i]);
 j:=8;
 while a[j]=0 do j:=j-1;
 if (a[j]0)and(j>=2) then
 begin
 if a[j]1) then
 begin
 write(' ');
 if a[1]>0 then write('+ ') else write('- ');
 if abs(a[1])1 then write(abs(a[1]));
 write('x');
 end;
 if (a[0]0)and(j>0) then
 begin
 write(' ');
 if a[0]>0 then write('+ ') else write('- ');
 write(abs(a[0]));
 end;
 writeln;
 end.
 第一个点错了,各位帮忙看看,谢谢
- 
  0@ 2009-11-04 22:10:16第一次交的时候出现了‘--’,强烈意识到对负数转换成字符串时未取绝对值、 
 第二次一交就AC了
 代码很长,是边做边改的
 做了 难题?水题? 后发现还是先列提纲,快些,要不越改越错program p1258; 
 var str1,strfh,strxs,strcs,strx,strk:string;
 a,i,p:longint;
 begin
 p:=0; str1:=''; strfh:=''; strxs:=''; strcs:=''; strx:='x^'; strk:=' ';
 for i:=9 downto 1 do
 begin
 read(a);
 if (p=0) and (i=1) then begin
 if a
- 
  0@ 2009-11-04 17:10:52加号两边不写空格居然也AC了。 
- 
  0@ 2009-11-02 19:44:39我shax了,,交了十次,,结果错在a【8】上。。。 
 注意倒数两项都要特殊处理。。
- 
  0@ 2009-10-30 17:19:09为什么楼下的大牛们的程序那么长呢? 
 10+行轻松AC...
 #include
 main()
 {
 int i=0,flag=0,temp;
 for(i=0;i0)printf("%d",temp>0?temp:-temp);
 if(i==7)printf("x");
 else if(8-i)printf("x^%d",8-i);
 else if(temp==-1||temp==1)printf("1");
 flag=1;
 }
 if(!flag)printf("0");
 }
- 
  0@ 2009-10-29 17:18:28感觉ZZK这题的貌似有误