题目:1010 一元多项式求导 (25 分)
设计函数求一元多项式的导数。(注:xn(n为整数)的一阶导数为nxn−1。)
输入格式:
以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过 1000 的整数)。数字间以空格分隔。
输出格式:
以与输入相同的格式输出导数多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。注意“零多项式”的指数和系数都是 0,但是表示为
0
。
0输入样例:
3 4 -5 2 6 1 -2 0
输出样例:
12 3 -10 1 6 0
思路:
- 这个题目还是简单的,只是要注意刚开始输入指数就是0的情况,这种情况就直接输出0 0并结束程序就好了,其他正常输入的情况遇到指数为0的就不做输出处理。不然测试点2、4会过不了。
- 还有一个略微麻烦的地方就在格式处理。
代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <sstream> 5 #include <cmath> 6 #include <algorithm> 7 #include <string> 8 #include <stack> 9 #include <queue> 10 #include <vector> 11 #include <map> 12 using namespace std; 13 14 struct mulnomial{ 15 int coef; 16 int index; 17 }nomial[1005]; 18 19 int main() 20 { 21 int cnt = 0; 22 while(1) 23 { 24 scanf("%d%d", &nomial[cnt].coef, &nomial[cnt].index); 25 if(nomial[cnt].index == 0 && cnt == 0) 26 { 27 printf("0 0"); 28 break; 29 } 30 if(nomial[cnt].index == 0 && cnt != 0) 31 break; 32 if(cnt != 0) 33 printf(" "); 34 printf("%d %d", nomial[cnt].coef * nomial[cnt].index, nomial[cnt].index-1); 35 cnt++; 36 } 37 return 0; 38 }
总结:
这题是边输入边处理边输出的题,用结构体其实做麻烦了,实际上不需要结构体直接设立两个普通整型进行输入也可以解决。而且感觉个人在细节处理上有点冗长,可以说没必要吧……。25行--31行的处理就显得有点重复。