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 ...
随机推荐
- sqlserver apply
IF OBJECT_ID('tb') IS NOT NULL DROP TABLE tb go CREATE TABLE tb(name VARCHAR(10),value VARCHAR(200)) ...
- jQuery中cookie的简单操作
jQuery 可以通过 jquery.cookie.js 插件来操作 Cookie. 用NuGet安装:PM>Install-Package js-cookie -Version 官网:http ...
- jQuery中使用Ajax获取JSON格式数据示例代码
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.JSONM文件中包含了关于“名称”和“值”的信息.有时候我们需要读取JSON格式的数据文件,在jQuery中 ...
- localStorage、sessionStorage、Cookie的区别及用法
1.webstorage 本地存储,存储在客户端,包括localStorage和sessionStorage. (1)localStorage:生命周期是永久,这意味着除非用户显示在浏览器提供的UI上 ...
- JavaWeb第一天--HTML
HTML HTML简介 HTML(Hyper TextMarkupLanguage) 超文本标记语言. 超文本: 超越了文本仅仅展示信息的功能范畴,泛指:图片.有样式的文字.超链接. 标记: 标签. ...
- 93.vue---在vue环境用webuploader分片上传插件遇到的超级bug(独家仅此一份)
本来我是想想用vue-simple-uploader (https://www.cnblogs.com/xiahj/p/vue-simple-uploader.html)的 但是公司后台已经做好了we ...
- JavaScript 事件(基础)
一.事件 事件:触发-响应机制. 二.事件三要素 1.事件源:触发事件的元素 2.事件名称:发送了什么方式的事件 3.事件处理程序:事件触发后要执行的代码(函数形式) 三.事件的使用方式 1.获取元素 ...
- Android-----CheckBox复选使用(实现简单选餐)
直接上代码: xml布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmln ...
- 使用ProcDump自动生成Dump文件
ProcDump工具来自Sysinternals Suite 最近用来自动产生Dump文件 一是用来监视服务器程序无响应 procdump -accepteula -64 -ma -h server. ...
- k8s 学习笔记
常用的kubectl命令 kubectl run kubia --image=luksa/kubia --port=8080 --generator=run/v1 --image 指定镜像 - ...