1001 A+B Format (20 分)

 

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

Input Specification:

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.

Output Specification:

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.

Sample Input:

-1000000 9

Sample Output:

-999,991

解题时注意取余时i的位置
满分代码:
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<) {
int len = A.length();
int c = (len-)%;
System.out.print('-');
for(int i=;i<=c;i++) {
System.out.print(A.charAt(i));
}
if(c!=&&len>) System.out.print(','); for(int i=c+;i<A.length();i++) {
if((i-c-)!=&&(i-c-)%==&&i!=A.length()-) System.out.print(',');
System.out.print(A.charAt(i));
}
System.out.println();
}else {
int len = A.length();
int c = len%;
for(int i=;i<c;i++) {
System.out.print(A.charAt(i));
}
if(c!=&&len>) System.out.print(','); for(int i=c;i<A.length();i++) {
if((i-c)!=&&(i-c)%==&&i!=A.length()-) System.out.print(',');
System.out.print(A.charAt(i));
}
System.out.println();
}
}
in.close();
}
}

1002 A+B for Polynomials (25 分)

 

This time, you are supposed to find A+B where A and B are two polynomials.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:

K N​1​​ a​N​1​​​​ N​2​​ a​N​2​​​​ ... N​K​​ a​N​K​​​​

where K is the number of nonzero terms in the polynomial, N​i​​ and a​N​i​​​​ (,) are the exponents and coefficients, respectively. It is given that 1,0.

Output Specification:

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项需要溢出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 = ;
int up = in.nextInt();
Map<Integer, Double> hm = new HashMap<>();
for (int i = ; i < up; i++) {
int x = in.nextInt();
if (x > keyMax)
keyMax = x;
hm.put(x, in.nextDouble());
}
int down = in.nextInt();
for (int i = ; 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!=)
hm.put(x, y);
else hm.remove(x);
}
}
if(hm.size()==) System.out.print("");
else
System.out.print(hm.size() + " ");
int sign = ;
for (int i = keyMax; i >= ; 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)));
}
}
}
}
}
 

PAT甲级练习题1001、1002的更多相关文章

  1. PAT 乙级练习题1001 害死人不偿命的(3n+1)猜想 (15)

    1001. 害死人不偿命的(3n+1)猜想 (15) 卡拉兹(Callatz)猜想: 对任何一个自然数n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把(3n+1)砍掉一半.这样一直反复砍下去, ...

  2. pat甲级题目1001 A+B Format详解

    pat1001 A+B Format (20 分) Calculate a+b and output the sum in standard format -- that is, the digits ...

  3. 【PAT甲级】1001 A+B Format (20 分)

    题意:给两个整数a,b,计算a+b的值并每三位用逗号隔开输出(−1e6​​≤a,b≤1e6​​) AAAAAccepted code: #include<bits/stdc++.h> us ...

  4. 1080 Graduate Admission——PAT甲级真题

    1080 Graduate Admission--PAT甲级练习题 It is said that in 2013, there were about 100 graduate schools rea ...

  5. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  6. PAT甲级代码仓库

    大道至简,知易行难.希望能够坚持刷题. PAT甲级真题题库,附上我的代码. Label Title Score Code Level 1001 A+B Format 20 1001 * 1002 A+ ...

  7. PAT甲级1107. Social Clusters

    PAT甲级1107. Social Clusters 题意: 当在社交网络上注册时,您总是被要求指定您的爱好,以便找到一些具有相同兴趣的潜在朋友.一个"社会群体"是一群拥有一些共同 ...

  8. PAT甲级1076. Forwards on Weibo

    PAT甲级1076. Forwards on Weibo 题意: 微博被称为中文版的Twitter.微博上的一位用户可能会有很多关注者,也可能会跟随许多其他用户.因此,社会网络与追随者的关系形成.当用 ...

  9. PAT甲级考前整理(2019年3月备考)之一

       转载请注明出处:https://www.cnblogs.com/jlyg/p/7525244.html 终于在考前,刷完PAT甲级131道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种 ...

随机推荐

  1. Eclipse设置C++自动补全变量名快捷键

    用快捷键:Alt+/ 要是还是有些场合不能提示,按照下列步骤 Window-Preferences-c/c++-Editor-Content Assist-Advanced 将未勾选的全部勾选

  2. 关于stm32优先级大小的理解

    转载自:https://www.cnblogs.com/ZKeJun/p/6112591.html 一. 组别:0>1>2>3>4   组别优先顺序(第0组优先级最强,第4组优 ...

  3. 【HIHOCODER 1599】逃离迷宫4

    描述 小Hi被坏女巫抓进一座由无限多个格子组成的矩阵迷宫. 小Hi一开始处于迷宫(x, y)的位置,迷宫的出口在(a, b).小Hi发现迷宫被女巫施加了魔法,假设当前他处在(x, y)的位置,那么他只 ...

  4. BZOJ 2465: [中山市选2009]小球

    难度在于读题 #include<cstdio> #include<algorithm> using namespace std; int a[1000005]; struct ...

  5. CodeForces 519E 树形DP A and B and Lecture Rooms

    给出一棵树,有若干次询问,每次询问距两个点u, v距离相等的点的个数. 情况还挺多的,少侠不妨去看官方题解.^_^ #include <iostream> #include <cst ...

  6. T-SQL中的indexof函数

    在C#字符串中查找字符有indexof方法,那么在T-SQL与之相对应的是CHARINDEX方法,其语法为CHARINDEX(要查找的字符,字符串),返回一个数字. CHARINDEX(',','aa ...

  7. MySQL中的DDL(Data Definition Language,数据定义语言)

    create(创建表) 标准的建表语句: create table [模式名.]表名 ( #可以有多个列定义 columnName1 dataType [default expr(这是默认值)], . ...

  8. webdriver高级应用- 无人工干预地自动上传附件

    方法一:使用webdriver的send_keys方法上传文件,代码如下: #encoding=utf-8 from selenium import webdriver import unittest ...

  9. TOJ4203: Domino Piece

    4203: Domino Piece  Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByteTotal Submit: 5 ...

  10. php默认有最大执行时间

    执行php默认有最大执行时间,默认30s,修改,不能设置’1h’,貌似单位不能修改