周赛一 ACdream 1204 模拟
2015-08-09 20:48
309 查看
Suppose there are a polynomial which has n nonzero terms, please print the integration polynomial of the given polynomial.
The polynomial will be given in the following way, and you should print the result in the same way:
k[1] e[1] k[2] e[2] ... k
e
where k[i] and e[i] respectively represent the coefficients and exponents of nonzero terms, and satisfies e[1] < e[2] < ... < e
.
Note:
Suppose that the constant term of the integration polynomial is 0.
If one coefficient of the integration polynomial is an integer, print it directly.
If one coefficient of the integration polynomial is not an integer, please print it by using fraction a/b which satisfies thata is coprime to b.
Input
There are multiple cases.
For each case, the first line contains one integer n, representing the number of nonzero terms.
The second line contains 2*n integers, representing k[1], e[1], k[2], e[2], ..., k
, e
。
1 ≤ n ≤ 1000
-1000 ≤ k[i] ≤ 1000, k[i] != 0, 1 ≤ i ≤ n
0 ≤ e[i] ≤ 1000, 1 ≤ i ≤ n
Output
Print the integration polynomial in one line with the same format as the input.
Notice that no extra space is allowed at the end of each line.
Sample Input
Sample Output
Hint
f(x) = 1 + 3x2 + 2x4
After integrating we get: ∫f(x)dx = x + x3 + (2/5)x5
The polynomial will be given in the following way, and you should print the result in the same way:
k[1] e[1] k[2] e[2] ... k
e
where k[i] and e[i] respectively represent the coefficients and exponents of nonzero terms, and satisfies e[1] < e[2] < ... < e
.
Note:
Suppose that the constant term of the integration polynomial is 0.
If one coefficient of the integration polynomial is an integer, print it directly.
If one coefficient of the integration polynomial is not an integer, please print it by using fraction a/b which satisfies thata is coprime to b.
Input
There are multiple cases.
For each case, the first line contains one integer n, representing the number of nonzero terms.
The second line contains 2*n integers, representing k[1], e[1], k[2], e[2], ..., k
, e
。
1 ≤ n ≤ 1000
-1000 ≤ k[i] ≤ 1000, k[i] != 0, 1 ≤ i ≤ n
0 ≤ e[i] ≤ 1000, 1 ≤ i ≤ n
Output
Print the integration polynomial in one line with the same format as the input.
Notice that no extra space is allowed at the end of each line.
Sample Input
3 1 0 3 2 2 4
Sample Output
1 1 1 3 2/5 5
Hint
f(x) = 1 + 3x2 + 2x4
After integrating we get: ∫f(x)dx = x + x3 + (2/5)x5
#include<iostream> #include<algorithm> #include<cstring> using namespace std; struct node { int data; int num; } p[10000]; //int cmp(node p1,node p2) //{ // return p1.num<p2.num; //} int gcd(int a,int b) { return b?gcd(b,a%b):a; } int main() { int n; while(cin>>n) { for(int i=1; i<=n; i++) { cin>>p[i].data>>p[i].num; } // sort(p,p+n,cmp); //不用排序 int kk=1; for(int i=1; i<=n; i++) { if(kk==1) kk=0; else cout<<" "; if(p[i].data>=0) { if(p[i].data%(p[i].num+1)==0) cout<<p[i].data/(p[i].num+1)<<" "<<p[i].num+1; else { int a=gcd(p[i].data,p[i].num+1); // cout<<a<<endl; cout<<p[i].data/a<<"/"<<(p[i].num+1)/a<<" "<<p[i].num+1; } } else { p[i].data*=-1; if(p[i].data%(p[i].num+1)==0) cout<<-1*p[i].data/(p[i].num+1)<<" "<<p[i].num+1; else { int a=gcd(p[i].data,p[i].num+1); cout<<-1*p[i].data/a<<"/"<<(p[i].num+1)/a<<" "<<p[i].num+1; } } } cout<<endl; } } //1.注意p[i].data为负数时 //2. p[i].data与p[i].num可约分时
相关文章推荐
- LightOJ 1269 Consecutive Sum(字典树)
- 【leetcode】【Single Number题目】java 异或运算解决数字出现偶数次还是奇数次问题||HashMap
- 死锁,死锁产生的原因,产生死锁的必要条件和解决死锁的方法
- Cookie V.S. Session
- Ruby操作数据时需要掌握的基础知识
- Android网络编程
- POJ2449 Remmarguts' Date
- 生成所有的出栈序列 (回溯法)
- 周赛一 ACdream 1196 模拟题
- Leetcode Exercise
- myql5.7.7优化配置参数
- Trie树 hdu3460 Ancient Printer
- 给dubbo接口添加白名单——dubbo Filter的使用
- 为别人撑伞(深度好文)
- JS将一个十进制转换成二进制
- java使用dbcp操作数据库的用法
- hdu 2874 lca-tarjan离线算法(模板)
- HDU 3478
- 第一章 绪论
- 高并发处理方案