PTA数据结构与算法题目集(中文) 7-16

PTA数据结构与算法题目集(中文)  7-16

7-16 一元多项式求导 (20 分)
 

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

输入格式:

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

输出格式:

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

输入样例:

3 4 -5 2 6 1 -2 0

输出样例:

12 3 -10 1 6 0
题目分析:要注意判断输入停止的标志 利用~scanf来判断 其它问题到没发现 (os:看了其它人的写法 发现自己写的不够灵性 代码还是一言难尽)
 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 
 4 struct PolyNode
 5 {
 6     int Coefficient; //系数
 7     int Exponent;    //指数
 8 }Polynomial[10000],Poly[10000];
 9 int N = 0;
10 int M = 0;
11 void Read()
12 {
13     int Coe, Exp;
14     while (~scanf("%d %d", &Coe, &Exp))
15     {
16         Polynomial[N].Coefficient = Coe;
17         Polynomial[N++].Exponent = Exp;
18     }
19 }
20 void Judge()
21 {
22     for (int i = 0; i < N; i++)
23     {
24         Polynomial[i].Coefficient *= Polynomial[i].Exponent;
25         Polynomial[i].Exponent--;
26         if (Polynomial[i].Coefficient != 0)
27         {
28             Poly[M].Coefficient = Polynomial[i].Coefficient;
29             Poly[M].Exponent = Polynomial[i].Exponent;
30             M++;
31         }
32     }
33 }
34 void Print()
35 {
36     for (int i = 0; i < M - 1; i++)
37         printf("%d %d ", Poly[i].Coefficient, Poly[i].Exponent);
38     printf("%d %d", Poly[M - 1].Coefficient, Poly[M - 1].Exponent);
39     
40 }
41 int main()
42 {
43     Read();
44     Judge();
45     Print();
46     return 0;
47 }
View Code

猜你喜欢

转载自www.cnblogs.com/57one/p/11622614.html