POJ 1396 Simple Arithmetics

来源:互联网 发布:程序员有含金量的证书 编辑:程序博客网 时间:2024/05/17 03:20

这是一道大数模拟题,所以就想用Java来写写。于是各种TLE。


无奈就研究怎样才不会TLE。


注解一下关于Java的文件操作:


1、正常的输入输出(效率不高)

import java.util.Scanner;import java.io.*;public class Main {static private final String INPUT = "data1.in";static private final String OUTPUT = "1.out";public static void main(String[] args) { FileInputStream instream = null; //建立文件输入对象PrintStream outstream = null;    //建立屏幕输出对象try {instream = new FileInputStream(INPUT); //初始化文件输入对象outstream = new PrintStream(new FileOutputStream(OUTPUT));  //初始化屏幕输出对象输出到文件System.setIn(instream);System.setOut(outstream);} catch (Exception e) {System.err.println("Error Occurred.");  //如果访问文件有异常就输出并退出}Scanner cin = new Scanner(System.in);while (cin.hasNext()) {                     //判断是否到文件结尾String s = cin.next();                  //输入输出System.out.println(s);}}}

2、Buffer输入输出(效率高):

import java.io.*;public class Main {static private final String INPUT = "data1.in";static private final String OUTPUT = "1.out";public static void main(String[] args) throws IOException {  BufferedReader read = null; //建立文件输入对象PrintStream outstream = null;    //建立屏幕输出对象try {read = new BufferedReader(new FileReader(INPUT));outstream = new PrintStream(new FileOutputStream(OUTPUT));  //初始化屏幕输出对象输出到文件System.setOut(outstream);} catch (Exception e) {System.err.println("Error Occurred.");  //如果访问文件有异常就输出并退出}StringBuffer s = new StringBuffer(""); //StringBuffer初始化String string;while (null != (string = read.readLine())) { //读取数据时每次读一行s.append(string); //加入到 StringBuffer 里面s.append("\n");   //格式化输出每一行都要有回车}System.out.println(s);//整体输出StringBuffer}}



就有了下面的AC代码:

import java.math.BigInteger;import java.util.Scanner;import java.io.*;import javax.jws.soap.SOAPBinding;public class Main {static private final String INPUT = "data1.in";static private final String OUTPUT = "1.out";public static void main(String[] args) throws IOException {FileInputStream instream = null;PrintStream outstream = null;/*try {instream = new FileInputStream(INPUT);outstream = new PrintStream(new FileOutputStream(OUTPUT));System.setIn(instream);System.setOut(outstream);} catch (Exception e) {System.err.println("Error Occurred.");}*/int i, j, t;char cc = 0;BigInteger a = BigInteger.ZERO;BigInteger b = BigInteger.ZERO;BigInteger c = BigInteger.ZERO;//Scanner cin = new Scanner(System.in);BufferedReader read = new BufferedReader(new InputStreamReader(System.in));//BufferedReader read = new BufferedReader(new FileReader(INPUT));//String s = cin.next();//System.out.println(s);t = Integer.valueOf(read.readLine());while (t--!=0) {StringBuffer s0  =  new  StringBuffer();String str = read.readLine();String tmp;for (i = 0; i <= str.length(); i++) {if (str.charAt(i) == '+' || str.charAt(i) == '*'|| str.charAt(i) == '-') {cc = str.charAt(i);break;}}if (cc == '+') {String[] strs = str.split("\\+");a = new BigInteger(strs[0]);b = new BigInteger(strs[1]);c = a.add(b);Integer min = 0;tmp = String.valueOf(c);if (min < tmp.length()) {min = tmp.length();}if (min < strs[0].length()) {min = strs[0].length();}if (min < strs[1].length() + 1) {min = strs[1].length() + 1;}for (i = 0; i < min - strs[0].length(); i++) {//s0.append(" ");s0.append(" ");}//System.out.println(strs[0]);s0.append(strs[0]);s0.append("\n");for (i = 0; i < min - strs[1].length() - 1; i++) {//s0.append(" ");s0.append(" ");}//System.out.println("+" + strs[1]);s0.append("+");s0.append(strs[1]);s0.append("\n");for (i = 0; i < min; i++) {//s0.append("-");s0.append("-");}//System.out.println();s0.append("\n");for (i = 0; i < min - tmp.length(); i++) {s0.append(" ");}//System.out.println(tmp);s0.append(tmp);s0.append("\n");} else if (cc == '-') {String[] strs = str.split("\\-");a = new BigInteger(strs[0]);b = new BigInteger(strs[1]);c = a.subtract(b);Integer min = 0;Integer min2=strs[1].length()+1;tmp = String.valueOf(c);if(min2<tmp.length()){min2=tmp.length();}if (min < tmp.length()) {min = tmp.length();}if (min < strs[0].length()) {min = strs[0].length();}if (min < strs[1].length() + 1) {min = strs[1].length() + 1;}for (i = 0; i < min - strs[0].length(); i++) {s0.append(" ");}//System.out.println(strs[0]);s0.append(strs[0]);s0.append("\n");for (i = 0; i < min - strs[1].length() - 1; i++) {s0.append(" ");}//System.out.println("-" + strs[1]);s0.append("-");s0.append(strs[1]);s0.append("\n");for(i=0;i<min-min2;i++){s0.append(" ");}for (i = 0; i < min2; i++) {s0.append("-");}//System.out.println();s0.append("\n");for (i = 0; i < min - tmp.length(); i++) {s0.append(" ");}//System.out.println(tmp);s0.append(tmp);s0.append("\n");} else if (cc == '*') {String[] strs = str.split("\\*");a = new BigInteger(strs[0]);b = new BigInteger(strs[1]);c = a.multiply(b);Integer min = 0;tmp = String.valueOf(c);int len = tmp.length();if(len<strs[1].length()+1){len=strs[1].length()+1;}min=strs[1].length()+1;for (i = 0; i < len - strs[0].length(); i++) {s0.append(" ");}//System.out.println(strs[0]);s0.append(strs[0]);s0.append("\n");for (i = 0; i < len - strs[1].length() - 1; i++) {s0.append(" ");}//System.out.println("*" + strs[1]);s0.append("*");s0.append(strs[1]);s0.append("\n");if (strs[1].length() != 1) {if (len < min)len = min;int le2 = strs[1].length();BigInteger d = BigInteger.ZERO;for (i = 0; i < le2; i++) {d = BigInteger.valueOf(Long.valueOf(String.valueOf(strs[1].charAt(le2 - 1 - i))));d = d.multiply(a);String tmp2 = String.valueOf(d);if(i==0){if(min<tmp2.length()){min=tmp2.length();}for (j = 0; j < len - min; j++) {s0.append(" ");}for (j = 0; j < min; j++) {s0.append("-");}//System.out.println();s0.append("\n");}for (j = 0; j < len - tmp2.length() - i; j++) {s0.append(" ");}//System.out.println(tmp2);s0.append(tmp2);s0.append("\n");}for (i = 0; i < len - tmp.length(); i++) {s0.append(" ");}for (i = 0; i < tmp.length(); i++) {//System.out.printf("-");s0.append("-");}//System.out.println();s0.append("\n");}else{int min2=tmp.length();if(min2<strs[1].length()+1){min2=strs[1].length()+1;}for (i = 0; i < len - min2; i++) {s0.append(" ");}for (i = 0; i < min2; i++) {//System.out.printf("-");s0.append("-");}//System.out.println();s0.append("\n");}for (i = 0; i < len - tmp.length(); i++) {s0.append(" ");}//System.out.println(c);s0.append(c);s0.append("\n");}//if(t-1!=0)System.out.println();System.out.println(s0);//System.out.println("\r");}//}}}


0 0
原创粉丝点击