多项式输出

 

 tips:对于100%数据,0 ≤n≤100,−100≤系数<=100

将符号、系数、指数分开输出

#include <stdio.h>
#include <stdlib.h>
#include <string.h> 
int main()
{  
	int n;
	int len=0;
	scanf("%d",&n); 
	 
	int a[105];
	for(int i=n;i>=0;i--)
		scanf("%d",&a[i]) ;
	
	//由于有0次幂,因此特别构造这样的循环
	for(int i=n;i>=0;i--)
	{ 
		int ax =a[i]; 
		//正负号
		if(ax==0)		//系数为0,不输出
		{
			continue;
		}
		else if(ax>0)
		{
			if(i!=n)	//n次项,位于开始,不需要+
				printf("+"); 
		} 
		else	//负号跟随系数即可
		{
			;
		}
		
		//系数
		if(i==0)	//0次幂,都要输出
		{
			printf("%d",ax); 
		}
		else
		{
			if(ax==1)
				;
			else if(ax==-1)
				printf("-");
			else 
				printf("%d",ax);  
		}  
		
		if(i>1)	//如果不是0次幂,正常输出
			printf("x^%d",i);  
		else if(i==1)
			printf("x");
		else
			; 
	} 
	printf("\n");
    return  0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h> 
int main()
{
    int n=0;
    scanf("%d",&n);
    int a[n+1];//里面赋值为n不能初始化
    int b=n+1;
    int tmp=200;
   for (int  i = 0; i <= n; i++)
   {
    scanf("%d",&a[i]);
    if (a[i]!=0&&tmp>i)
    {
        tmp=i;
        //printf("%d\n",tmp);
    }
    
   }
   for (int i = tmp; i <= n; i++)
   {
        if (i==tmp&&a[tmp]>0)
        {
            if (a[tmp]>1)
            {
                printf("%dx^%d",a[tmp],b-1);
            }
            else
            {
                printf("x^%d",b-1);
            }
            
        }
        else if(i==tmp&&a[tmp]<0)
        {
            if (a[tmp]==-1)
            {
                printf("-x^%d",b-1);
            }
            else
            {
                printf("%dx^%d",a[tmp],b-1);
            }
            
        }
        else if (tmp<i&&i<n-1)
        {
            if (a[i]==-1)
            {
                printf("-x^%d",b-i-1);
            }
            else if (a[i]==1)
            {
                printf("+x^%d",b-i-1);
            }
            else if (a[i]>1)
            {
                printf("+%dx^%d",a[i],b-i-1);
            }
            else if (a[i]==0)
            {
                ;
            }
            else
            {
                printf("%dx^%d",a[i],b-i-1);
            }
        }
        else if (i==n-1)
        {
            if (a[i]==-1)
            {
                printf("-x");
            }
            else if (a[i]==1)
            {
                printf("+x");
            }
            else if (a[i]>1)
            {
                printf("+%dx",a[i]);
            }
            else if (a[i]==0)
            {
                ;
            }
            else
            {
                printf("%dx",a[i]);
            }
        }
        else
        {
           
            if (a[i]>0)
            {
                printf("+%d",a[i]);
            }
            else if (a[i]==0)
            {
                ;
            }
            else
            {
                printf("%d",a[i]);
            }
        }
   }
   
   
    
    return 0;
}
  1. 将多项式分为 系数+两部分
  2. 考虑正负号(包括第一个为正数时)
  3. 考虑x^1, x^0的特殊性(系数,格式)
  4. 考虑系数为1,-1,0时的特殊性

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值