本文共 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 Stackstack = 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/