博客
关于我
PAT——1049. 数列的片段和
阅读量:464 次
发布时间:2019-03-06

本文共 2482 字,大约阅读时间需要 8 分钟。

给定一个正数数列,我们可以从中截取任意的连续的几个数,称为片段。例如,给定数列{0.1, 0.2, 0.3, 0.4},我们有(0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1, 0.2, 0.3, 0.4) (0.2) (0.2, 0.3) (0.2, 0.3, 0.4) (0.3) (0.3, 0.4) (0.4) 这10个片段。

给定正整数数列,求出全部片段包含的所有的数之和。如本例中10个片段总和是0.1 + 0.3 + 0.6 + 1.0 + 0.2 + 0.5 + 0.9 + 0.3 + 0.7 + 0.4 = 5.0。

输入格式:

输入第一行给出一个不超过105的正整数N,表示数列中数的个数,第二行给出N个不超过1.0的正数,是数列中的数,其间以空格分隔。

输出格式:

在一行中输出该序列所有片段包含的数之和,精确到小数点后2位。

输入样例:

40.1 0.2 0.3 0.4

输出样例:

5.00
1 package com.hone.basical; 2  3 import java.util.Scanner; 4 import java.util.Stack; 5  6 /** 7  * 题目中规定:数字最长为100位,则通过一般的数肯定会越界,可以考虑使用字符串 8  * 这类题目都可以考虑利用字符串来处理数据的进制转化。 9  * @author Xia10  *11  */12 public class basicalLevel1048encryptNUm2 {13 14     public static void main(String[] args) {15 16         Scanner in = new Scanner(System.in);17         String a = in.next();            //用字符串a表示数A18         String b = in.next();            //用字符串b表示数B19         in.close();20 21         int i = a.length() - 1;22         int j = b.length() - 1;23 24         //首先补全位数25         if (i > j) {26             int c = i - j;27             while (c != 0) {28                 b = "0" + b;29                 c--;30             }31         } else if (i < j) {32             int c = j - i;33             while (c != 0) {34                 a = "0" + a;35                 c--;36             }37         }38 39         int index = b.length() - 1; // 采用一种倒叙的方法40         boolean isOdd = true;41         Stack
stack = new Stack<>();42 while (index >= 0) {43 if (isOdd) {44 isOdd = false;45 int cNumber = a.charAt(index) - '0' + b.charAt(index) - '0';46 String r = null;47 switch (cNumber) {48 case 10:49 r = "J";50 break;51 case 11:52 r = "Q";53 break;54 case 12:55 r = "K";56 break;57 default:58 r = "" + cNumber % 13;59 break;60 }61 62 stack.push(r);63 } else {64 isOdd = true;65 int cNumber = (b.charAt(index) - a.charAt(index) + 10) % 10;66 String r = "" + cNumber;67 stack.push(r);68 }69 index--;70 }71 72 while (!stack.isEmpty()) {73 System.out.print(stack.pop());74 }75 }76 77 }

 

转载地址:http://ejnbz.baihongyu.com/

你可能感兴趣的文章
MySQL参数调优详解
查看>>
mysql参考触发条件_MySQL 5.0-触发器(参考)_mysql
查看>>
MySQL及navicat for mysql中文乱码
查看>>
MySqL双机热备份(二)--MysqL主-主复制实现
查看>>
MySQL各个版本区别及问题总结
查看>>
MySql各种查询
查看>>
mysql同主机下 复制一个数据库所有文件到另一个数据库
查看>>
mysql启动以后会自动关闭_驾照虽然是C1,一直是开自动挡的车,会不会以后就不会开手动了?...
查看>>
mysql启动和关闭外键约束的方法(FOREIGN_KEY_CHECKS)
查看>>
Mysql启动失败解决过程
查看>>
MySQL启动失败:Can't start server: Bind on TCP/IP port
查看>>
mysql启动报错
查看>>
mysql启动报错The server quit without updating PID file几种解决办法
查看>>
MySQL命令行登陆,远程登陆MySQL
查看>>
mysql命令:set sql_log_bin=on/off
查看>>
mySQL和Hive的区别
查看>>
MySQL和Java数据类型对应
查看>>
mysql和oorcale日期区间查询【含左右区间问题】
查看>>
MySQL和SQL入门
查看>>
mysql在centos下用命令批量导入报错_Variable ‘character_set_client‘ can‘t be set to the value of ‘---linux工作笔记042
查看>>