pat1002
2016-04-11 14:46
435 查看
This time, you are supposed to find A+B where A and B are two polynomials.
Input
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 aN1 N2 aN2 ... NK aNK,
where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, ..., K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10,0 <= NK < ... < N2 < N1 <=1000.
Output
For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.
Sample Input
Sample Output
Input
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 aN1 N2 aN2 ... NK aNK,
where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, ..., K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10,0 <= NK < ... < N2 < N1 <=1000.
Output
For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.
Sample Input
2 1 2.4 0 3.2 2 2 1.5 1 0.5
Sample Output
3 2 1.5 1 2.9 0 3.2
输入两行数,把指数相同的相加起来,最后输出在一行。最后指数为0.0的不能输出,也不记在总数里
用C#处理这种多输入真的头疼,使用Dictionary<int,double>存数据,用了linq的排序方法= =感觉好复杂
输出格式为{0} {1:.0}
using System; using System.Collections; using System.Collections.Generic; using System.Linq; namespace Test { class Program { static void Main(string[] args) { int n1, n2; Dictionary<int, double> d = new Dictionary<int, double>(); string[] str1 = Console.ReadLine().Split(); string[] str2 = Console.ReadLine().Split(); n1 = Convert.ToInt32(str1[0]); n2 = Convert.ToInt32(str2[0]); int i, j; for (i = 1, j = 2; i <= str1.Length - 1 && j <= str1.Length - 1; i += 2, j += 2) { d.Add(Convert.ToInt32(str1[i]), Convert.ToDouble(str1[j])); } for (i = 1, j = 2; i <= str2.Length - 1 && j <= str2.Length - 1; i += 2, j += 2) { if (d.ContainsKey(Convert.ToInt32(str2[i]))) { d[Convert.ToInt32(str2[i])] += Convert.ToDouble(str2[j]); } else { d.Add(Convert.ToInt32(str2[i]), Convert.ToDouble(str2[j])); } } int co = 0; int nums = d.Count; d = d.OrderByDescending(r => r.Key).ToDictionary(r => r.Key, r => r.Value); foreach (KeyValuePair<int, double> ke in d) { if (ke.Value == 0) nums--; } Console.Write(nums); if (nums != 0) Console.Write(" "); foreach (KeyValuePair<int, double> ke in d) { if (ke.Value == 0) { continue; } co++; Console.Write("{0} {1:.0}", ke.Key, ke.Value); if (co != nums) Console.Write(" "); } Console.WriteLine(); } } }
相关文章推荐
- c#调用COM组件
- C#实现把指定数据写入串口
- C#动态创建button的方法
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- C#实现给图片加水印的方法
- C#使用加边法计算行列式的值
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#中的delegate委托类型基本学习教程
- C#实现子窗体与父窗体通信方法实例总结
- C#通用邮件发送类分享
- 举例讲解C#中自动实现的属性
- C#中this的用法集锦
- C#数据结构之顺序表(SeqList)实例详解
- C#.NET获取拨号连接的宽带连接方法
- C#异步绑定数据实现方法
- C#实现AddRange为数组添加多个元素的方法
- C#中Equality和Identity浅析
- C#比较二个数组并找出相同或不同元素的方法