Calculate a+b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).html
Each input file contains one test case. Each case contains a pair of integers a and b where −. The numbers are separated by a space.java
For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.git
-1000000 9
解題時注意取餘時i的位置
滿分代碼:-999,991
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int a = in.nextInt(); int b = in.nextInt(); int res = a+b; String A = String.valueOf(res); if(res<0) { int len = A.length(); int c = (len-1)%3; System.out.print('-'); for(int i=1;i<=c;i++) { System.out.print(A.charAt(i)); } if(c!=0&&len>3) System.out.print(','); for(int i=c+1;i<A.length();i++) { if((i-c-1)!=0&&(i-c-1)%3==0&&i!=A.length()-1) System.out.print(','); System.out.print(A.charAt(i)); } System.out.println(); }else { int len = A.length(); int c = len%3; for(int i=0;i<c;i++) { System.out.print(A.charAt(i)); } if(c!=0&&len>3) System.out.print(','); for(int i=c;i<A.length();i++) { if((i-c)!=0&&(i-c)%3==0&&i!=A.length()-1) System.out.print(','); System.out.print(A.charAt(i)); } System.out.println(); } } in.close(); } }
This time, you are supposed to find A+B where A and B are two polynomials.less
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:flex
K N1 aN1 N2 aN2 ... NK aNKspa
where K is the number of nonzero terms in the polynomial, Ni and aNi (,) are the exponents and coefficients, respectively. It is given that 1,0.code
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.orm
2 1 2.4 0 3.2 2 2 1.5 1 0.5
3 2 1.5 1 2.9 0 3.2
注意爲0項須要溢出map表
滿分代碼:
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int keyMax = 0; int up = in.nextInt(); Map<Integer, Double> hm = new HashMap<>(); for (int i = 0; i < up; i++) { int x = in.nextInt(); if (x > keyMax) keyMax = x; hm.put(x, in.nextDouble()); } int down = in.nextInt(); for (int i = 0; i < down; i++) { int x = in.nextInt(); if (x > keyMax) keyMax = x; if (!hm.containsKey(x)) { hm.put(x, in.nextDouble()); } else { double y = hm.get(x); y += in.nextDouble(); if(y!=0) hm.put(x, y); else hm.remove(x); } } if(hm.size()==0) System.out.print("0"); else System.out.print(hm.size() + " "); int sign = 0; for (int i = keyMax; i >= 0; i--) { if (hm.containsKey(i)) { sign++; System.out.print(i + " "); if (sign != hm.size()) System.out.print(String.format("%.1f", hm.get(i)) + " "); else System.out.print(String.format("%.1f", hm.get(i))); } } } } }