1. 程式人生 > >杭電ACM第1002題(A + B Problem II)答案 java版

杭電ACM第1002題(A + B Problem II)答案 java版

方法一:

package hduacm;

import java.util.Scanner;
public class Main{
    public static void main(String[] args) throws Exception{
         Scanner in = new Scanner(System.in);
         int n = in.nextInt();  
         int up = 0;   //進位
         int ma = 0;   //進位後的尾數
         int j = 1;    //case的編號

         while
(n > 0) { StringBuffer temResult = new StringBuffer(); up = 0; String a = in.next(); //第一個數字 String b = in.next(); //第二個數字 //獲取a和b的長度 int al = a.length(); int bl = b.length(); al--; bl--; //從a和b的最低位開始相加
for(; al >= 0 && bl >= 0; al--, bl--){ int temp1 = Integer.parseInt(a.charAt(al)+""); int temp2 = Integer.parseInt(b.charAt(bl)+""); int temp3 = temp1 + temp2 + up; if(temp3 >= 10){ up = temp3 / 10
; ma = temp3 % 10; }else{ up = 0; ma = temp3; } temResult.append(ma); } //兩個數位數不一致,第1個數位數長 while(al >= 0){ //或去第一個數的下一位數字 int temp1 = Integer.parseInt(a.charAt(al)+""); //加上進位 int temp3 = temp1 + up; //插入結果的下一位 temResult.append(temp3); //將進位設為0 up = 0; al--; } //兩個數位數不一致,第2個數位數長 while(bl >= 0){ int temp1 = Integer.parseInt(b.charAt(bl)+""); int temp3 = temp1 + up; temResult.append(temp3); up = 0; bl--; } //兩個數位數一樣,直接將進位插入結果的下一位 if(up != 0){ temResult.append(up); } //將結果轉為正序排列 int len = temResult.length(); char[] result = new char[len]; len--; for(int i = 0; len >= 0; i++, len--){ result[i] = temResult.charAt(len); } //輸出 System.out.println("Case "+j+":"); System.out.print(a + " +" + " " + b + " = " + String.valueOf(result)); if(n != 1){ System.out.println(); } System.out.println(); n--; j++; } } }

方法二:使用BigDecimal 類

import java.math.BigDecimal;
import java.util.Scanner;

public class Main{
    public static void main(String args[])     { 
        Scanner scanner = new Scanner(System.in);   
        String temp1=null;   
        String temp2=null;   
        String result=null;   
        int i; 

        int a=scanner.nextInt();   
        for(i=0;i<a;i++){    
            temp1=scanner.next();    
            temp2=scanner.next(); 
            BigDecimal bigdecimal=new BigDecimal(temp1);    
            BigDecimal bigdecimal2=new BigDecimal(temp2);    

            result=bigdecimal.add(bigdecimal2).toString();  
            if(i!=(a-1)) {
                System.out.println("Case"+" "+
                (i+1)+":\r\n"+bigdecimal+" + 
                "+bigdecimal2+" = "+result+"\r\n");
            } 
            else {
                System.out.println("Case"+" "+(i+1)
                +":\r\n"+bigdecimal+" +
                 "+bigdecimal2+" = "+result);
            }   
        }     
    } 
} 

相關推薦

ACM1002(A + B Problem II)答案 java

方法一: package hduacm; import java.util.Scanner; public class Main{ public static void main(String[] args) throws Exception{

【HDUOJ】1002 A + B Problem II 純C語言解法

【HUDOJ-1002】1.原題:Problem DescriptionI have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of

ACM訓練--A + B Problem

Time limit1000 msMemory limit32768 kB total:Memory 1792kB Calculate A + B . Input Each line will contain two integers A and B . Process to end o

ACM1008——Elevator

#include <stdio.h> #include <stdlib.h> int main() { int n,i,flag; while(scanf("%d",&n)&&n) { int *a=(int *)m

ACM 2096 小明A+B

#include<stdio.h>int main(){ int n,a,b,c; scanf("%d",&n); while(n--) {  scanf("%d %d",&a,&b);   if(a>=100)   a=a%100;  if(b>=100)  

ACM1005——Number Sequence

#include <stdio.h> #include <stdlib.h> int main(){ int a,b,n,i; while(scanf("%d %d %d",&a,&b,&n)&&(a

acm1003Max Sum ( 動態規劃)

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 169492    Accepted Submi

ACM1003——Max Sum

#include <stdio.h> #include <stdlib.h> int main() { int T,sum,start,end,i,j,k,r,num,s; scanf("%d",&T); for(r=1;r<

ACM1007——Quoit Design

#include <iostream> #include <algorithm> #include <cmath> using namespace std; const int SIZE = 100005; typedef struct { double x; dou

HDOJ 1002.A+B Problem II 答案

#include<cstdio> #define MAXS 10000 char a[MAXS],b[MAXS],c[MAXS]; void cal(); int main() { in

ACM—HDU 1002 A + B Problem II

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1002 題目: Problem Description I have a very simple problem for you. Given two integers A

HDU 1002 A + B Problem II Java 大數的初級用法

<a target=_blank href="http://acm.hdu.edu.cn/showproblem.php?pid=1002">點選開啟連結</a> import

四站 HDU A + B Problem II

一道很簡單的題目讓我做得噁心無比,大數問題,不熟悉java,還是用C++寫吧。顯然需要用到字串,然後再將字串中的數字轉化為整數型別進行加法運算。去年做類似題目的時候還只知其然不知其所以然,現在明白了為什麼不能直接讀入陣列。 然而時隔一年也忘記了思路,加上當時沒有總結過,只是仿照程式碼敲

ACM oj 1002 大數相加,求助

杭電oj 1002題大數相加,格式與AC的相同,試了很多結果都是對的,但就是過不了,有大神幫忙看一下好嗎?謝謝! #include<stdio.h> #include<string.h> int main(void) { char a[

HPU 1002 A + B Problem II【大數】

lar 保存 memory positive test size mod ack auth A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/327

HDU 1002 B - A + B Problem II

scan ora include color content can 今天 time ons A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (

hduoj 1002 A + B Problem II

hdu cin http short ins line include RoCE problem 原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 題目描述如下: A + B Problem II Time Limit

題解報告:hdu 1002 A + B Problem II(大數加法)

return 大數類 class family HERE contains urn integer ons Problem Description I have a very simple problem for you. Given two integers A and

廈理OJ——1002A+B Problem

一、題目 Description Calculate a+b Input Two integer a,b (0<=a,b<=10) Output Output a+b Sample Input 1 2 Sample Output 3 H

HDOJ——1002 A + B Problem II

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 432906