1710819070


私信TA

用户名:1710819070

访问量:15331

签 名:

好好学习

等  级
排  名 323
经  验 5301
参赛次数 6
文章发表 21
年  龄 0
在职情况 学生
学  校 贺州学院
专  业 软件工程

  自我简介:

1+1+1+1+1+1=田

解题思路:

我也醉了 这题的答案。。例如n=4的时候输入0 1 0 0 0  输出的是 +x^4,居然过了,还有就是全输入0的时候是没有输出的。不要纠结为啥不是+x^3,请问 1 前面所有的不符合的0 算多项式么?

第一个题解是不判断前面的0的,也就是会出现0 1 0 0 0,+x这样;

第二个题解是判断前面的0的,也就是输入0 1 0 0 0,输出为 x^4;

思路:

    我是按照自己的思路写的

    三个重要的判断(前,中,后)

    首先,最前面的那一位,如果是 1 ,就输出 1;如果是 -1,就输出 -1;不是 -1 ,原样输出;

    中间,如果是 ±1 ,只需输出符号;不是 ±1,原样输出;

    末尾,只需判断正负加符号即可!

注意事项:

参考代码:

第一个题解:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int d[1005],a[1005];
int main()
{
	int n, d=0, i, j=0, k;
	cin >> n;
	for(i=0;i<n+1;i++)
		cin>>a[i];
	k = n ;				
	for(i = 0; i < n+1; i++)
	{				
			if(a[i]==0) { k--;d++;continue; }	   // 0 跳过,k-1 
		 	if(i == 0 && a[i] == -1 ) //特殊 -1
		 	{		 		
		 	 	if(k!=1) {	  cout << "-x^" << k--;continue;	}
		 	 	if(k==1) {	  cout << "-x" ;continue;	}
			}			
		 	if(i == 0 && a[i] == 1 ) //特殊 1 
		 	{
		 		if(k!=1) { cout << "x^" << k--;continue;	}
		 		if(k==1) { cout << "x" ;continue;	}
			}			
			if(i == 0) //首位不是 1 的数 
			{
				if(k!=1) {	cout << a[0] << "x^" << k--;continue;}	 
				if(k==1)  {	cout << a[0] << "x" ;continue;}
			}		
			//中间的数	
			if(i > 0 && i < n && a[i] == 1) 
			{
				if(k!=1)	{	cout << "+x^" << k--;continue;	 }
				if(k==1)   	{	cout << "+x" ;continue;	 }
			}			 
			if(i > 0 && i < n && a[i] == -1) 
			{
				if(k!=1) {	cout << "-x^" << k--;continue;	}
				if(k==1) {	cout << "-x" ;continue;	}
			}
			if(i > 0 && i < n )
			{
				if(a[i] < -1)
				{
					if(k!=1) {	cout << a[i] << "x^" << k--;continue;	}
					if(k==1) {	cout << a[i] << "x" ;continue;	}
				}
				if(a[i] > 1)
				{
					if(k!=1) {	cout << "+" << a[i] << "x^" << k--;continue;	}
					if(k==1) {	cout << "+" << a[i] << "x" ;continue;	}
				}
			}	
			//最后的数字		
			if(i == n && a[i] < 0) {	cout << a[i] << endl; continue;	}
			if(i == n && a[i] > 0) {	cout << "+" << a[i] << endl; continue;	}
	}			
	return 0;
}

第二个题解:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int d[1005],a[1005];
int main()
{
	int n, d=0, i, j=0, k;
	cin >> n;
	for(i=0;i<n+1;i++)
		cin>>a[i];
	k = n ;		
	for(j=0;j<n+1;j++)	
		if(a[j]) break;	
	for(i = j; i < n+1; i++)
	{				
			if(a[i]==0) { k--;continue; }	   // 0 跳过,k-1 
		 	if(i == j && a[i] == -1 ) //特殊 -1
		 	{
		 	 	if(k!=1) {	  cout << "-x^" << k--;continue;	}
		 	 	if(k==1) {	  cout << "-x" ;continue;	}
			}			
		 	if(i == j && a[i] == 1 ) //特殊 1 
		 	{
		 		if(k!=1) { cout << "x^" << k--;continue;	}
		 		if(k==1) { cout << "x" ;continue;	}
			}			
			if(i == j) //首位不是 1 的数 
			{
				if(k!=1) {	cout << a[i] << "x^" << k--;continue;}	 
				if(k==1)  {	cout << a[i] << "x" ;continue;}
			}		
			//中间的数	
			if(i > j && i < n && a[i] == 1) 
			{
				if(k!=1)	{	cout << "+x^" << k--;continue;	 }
				if(k==1)   	{	cout << "+x" ;continue;	 }
			}			 
			if(i > j && i < n && a[i] == -1) 
			{
				if(k!=1) {	cout << "-x^" << k--;continue;	}
				if(k==1) {	cout << "-x" ;continue;	}
			}
			if(i > j && i < n )
			{
				if(a[i] < -1)
				{
					if(k!=1) {	cout << a[i] << "x^" << k--;continue;	}
					if(k==1) {	cout << a[i] << "x" ;continue;	}
				}
				if(a[i] > 1)
				{
					if(k!=1) {	cout << "+" << a[i] << "x^" << k--;continue;	}
					if(k==1) {	cout << "+" << a[i] << "x" ;continue;	}
				}
			}	
			//最后的数字		
			if(i == n && a[i] < 0) {	cout << a[i] << endl; continue;	}
			if(i == n && a[i] > 0) {	cout << "+" << a[i] << endl; continue;	}
	}			
	return 0;
}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区