浙大 pat Basic Level 1010

来源:互联网 发布:长沙网站整站优化 编辑:程序博客网 时间:2024/04/29 01:07

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

设计函数求一元多项式的导数。

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

输出格式:以与输入相同的格式输出导数多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。

输入样例:
3 4 -5 2 6 1 -2 0
输出样例:
12 3 -10 1 6 0

//注意到我注释掉的部分,这个测试的程序是用"EOF"(ctrl+z)作为结束的//注意一个测试  输入"9 0" 输出"0 0"//无需按照指数递降的顺序重新整理结果#include <iostream>#include <vector>//#include <stdio.h>using namespace std;int main(){vector<int> num;int coefficient,exponent;while(cin >> coefficient >> exponent){num.push_back(coefficient);num.push_back(exponent);}//vector<int> num;//char temp;//temp = getchar();//while(temp != '\n')//{//int tempnum = 0;//if('-' == temp)//{//temp = getchar();//while((temp != ' ')&&(temp != '\n'))//{//tempnum = tempnum*10 + (temp - '0');//temp = getchar();//}//tempnum = -tempnum;//}//else//{//while((temp != ' ')&&(temp != '\n'))//{//tempnum = tempnum*10 + (temp - '0');//temp = getchar();//}//}//num.push_back(tempnum);//if (temp != '\n')//{//temp = getchar();//}//}//用上面注释掉的代码的话,需要依次输入(ctrl+z)、enter、enter才会输出结果//修改后,只需要输入(ctrl+z)、enter,就会输出结果bool Firstone = true;for(vector<int>::size_type i = 1; i < num.size(); i = i+2){if (0 != num[i]*num[i-1]){if(Firstone){cout << num[i-1]*num[i] << ' '<< num[i]-1;Firstone = false;}elsecout << ' ' <<  num[i-1]*num[i] << ' ' << num[i]-1;}}if(Firstone)cout << "0 0"; cout << endl;system("pause");return 0;}


0 0
原创粉丝点击