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

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

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

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

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

12 3 -10 1 6 0

题目很简单,但是有个坑,如果导数为0要输出“0 0”。。。。 另外自己的代码比别人的长好多,,感觉自己萌萌哒O(∩_∩)O~

#include <iostream>using namespace std;struct N{int c;int e;}buf[2010], ans[2010];int main(){int x, y, index = 0, k = 0;while (cin >> x >> y){buf[index].c = x;buf[index++].e = y;}for (int i = 0; i < index; i++){if (buf[i].e){ans[k].c = buf[i].c*buf[i].e;ans[k++].e = buf[i].e – 1;}}if (k == 0){cout << "0 0" << endl;return 0;}for (int i = 0; i < k; i++)cout << ans[i].c << " " << ans[i].e << (i-k+1 ? " ": "\n");return 0;}

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

穷则思变,差则思勤!没有比人更高的山没有比脚更长的路。

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

相关文章:

你感兴趣的文章:

标签云: