Stack数据结构的特点后进先出的应用:大数据运算
2014-03-23 00:52
1206 查看
package com.sxt.test; import java.util.EmptyStackException; import java.util.Scanner; import java.util.Stack; public class TestStack { public static void main(String[] args) throws EmptyStackException { Scanner in = new Scanner(System.in); System.out.print("请输入第一个大数据:"); String number1 = in.next(); System.out.print("请输入第二个大数据:"); String number2 = in.next(); in.close(); bigNumberIncreament(number1, number2); } /** * 计算大数据的和 * @param number1 大数据1 * @param number2 大数据2 */ public static void bigNumberIncreament(String number1, String number2) { char[] number1_c = number1.toCharArray(); char[] number2_c = number2.toCharArray(); Stack<Integer> number1_stack = new Stack<Integer>(); Stack<Integer> number2_stack = new Stack<Integer>(); StringBuilder sb = new StringBuilder(); //保存计算结果 //将大数据压栈 for (char c : number1_c) { number1_stack.push(Integer.parseInt(c+"")); } for (char c : number2_c) { number2_stack.push(Integer.parseInt(c+"")); } int length1 = number1.length(); int length2 = number2.length(); int jinwei = 0; //初始进位为0 //按照较大的大数据长度循环 for(int i = 0; i < (length1 > length2? length1 : length2) + 1; i++) { int num1 = 0; int num2 = 0; try { num1 = number1_stack.pop().intValue(); } catch (EmptyStackException e) { num1 = 0; //空栈时,将当前位赋值为 } try { num2 = number2_stack.pop().intValue(); } catch (EmptyStackException e) { num2 = 0; //空栈时,将当前位赋值为0 } int sum = num1 + num2 +jinwei; if(sum >= 10) { jinwei = 1; sum = sum%10; } else { jinwei = 0; } sb.append(sum); } String result = sb.toString(); char[] sc = result.toCharArray(); if(sc[sc.length-1] != '0') { for(int i=sc.length-1; i>=0; i--) { System.out.print(sc[i]); } } else { for(int i=sc.length-2; i>=0; i--) { System.out.print(sc[i]); } } } }
相关文章推荐
- 我的php学习笔记(二)php基本数据类型、基本语法和基本运算类型及其应用
- Python 数据分析(二 本实验将学习利用 Python 数据聚合与分组运算,时间序列,金融与经济数据应用等相关知识
- 数据结构 单链表的应用:多项式及其运算
- poj 3347 Kadj Squares(扩大数据运算的典型应用+映射)
- Python 数据分析(二 本实验将学习利用 Python 数据聚合与分组运算,时间序列,金融与经济数据应用等相关知识
- 平行x 轴的线段 是否 遮掩 计算几何的扩大数据运算的典型应用,扩大根号2倍之后就避免了小数。 poj 3347 Kadj Squares
- POJ 3347 Kadj Squares 复杂的线段相交问题。这个题目是计算几何的扩大数据运算的典型应用 有时候扩大数据范围避免浮点误差,这个题就是应用
- SQL中case when应用及Decimal类型数据的运算
- 大数据行业应用现状与未来应用特点
- 数据分析之Pandas(二):索引、过滤 、算术运算、 函数应用和映射
- 大数据算法MOOC笔记1:大数据定义、特点、应用
- 大数据结构、特点、应用与价值
- 数据存储(大数据):Hbase概述、特点、应用场景、基本操作
- poj 3347 Kadj Squares(扩大数据运算的典型应用+映射)
- excel数据附件的使用方法及应用
- [原创]分布式系统之缓存的微观应用经验谈(三)【数据分片和集群篇】
- 数据结构之 栈应用---老鼠迷宫问题
- 微信小程序 数据绑定及运算的简单实例
- 最老程序员创业札记:全文检索、数据挖掘、推荐引擎应用34
- 16.Delphi数据浏览部件的应用及编程