1 条题解

  • 0
    @ 2021-6-15 1:39:38

    C :

    #include <stdio.h>
    
    typedef struct
    {
    	int x, y;
    } bdd;
    
    int main()
    {
    	int i, j, k, m, n;
    	bdd r[1000];
    	while (scanf("%d", &n) != EOF)
    	{
    		for (i = 0; i <= n; ++i)
    		{
    			scanf("%d", &r[i].x);
    			r[i].y = n - i;
    		}
    		if (0 == n)
    		{
    			printf("%d\n", r[0].x);
    		}
    		else
    		{
    			if (r[0].x)
    			{
    				if (1 == r[0].x)
    				{
    					printf("x^%d", r[0].y);
    				}
    				else if (-1 == r[0].x)
    				{
    					printf("-x^%d", r[0].y);
    				}
    				else
    				{
    					printf("%dx^%d", r[0].x, r[0].y);
    				}
    			}
    			for (i = 1; i < n; ++i)
    			{
    				if (r[i].x)
    				{
    					if (-1 == r[i].x)
    					{
    						printf("-x");
    						if (1 != r[i].y)
    						{
    							printf("^%d", r[i].y);
    						}
    					}
    					else if (1 == r[i].x)
    					{
    						printf("+x");
    						if (1 != r[i].y)
    						{
    							printf("^%d", r[i].y);
    						}
    					}
    					else
    					{
    						printf("%+dx", r[i].x);
    						if (1 != r[i].y)
    						{
    							printf("^%d", r[i].y);
    						}
    					}
    				}
    			}
    			if (r[n].x)
    			{
    				printf("%+d\n", r[n].x);	
    			}
    			else
    			{
    				putchar('\n');
    			}
    		}
    	}
    	return 0;
    }
    

    C++ :

    #include<iostream>
    #include<string>
    using namespace std;
    int main()
    {
    	int n; string str="x^";
    	while (cin>>n)
    	{
    		int first=1;
    		while (n!=-1)
    		{
    			int a; cin>>a;
    			if ( first && a )//1
    			{
    				if (a<0) { cout<<"-";a=-a; }
    				if (a==1) cout<<str<<n;
    				else cout<<a<<str<<n; 
    				goto end;
    			} 
    			if ( n==1 && a)
    			{
    				if (a<0) { cout<<"-";a=-a; } else cout<<"+";
    				if (a==1) cout<<"x";
    				else cout<<a<<"x";goto end;
    			}
    			if ( n==0 && a)
    			{
    				if (a>0) cout<<"+"; cout<<a; goto end;
    			}
    			if (a)
    			{
    				if (a<0) { cout<<"-";a=-a; } else cout<<"+";
    				if (a==1) cout<<str<<n;
    				else cout<<a<<str<<n;
    				goto end;
    			}
    			
    		end:	first=0;n--;
    		}
    		cout<<endl;
    	}
    	return 0;
    } 
    

    Pascal :

    var i,j,m,n,k,ans:longint;
    begin
      readln(n);
      for i:=n downto 0 do
      begin
        read(k);
        if k=0 then continue ;
        if k<0 then write('-');
        if (k>0) and (i<>n) then write('+');
        if (k<>1) and (k<>-1) or (i=0) then write(abs(k));
        if i>1 then write('x^',i);
        if i=1 then write('x');
      end;
    end.
    

    Java :

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    public class Main {
    	public static void main(String args[]) throws Exception {
    		// 输入范围
    		Scanner scanner = new Scanner(System.in);
    		String input = scanner.nextLine();
    		Integer n = Integer.parseInt(input);
    
    		input = scanner.nextLine();
    		String[] array = input.split(" ");
    		List<Integer> numList = new ArrayList<Integer>();
    		for (int i = 0; i < array.length; i++) {
    			numList.add(Integer.parseInt(array[i]));
    		}
    
    		String output = "";
    		int head = 0;
    
    		for (int i = 0; i < numList.size(); i++) {
    
    			if (numList.get(i) <= -2) {
    				output += String.valueOf(numList.get(i));
    			} else if (numList.get(i) == -1) {
    				if (n == 0) {
    					output += "-1";
    				} else {
    					output += "-";
    				}
    			} else if (numList.get(i) == 0) {
    				head++;
    			} else if (numList.get(i) == 1) {
    				if (n == 0) {
    					if (i == head) {
    						output += "1";
    					} else {
    						output += "+1";
    					}
    				} else {
    					if (i != head) {
    						output += "+";
    					}
    				}
    			} else if (numList.get(i) > 1) {
    				if (i != head) {
    					output += "+";
    				}
    				output += numList.get(i);
    			}
    
    			if (numList.get(i) != 0) {
    				if (n != 0) {
    					output += "x";
    				}
    			}
    
    			if (numList.get(i) != 0) {
    				if (n > 1) {
    					output += "^" + n;
    				}
    			}
    			n--;
    		}
    
    		System.out.println(output);
    
    	}
    }
    
    
    • 1

    信息

    ID
    268
    时间
    1000ms
    内存
    125MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者