首页 > 试题广场 >

一元多项式求导 (25)

[编程题]一元多项式求导 (25)
  • 热度指数:6540 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
设计函数求一元多项式的导数。(注:xn(n为整数)的一阶导数为n*xn-1。)

输入描述:
以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。


输出描述:
以与输入相同的格式输出导数多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。注意“零多项式”的指数和系数都是0,但是表示为“0 0”。
示例1

输入

3 4 -5 2 6 1 -2 0

输出

12 3 -10 1 6 0

python solution:

a, res = list(map(int, input().split())), ""
if len(a)==2 and a[1]==0:
    print("0 0")
else:
    for i in range(0, len(a), 2):
        if a[i + 1] != 0:
            res += str(a[i] * a[i + 1]) + " " + str(a[i + 1] - 1) + " "
    print(res.strip())
发表于 2017-10-11 09:14:17 回复(0)
#include<iostream>
using namespace std;
int main()
{
    int n,e,flag=0;
    while(cin>>n>>e){
        if(n*e!=0){
            if(flag) cout<<' ';
            cout<<n*e<<' '<<e-1;
            flag=1;
        }
    }
    if(!flag) cout<<"0 0";
    return 0;
}

发表于 2018-12-18 09:53:13 回复(0)
//每次输两个数n,e,注意以下几点:
//1.第一次输入没有空格,之后输入之前都需要一个空格,flag判断是否第一次
//2.如果求导系数为零,则不输出,判断语句if (n*e)
//3.如果输入的为零多项式,flag=false,则输出0 0
 #include <iostream>
using namespace std;

int main()
{
    int n, e;
    bool flag = false;
    while (cin>>n>>e)
    {
        if (n*e)
        {
            if (flag)
                cout<<" ";
            cout<<n*e<<" "<<e-1;
            flag = true;
        }
    }
    if (!flag)
        cout<<"0 0"<<endl;

    system("pause");
    return 0;
}

发表于 2018-01-26 10:21:04 回复(3)
#include <bits/stdc++.h>
using namespace std;
int main(){
    int t = 1,coef,ex;
    while(scanf("%d%d",&coef,&ex)!=EOF){
        if(ex==0&&t==1) {printf("%d %d",0,0);break;}
        else if(ex==0&&t>1) continue;
        else{
            if(t>1) printf(" ");
            printf("%d %d",coef*ex,ex-1);
        } 
        t++;
    }
    return 0;
}

发表于 2019-04-01 14:01:48 回复(0)
啥头像
python:
data = raw_input().split()
data = map(int, data)
idx = 0
while idx < len(data):
    if data[idx+1] != 0:
        print (data[idx]*data[idx+1]), data[idx+1]-1,
    idx += 2
if len(data) == 2 and data[1] == 0:
    print 0,0 


发表于 2016-01-17 20:29:08 回复(0)

#include<stdio.h>
int main(){
     int n,e,flag=0;
     while(scanf("%d%d",&n,&e)==2){
         if(n*e){
             if(flag)
                 printf(" ");
             else
                 flag=1;
             printf("%d %d",n*e,e-1); 
         }
     }
     if(!flag)
         printf("0 0");
 
}

编辑于 2019-07-04 10:08:08 回复(5)

这道题就是个坑


第一个坑:数字之间可能有多个空格 如果你是用Java切割字符串的话

第二个坑:当系数项是0的时候输出0 0  *如:3  4  -5  2  6  1  0  1  对应输出是12  3  -10  1  6  0  0 但是题目给出的输出是 12  3  -10  1  6  0

第三个坑:当系数项不是0,指数是0的时候   什么也不输出    *如:3  4  -5  2  6  1  -2  0  对应输出是12  3  -10  1  6  0  (-2  0没对应的数字输出)

第四个坑:当输出多项式是空串的时候要输出0 0  *如:输入只有 -2  0  的时候   输出空串  但是此时必须输出0  0
import java.util.ArrayList;  
import java.util.Scanner;    
public class Main{    
    public static void main(String[] args){    
        Scanner sc = new Scanner(System.in);    
        String str = sc.nextLine();               
        String[]newStr = str.split("\\s+");             //切割字符串  
        ArrayList<Integer>alist = new ArrayList<Integer>();  
        for(int i=0 ;i<newStr.length ;i+=2){  
            int j=i+1;  
            if(Integer.valueOf(newStr[i])==0){              //常数项为0  
                alist.add(0);  
                alist.add(0);  
            }  
            if(Integer.valueOf(newStr[i])!=0&&Integer.valueOf(newStr[j])==0){   //指数为0且常数项不为0  
          
            }  
            if(Integer.valueOf(newStr[i])!=0&&Integer.valueOf(newStr[j])!=0){   //  常数项和指数项都不为0  
                alist.add(Integer.valueOf(newStr[i])*Integer.valueOf(newStr[j]));  
                alist.add(Integer.valueOf(newStr[j])-1);  
            }  
        }     
        if(alist.isEmpty()){            //如果将要输出的是空字符串,那么就输出0 0   
            System.out.println("0 0");  
        }else{  
            for(int i=0 ;i<alist.size() ;i++){  
                System.out.print(alist.get(i));  
                if(i!=alist.size()-1){    
                    System.out.print(" ");  //行末不能有空格  控制空格的输出  
                }  
            }  
            System.out.println();  
        }  
          
                  
    }  
}  

发表于 2017-02-21 21:34:26 回复(2)
这个题目很简单,要注意的点就只有两个,一个是空格的的位置,第一个数前面无空格,最后一个数后面无空格,可以分为第一个数(%d %d)和之后的所有数( %d %d)两种情况来讨论,第二个注意的点就是输入空串的时候记得输出0 0
代码如下:
#include<stdio.h>
int main()
{
	int i=1;
	int a,a1;
	int b,b1;
	while (scanf("%d%d", &a, &b) != EOF)
	{
		a1 = a*b;
		b1 = b - 1;
			
		if (i)		
		{			
			if (b1 != -1)		
			{
				printf("%d %d", a1, b1);		//第一个数前面无空格
				i = 0;
			}
		}
		else
		{
			if (b1 != -1)
				printf(" %d %d", a1, b1);		//之后的数设置前空格,这样最后一个数后面就不会有空格
		}
		if (i)
			printf("%d %d", 0, 0);
	}
}

编辑于 2017-06-16 16:49:13 回复(0)
s = list(map(int,input().split()))
ans = []
for i in range(0,len(s),2):
    if s[i] and s[i+1]:
        ans.extend([s[i]*s[i+1],s[i+1]-1])
print(' '.join(map(str,ans)) if len(ans) else '0 0')

发表于 2018-12-29 10:04:03 回复(0)
#include<bits/stdc++.h>
using namespace std;

int main() {
	int n,e,flag=0;
	while(cin>>n>>e) {
		if(n*e!=0) {
			if(flag) cout<<" ";
			cout<<n*e<<" "<<e-1;
			flag=1;
		}
	}
	if(!flag) cout<<"0 0";
	return 0;
}

发表于 2022-11-03 13:36:24 回复(1)
#include<stdio.h>
int main(int argc, char** argv){
	int c,e;	//系数,指数
	int prespace=0; //是否打印前置空格 
	int iszero=1; //是否为0多项式
	while(scanf("%d%d",&c,&e)!=EOF){
		if(c==0 || e==0) continue;
		if(prespace) printf(" ");
		prespace=1;
		iszero=0;
        printf("%d %d",c*e,e-1);
	}
	if(iszero) printf("0 0");
	return 0;
}
编辑于 2022-01-20 11:40:13 回复(0)
搞不懂为什么 -2 0 的时候要输出 0 0, 而 3 4 -5 2 6 1 -2 0 的时候不是输出 12 3 -10 1 6 0 0 0
发表于 2021-09-12 15:50:33 回复(0)


又是水题一道。。。
根据一阶导数的规则 ==x^n^(n为整数)的一阶导数为n*x^n-1^== 进行求导即可。

#include <iostream>
using namespace std;

int main() {
    int n = 0, m = 0;
    //用于记录是否是第一次输出
    bool isFirst = true;
    while (scanf("%d %d", &n, &m) != EOF) {
        if (m == 0) {
            //常数的导数为0,不输出
            continue;
        }
        //导数规则 x^n(n为整数)的一阶导数为n*x^(n-1)
        n *= m;
        m -= 1;
        if (!isFirst) {
            //非第一次输出需要输出空格间开
            printf(" ");
        }
        printf("%d %d", n, m);
        isFirst = false;
    }
    //如果所有数据读取完毕后,然后没有合格的输出,此时需要输出结果0 0
    if (isFirst) {
        printf("0 0\n");
    } else {
        printf("\n");
    }
    return 0;
}
————————————————
版权声明:本文为CSDN博主「hestyle」的原创文章,遵循 CC 4.0 BY 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://hestyle.blog.csdn.net/article/details/104763133
发表于 2020-03-09 22:26:55 回复(0)
#include<stdio.h>
(737)#include<stdlib.h>
#include<string.h>

int main()
{
    char nums[100];
    char index[10];
    int j=0;//j数字长度
    int k=0;//整型数组计数器
    int temp;
    int answers[20];
    memset(answers,-1,sizeof(answers));
    int m=0;
    gets(nums);
    for(int i=0;i<100 && nums[i] != '\0';i++)
    {

        if(nums[i] == ' ')
        {
            m=0;
            for(;m<j;m++)
            {
                index[m] = nums[i-j+m];
            }
            index[m] ='\0';
            temp = atoi(index);
            answers[k] = temp;
            k++;
            j=-1;
        }
        if(nums[i+1] == '\0')
        {
            i=i+1;
            m=0;
            for(;m<=j;m++)
            {
                index[m] = nums[i-j-1+m];
            }
            index[m] ='\0';
            temp = atoi(index);
            answers[k] = temp;
            i=i-1;

        }
        j++;
    }


    for(int s=0;s<k && answers[s] != -1 && answers[s+1] != -1  ;s+=2)
    {
        //if()
        {
            if(answers[s+1]== 0)
                answers[s]=answers[s+1]=0;
            else
            {
                answers[s] *=answers[s+1];
                answers[s+1]--;
            }



        }

    }
   for(int s=0;s<k;s +=2)
   {if(answers[s+1]!= 0 && answers[s]!= 0)
            {
                if(s+1 == k)
                    printf("%d %d",answers[s],answers[s+1]);
                else
                   printf("%d %d ",answers[s],answers[s+1]);
            }
            else
            {
                if(answers[s+1]== 0) {printf("%d %d",answers[s],answers[s+1]);break;}
                if(s==0) printf("0 0");break;
                //else
            }}
    return 0;
}
求解答!!!为什么提交了答案正确,但是有一个段错误??
发表于 2020-03-08 16:33:18 回复(0)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 10000

int main()
{
    char func[MAX] = {0};
    int res[MAX] = {0};
    while(gets(func))
    {
        int fun[MAX] = {0};
        int i = 0, j = 0;
        int temp = 1;//记录当前值的正负
        while(1)
        {
            if(func[i] == ' ' || func[i] == '\0')
            {
                if(func[i] == ' ' && func[i + 1] == ' ')
                {
                    i++;
                    continue;
                }
                if(func[i] == ' ' && func[i + 1] == '\0')
                    break;
                fun[j] *= temp;
                j++;
                temp = 1;
                if(func[i] == '\0')
                    break;
            }
            else if(func[i] == '-')
            {
                temp = -1;
            }
            else
            {
                fun[j] = fun[j] * 10 + (func[i] - '0');
            }
            i++;
        }
        /*
        for(int x = 0; x < j; x++)
            printf("%d ", fun[x]);
        printf("\n");
        */
        for(i = 0; i < j; i++)
        {
            res[i * 2] = fun[i * 2] * fun[i * 2 + 1];
            res[i * 2 + 1] = fun[i * 2 + 1] - 1;
        }
        /////////////////////////////////////
        /*for(int x = 0; x < j; x++)
            printf("%d ", res[x]);
        printf("\n");
        */
        /////////////////////////////////////
        //printf("%d\n", j);
        if(res[j - 1] == -1)
        {
            j -= 2;
            if(j <= 0)
            {
                res[0] = 0;
                res[1] = 0;
                j = 2;
            }
        }
        for(i = 0; i < j; i++)
        {
            if(i == 0)
                printf("%d", res[i]);
            else
                printf(" %d", res[i]);
        }
        printf("\n");
    }
    return 0;
}
唉,看了大佬的回答我才发现我写的太繁琐了
编辑于 2020-03-05 15:40:11 回复(0)
#include<bits/stdc++.h>
using namespace std;

int main(){
    int a,b=0;
    cin >> a;
    cin >> b;
    if(b!=0) cout << a*b << ' ' << b-1;
    else cout << 0 << ' ' << 0;
    while(cin >> a&&cin>>b){
        if(b!=0) cout << ' ' << a*b << ' ' << b-1; 
    }
    return 0;
}


发表于 2020-03-02 18:39:14 回复(0)
import java.util.ArrayList;
import java.util.Scanner;

public class Main
{
	public static void main(String[] args)
	{
		Scanner in = new Scanner(System.in);
		String buffer = in.nextLine();
		String[] strings = buffer.split("\\s+");
		ArrayList<Integer> source = new ArrayList<>();
		for (String s : strings)
		{
			source.add(Integer.parseInt(s));
		}
		ArrayList<Integer> out = new ArrayList<>();
		for (int i = 0; i < source.size(); i += 2)
		{
			if (source.get(i + 1) != 0)
			{
				if (source.get(i) == 0)
				{
					out.add(0);
					out.add(0);
				} else
				{
					out.add(source.get(i) * source.get(i + 1));
					out.add(source.get(i + 1) - 1);
				}
			}
		}
		if (out.isEmpty())
			System.out.println("0 0");
		else
		{
			for (int i = 0; i < out.size(); i++)
			{
				System.out.print(out.get(i));
				if (i != out.size() - 1)
					System.out.print(' ');
			}
		}
	}
}

发表于 2020-02-12 09:28:42 回复(0)
#include <iostream>
using namespace std;

int main() {
    int i, j;
    bool flag = 0;
    while (cin >> i >> j) {
        if (j == 0 || i == 0)continue;
        if (flag)cout << " ";
        cout << i * j << " " << j - 1;
        flag = 1;
    }
    if (!flag)cout << "0 0";
    return 0;
}

发表于 2019-04-02 17:29:23 回复(0)
#include <iostream>
using namespace std;

struct Poly
{  int coe;  int exp;
} poly[1000 + 10];

int main()
{  int len = 0;  int coe, exp;
    while (cin >> coe >> exp)
    {  poly[len].coe = coe;
        poly[len++].exp = exp;
        if (getchar() == '\n')
        { break; }  }
    for (int i = 0; i < len; ++i)
    {  if (!poly[i].exp && len == 1)
        { cout << "0 0"; continue; }
        else if (!poly[i].exp) { continue; }
        cout << poly[i].coe * poly[i].exp << " " << poly[i].exp - 1 << (i == len - 1 ? "" : " ");  }
    cout << endl;  return 0;
}

编辑于 2019-03-14 21:19:35 回复(0)
//1042.一元多项式求导
#include <iostream> 
#include <string> //c++11 std::to_string 
//string to_string(int val);
using namespace std;
int main() 
{
    int m,n,space=1;
    string out;
    while(cin>>m>>n){ 
        if(n==0){
            if(space!=1) continue; 
            else{ m=0;n=0;}
        }else{
            m=n*m;
            n=n-1;
        }
        if(space==1){out=to_string(m)+' '+to_string(n); space=0;}
        else {out=out+' '+to_string(m)+' '+to_string(n);}
    }
    cout<<out<<endl;
    return 0;
}
发表于 2019-02-15 10:44:28 回复(0)

问题信息

难度:
40条回答 20662浏览

热门推荐

通过挑战的用户

一元多项式求导 (25)