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. python之自定义排序函数sorted()

    sorted()也是一个高阶函数,它可以接收一个比较函数来实现自定义排序,比较函数的定义是,传入两个待比较的元素 x, y,如果 x 应该排在 y 的前面,返回 -1,如果 x 应该排在 y 的后面, ...

  2. Linux入门学习笔记1:VI常用命令

    常用命令 yy 复制 p 黏贴 shift+v 多行选中 shift+ctrl+< 左移 shift+ctrl+> 右移 ndd 删除光标所在行及其后n-1行 i 进入编辑状态 esc 退 ...

  3. 转:跟我一起写Makefile (PDF重制版)

    原文地址:http://seisman.info/how-to-write-makefile.html 其它一些问题  不妨看一下:http://blog.csdn.net/huyansoft/art ...

  4. jenkins的构建项目配置

    继http://www.cnblogs.com/yajing-zh/p/5109517.html搭建好jenkins系统配置之后,新建jenkins构建项目,用于自动化构建. 点击Jenkins界面左 ...

  5. selenium2 页面对象模型Page Object

    开发Selenium WebDriver测试时,可以使用页面对象模型,这样可使得测试脚本有更高的可维护性,减少了重复的代码,把页面抽象出来.同时页面对象模型也提供了一个注释,帮助缓存远程,避免出现元素 ...

  6. bash函数定义/使用/传参…

    函数:function, 功能     过程式编程,代码重用         模块化编程         简洁             语法:         function f_name {    ...

  7. HDU 3594 Cactus 有向仙人掌图判定

    题意 给出一个有向图,并给出仙人掌图的定义 图本身是强连通的 每条边属于且只属于一个环 判断输入的图是否是强连通的. 分析 杭电OJ上的数据比较弱,网上一些有明显错误的代码也能AC. 本着求真务实的精 ...

  8. Andorid 生成NDK动态链接库 .so库

    .so库第一次见到是在搜索Android保存静态秘钥等特殊id字段做法时看到的-通过NDK的方式将静态秘钥保存在so文件中, 关于原生开发工具包(NDK)详细见官网指南要更详细,这里我记录我度娘各种结 ...

  9. list里内置程序用法

    列表是我们编程工作中经常都会遇到的数据类型.以下是列表里面的一些常用操作,主要分为:增! 删! 改! 查! first 查: 1.索引(下标),其中有切片操作,但要注意下标都是从零开始: 2.查元素出 ...

  10. js中的事件委托和事件代理详解

    起因: 1.这是前端面试的经典题型,要去找工作的小伙伴看看还是有帮助的: 2.其实我一直都没弄明白,写这个一是为了备忘,二是给其他的知其然不知其所以然的小伙伴们以参考: 概述: 那什么叫事件委托呢?它 ...