码迷,mamicode.com
首页 > 其他好文 > 详细

1002. A+B for Polynomials (25)

时间:2015-12-06 10:01:59      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

注意输入输出的:

1,输入小数的语句 scanf("%lf", &a)

2,输出一位小数的语句 printf("%.1f", count);


时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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
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

  1. #pragma warning(disable:4996)
  2. /**/
  3. #include <stdio.h>
  4. using namespace std;
  5. int main(void){
  6. int k1 = 0, k2 = 0;
  7. int ak1[1001] = { 0 }, ak2[1001] = { 0 };//项数
  8. double nk1[1001] = { 0.0 }, nk2[1001] = { 0.0 };//系数
  9. scanf("%d", &k1);
  10. for (int i = 0; i < k1; i++){
  11. scanf("%d", &ak1[i]);
  12. scanf("%lf", &nk1[i]);
  13. }
  14. scanf("%d", &k2);
  15. for (int j = 0; j < k2; j++){
  16. scanf("%d", &ak2[j]);
  17. scanf("%lf", &nk2[j]);
  18. }
  19. double c[1001] = {0};
  20. for (int i = 0; i < k1; i++){
  21. c[ak1[i]] += nk1[i];
  22. }
  23. for (int j = 0; j < k2; j++){
  24. c[ak2[j]] += nk2[j];
  25. }
  26. int count = 0;
  27. for (int k = 0; k <= 1000; k++){
  28. if (c[k] != 0)
  29. count++;
  30. }
  31. printf("%d", count);
  32. for (int q = 1000; q >= 0; q--){
  33. if (c[q] != 0)
  34. printf(" %d %.1f", q, c[q]);
  35. }
  36. return 0;
  37. }
  1. #pragma warning(disable:4996)
  2. #include <stdio.h>
  3. using namespace std;
  4. int main(void) {
  5. int k1 = 0, k2 = 0;
  6. k2 = 0;
  7. int n1[1001] = { 0 }, n2[1001] = { 0 };
  8. double a1[1001] = { 0.0 }, a2[1001] = { 0.0 };
  9. int i = 0;
  10. scanf("%d", &k1);
  11. for (i = 0; i < k1; i++) {
  12. scanf("%d", &n1[i]);
  13. scanf("%lf", &a1[i]);////not number 1
  14. }
  15. i = 0;
  16. scanf("%d", &k2);
  17. for (i = 0; i < k2; i++) {
  18. scanf("%d", &n2[i]);
  19. scanf("%lf", &a2[i]);
  20. }
  21. double c[1001] = { 0 };
  22. for (i = 0; i < k1; i++) {
  23. c[n1[i]] += a1[i];
  24. }
  25. for (i = 0; i < k2; i++) {
  26. c[n2[i]] += a2[i];
  27. }
  28. int count = 0;
  29. for (i = 0; i < 1001; i++) {
  30. if (c[i] != 0)
  31. count++;
  32. }
  33. printf("%d", count);
  34. for (i = 1000; i >= 0; i--) {
  35. if (c[i] != 0) //****
  36. printf(" %d %.1f", i, c[i]);
  37. }
  38. return 0;
  39. }

 



1002. A+B for Polynomials (25)

标签:

原文地址:http://www.cnblogs.com/zzandliz/p/5023027.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!