[C++]PAT乙级1010. 一元多项式求导 (25/25)

/*
1010. 一元多项式求导 (25)

设计函数求一元多项式的导数。(注:x^n(n为整数)的一阶导数为n*x^n-1。)

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

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

输入样例:
    3 4 -5 2 6 1 -2 0
输出样例:
    12 3 -10 1 6 0
*/
/*
    思路1:
        结构体辅助
    思路2:
        边输入边处理while(cin>>x>>y){...},解决长度和结束未知的问题
*/
#include<iostream>
#include<vector>
using namespace std;

int main(){
    int a,b;
    bool first=true;
    while(cin>>a>>b){
        if(a!=0&&b!=0){
            if(!first){//未到最后一项(零项式)
                cout<<" ";
            }
            cout<<a*b<<" "<<b-1;
            first = false;
        }
    }
    if (first) {
        cout<<"0 0";
    }
    return 0;
}

/*
    ↓思路2源码 15/20(存在bug:0 0用例)
*/

/*
1010. 一元多项式求导 (25)

设计函数求一元多项式的导数。(注:x^n(n为整数)的一阶导数为n*x^n-1。)

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

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

输入样例:
    3 4 -5 2 6 1 -2 0
输出样例:
    12 3 -10 1 6 0
*/
//#include<iostream>
//#include<vector>
//using namespace std;
//
//struct Item{
//    int factor;//系数
//    int index;
//    int derivative_factor;//导数系数
//    int derivative_index;//导数指数
//};
//
//int main(){
//    vector<Item>::iterator iter;
//    vector<Item> func_items;//函数多项式
//
//    Item tmp;
//    tmp.index = -1;
//    for(;tmp.index != 0;){
//        scanf("%d", &tmp.factor);
//        scanf("%d", &tmp.index);
//        if(tmp.index!=0){
//            tmp.derivative_factor = tmp.factor*tmp.index;
//            tmp.derivative_index = tmp.index - 1;
//            func_items.push_back(tmp);
//        }
//    }
//
//    //print
//    ;
//    for(int i=0,size = func_items.size();i<size;i++)
//        printf("%d %d%s", func_items[i].derivative_factor, func_items[i].derivative_index, i!=size-1?" ":"");
//
//    //printf("*");//test
//    return 0;
//}

  

原文地址:https://www.cnblogs.com/johnnyzen/p/8877910.html