PAT刷题 (Java语言)
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
Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.
Output
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 我的代码:
package _1001; import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Scanner; public class Main { public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
int a=sc.nextInt();
int b=sc.nextInt();
int ans=a-b;
NumberFormat format=new DecimalFormat("#,###,###");
String str=new String();
str=format.format(ans);
System.out.println(str);
}
}
}
1002. A+B for Polynomials (25)
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
package _1002;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int n;
double[] a=new double[10];
double[] b=new double[10];
double[] ans=new double[10];
int i;
int max=0;
//第一行
Scanner scan=new Scanner(System.in);
n=scan.nextInt();
for(i=0;i<n;i++){
int index=scan.nextInt();
a[index]=scan.nextDouble();
}
//第二行
scan=new Scanner(System.in);
n=scan.nextInt();
for(i=0;i<n;i++){
int index=scan.nextInt();
b[index]=scan.nextDouble();
}
for(i=0;i<10;i++){
ans[i]=b[i]+a[i];
}
for(i=9;i>=0;i--){
if(ans[i]!=0){
max=i+1;
break;
}
}
System.out.print(max);
for(i=max-1;i>=0;i--){//保证一位小数规格化输出
System.out.format(" %d %.1f", i,ans[i]);
}
System.out.println();
}
}
PAT刷题 (Java语言)的更多相关文章
- 算法笔记_216:第六届蓝桥杯软件类校赛部分真题(Java语言C组)
目录 1 题目一 2 题目二 3 题目三 4 题目四 5 题目五 前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 题目一 二项式的系数规律,我国数学家很早就发现了. 如[图1.png],我国南宋数学 ...
- 算法笔记_206:第五届蓝桥杯软件类决赛真题(Java语言A组)
目录 1 海盗分金币 2 六角幻方 3 格子放鸡蛋 4 排列序数 5 幂一矩阵 6 供水设施 前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 海盗分金币 有5个海盗,相约进行一次帆船比赛. 比 ...
- 算法笔记_208:第六届蓝桥杯软件类决赛真题(Java语言A组)
目录 1 胡同门牌号 2 四阶幻方 3 显示二叉树 4 穿越雷区 5 切开字符串 6 铺瓷砖 前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 胡同门牌号 标题:胡同门牌号 小明家住在一条胡同里. ...
- 算法笔记_210:第六届蓝桥杯软件类决赛真题(Java语言C组)
目录 1 机器人数目 2 生成回文数 3 空心菱形 4 奇怪的数列 5 密文搜索 6 居民集会 前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 机器人数目 标题:机器人数目 少年宫新近邮购了小机器人 ...
- 算法笔记_215:第六届蓝桥杯软件类校赛部分真题(Java语言B组)
目录 1 题目一 2 题目二 3 题目三 前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 题目一 java中提供了对正则表达式的支持. 有的时候,恰当地使用正则,可以让我们的工作事半功倍! 如下代码 ...
- 算法笔记_214:第六届蓝桥杯软件类校赛真题(Java语言A组)
目录 1 题目一 2 题目二 3 题目三 4 题目四 5 题目五 6 题目六 7 题目七 前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 题目一 一个串的子串是指该串的一个连续的局部.如果不要求连续 ...
- 算法笔记_204:第四届蓝桥杯软件类决赛真题(Java语言C组)
目录 1 好好学习 2 埃及分数 3 金蝉素数 4 横向打印二叉树 5 危险系数 6 公式求值 前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 好好学习 汤姆跟爷爷来中国旅游.一天,他帮助中国的 ...
- 算法笔记_207:第五届蓝桥杯软件类决赛部分真题(Java语言C组)
目录 1 数字拆分 2 稍大的串 前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 数字拆分 正整数可以表示为若干正整数的累加和. 如,对于正整数n=6,可以分划为: 6 5+1 4+2 4+1+ ...
- 算法笔记_211:第七届蓝桥杯软件类决赛部分真题(Java语言A组)
目录 1 阶乘位数 2 凑平方数 3 棋子换位 4 机器人塔 前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 阶乘位数 阶乘位数 9的阶乘等于:362880 它的二进制表示为:10110001001 ...
随机推荐
- ArrayDeque详解
美人如斯! ArrayDeque是java中对双端队列的线性实现 一.特性 无容量大小限制,容量按需增长: 非线程安全队列,无同步策略,不支持多线程安全访问: 当用作栈时,性能优于Stack,当用于队 ...
- Oracle For Linux 恢复日记 霆智X8III
公司最近的客户需要在LINUX系统中做数据迁移,备份出来的内容数据库物理文件,回档日志和SpfileXXXX 客户用的是北京霆智的X8备份阵列,X8与数据库服务器都放在IDC机所,IDC机房与客户之间 ...
- 【翻译】nginx初学者指南
nginx初学者指南 本文翻译自nginx官方网站:http://nginx.org/en/docs/beginners_guide.html#control 该指南会对nginx做一个简要的介绍,同 ...
- Myeclipse6.5迁移到IDEA
背景 myeclipse开发的javaweb项目用svn管理.现要转用idea开发.因为发现idea实在是太好用了.myeclipse6.5是个纯净版,用了两年,对于新手来说用myeclipse6.5 ...
- hadoop中HDFS的NameNode原理
1. hadoop中HDFS的NameNode原理 1.1. 组成 包括HDFS(分布式文件系统),YARN(分布式资源调度系统),MapReduce(分布式计算系统),等等. 1.2. HDFS架构 ...
- ionic创建组件、页面或者过滤器
ionic可以直接 用命令来创建组件.页面或者过滤器. 在ionic项目根目录打开命令窗口.输入下列命令: ionic g page pageName //创建新页面 ionic g componen ...
- Java Web项目搭建过程记录(struts2)
开发工具:eclipse 搭建环境:jdk1.7 tomcat 8.0 基础的java开发环境搭建过程不再赘述,下面从打开eclipse 之后的操作开始 第一步: 创建项目,File -> ...
- 搭建idea出现无法自动映射Mapper问题
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 如出这种问题,导致的原因是mapp.xm ...
- 服务器安装python3环境
服务器安装python3环境 先安装相关包 yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel re ...
- 算法 翻转二叉树 dfs
翻转二叉树 翻转一棵二叉树.左右子树交换. Example 样例 1: 输入: {1,3,#} 输出: {1,#,3} 解释: 1 1 / => \ 3 3 样例 2: 输入: {1,2,3,# ...