`
endual
  • 浏览: 3545099 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

ACM 1002

    博客分类:
  • ACM
 
阅读更多

A + B Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 98171    Accepted Submission(s): 18601


Problem Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
 

Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
 

Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
 

Sample Input
2 1 2 112233445566778899 998877665544332211
 

Sample Output
Case 1: 1 + 2 = 3 Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110
 

Author
Ignatius.L

 

 

  转的答案 ac了

 

 import java.math.BigDecimal;

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		int t, i;
		String num1, num2;
		BigDecimal big1, big2;
		Scanner in = new Scanner(System.in);
		t = in.nextInt();
		for (i = 0; i < t; i++) {
			num1 = in.next();
			num2 = in.next();
			big1 = new BigDecimal(num1);
			big2 = new BigDecimal(num2);
			System.out.println("Case " + (i + 1) + ":");
			System.out.println(big1 + " + " + big2 + " = " + big1.add(big2));
			if (i != t - 1)
				System.out.println();
		}
	}
}

 

 

我的答案 wrong 不知道是为什么

 

package endual;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;

public class Main {

	public static void main(String[] args) {

		Scanner cin = new Scanner(System.in);
		int n = 0;
		n = cin.nextInt();
		List<String> list = new ArrayList<String>();
		String a, b;

		for (int i = 0; i < n; i++) {

			a = cin.next();
			b = cin.next();
			
			String tepa = a; 
			String tepb = b;
			
			int aLen = a.length();
			int bLen = b.length();
			if (aLen < bLen) { // 保证a的长度要大于b的长度
				String tempBtoA = a;
				a = b;
				b = tempBtoA;
			} // 保证a的长度要大于等于b的长度
			String he = jia(a, b);
			list.add(tepa + " + " + tepb + " = " + he);
		} // end for
		System.out.println();
		for (int j = 0; j < list.size(); j++) {
			System.out.println("Case " + (j + 1) + ":");
			System.out.println(list.get(j));
			System.out.println();

		}

	}

	private static String jia(String a, String b) {

		Stack stacka = new Stack();
		Stack stackb = new Stack();
		String ab = a;
		while (ab.length() != 0) {

			char c = ab.charAt(0);
			String subab = ab.substring(1);
			stacka.push(c);
			ab = subab;
		}

		String abc = b;
		while (abc.length() != 0) {

			char c = abc.charAt(0);
			String subabc = abc.substring(1);
			stackb.push(c);
			abc = subabc;
		}

		Stack sum = new Stack();
		int temp = 0;
		int t = 0;
		while (!stackb.isEmpty()) { // 如果两个里面其中有一个是空了,那么停止

			int aInt = Integer.parseInt(stacka.pop().toString());
			int bInt = Integer.parseInt(stackb.pop().toString());
			temp = aInt + bInt + t;
			if (temp < 10) {
				t = 0;
			} else {
				temp = temp - 10;
				t = 1; // 将t设置成为1,此时将sumAB的1取得
			}
			sum.push(temp); // 添加到栈中去
		}// end while ;

		while (!stacka.isEmpty()) {

			int aInt = Integer.parseInt(stacka.pop().toString());
			temp = aInt + t;
			if (temp < 10) {
				sum.push(temp);
				t = 0;
			} else {

				temp = temp - 10;
				t = 1; // 将t设置成为1,此时将sumAB的1取得
			}
			sum.push(temp); // 添加到栈中去

		}
		if (t == 1) {

			sum.push(t);
		}
		String s = "";
		while (!sum.isEmpty()) { // 如果两个里面其中有一个是空了,那么停止

			s = s + sum.pop().toString();

		}
		return s;

	}

}
 
分享到:
评论

相关推荐

    北大acm1002题

    【北大ACM1002题】是一道与编程竞赛相关的题目,常见于北京大学组织的ACM/ICPC(国际大学生程序设计竞赛)训练或比赛中。这类问题通常要求参赛者运用算法和编程技巧在限定时间内解决特定的计算问题。在这个案例中,...

    浙大acm1002答案

    浙江大学acm题1002道的答案 个人答案 仅供参考

    杭电ACM1002解题答案

    ### 杭电ACM1002解题分析与代码详解 #### 题目背景与解析 根据题目描述及代码内容,我们可以推断出这是一道关于大数加法处理的问题。通常情况下,整数的加法可以直接通过内置类型(如`int`或`long long`)来实现,...

    杭电ACM 1002

    大数相加 Problem Description I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. Input The first line of the input contains an integer T(1) ...

    杭电ACM1002-1025VC版个人解答

    【杭电ACM1002-1025VC版个人解答】是针对杭州电子科技大学(杭电)ACM在线编程竞赛中的1002到1025题的一系列解题方案集,主要使用VC++编程语言进行实现。这些题目涵盖了算法设计、数据结构和编程技巧等多个方面,...

    大数据加减ACM1002

    ### 大数据加减ACM1002 #### 知识点概述 在计算机科学领域,处理大数据的加减法通常涉及到特定的数据结构和算法。对于超出基本数据类型(如`int`、`long`等)所能表示的数值范围的情况,通常采用字符串或数组来...

    杭电acm1002源码

    **杭电ACM1002源码** 是一个编程题目,旨在帮助初学者掌握处理大整数加法的技巧。该题目要求参赛者编写一个程序来计算两个非常大的整数的和,并正确输出结果。这里的大整数是指那些不能直接用32位或64位整型变量表示...

    pku acm 1002 487-3279代码

    pku acm 1002 487-3279代码 二叉查找数实现 解题报告请访问:http://blog.csdn.net/china8848

    浙江大学ACM1002题

    看看的思路。。。。。。。。。。。。。。 我有点错,帮帮我看看哦

    杭电acm1002题C++编的程序

    用C++自己编的,感觉比较简单,定义两个字符数组存储两个大数,再定义一个正西数组存放结果

    北大ACM···············

    8. **北大ACM[1002-3200]**:这部分可能是指北京大学ACM团队在过去比赛中参与的题目范围,编号通常代表了比赛的题目ID,这可能是一份比赛题目的集合,用于回顾和研究。 综合这些资源,学习者可以通过阅读文档、研究...

    ACM 题目 1002 to 1011

    【标题】"ACM题目1002到1011"揭示了这是一组针对ACM(国际大学生程序设计竞赛)的编程练习题目,主要使用Java语言进行编写。ACM竞赛是全球知名的大学生编程比赛,旨在提升参赛者在算法设计、问题解决以及编程实施上...

    ACM杭电1002 C++程序

    标题 "ACM杭电1002 C++程序" 指向的是一个与ACM国际大学生程序设计竞赛相关的题目,具体是杭州电子科技大学(Hangzhou Dianzi University)在线评测系统上的第1002号问题。这个问题要求用C++编程语言来解决大数相加...

    北大ACM题库[1002-3200].iso

    北大ACM题库[1002-3200].iso 北大ACM题库[1002-3200].iso

    UralACM1002(c++)

    【标题】"UralACM1002(c++)"是一个与编程竞赛相关的主题,尤其在ACM(国际大学生程序设计竞赛,International Collegiate Programming Contest)的范畴内。Ural ACM通常指的是乌拉尔大学(University of Ural)举办...

    HDU_ACM_1002_大数相加C源代码

    HDU_ACM_1002_大数相加C源代码,利用字符串处理

    acm.rar_1048_ACM_acm 答案_visual c

    1. Cpp1002.cpp:这是一个C++源代码文件,可能包含了ACM题目1002的解决方案。 2. Cpp2736.cpp:同样为C++源代码,对应ACM题目2736的解答。 3. Cpp1915.cpp:ACM题目1915的C++实现。 4. 1048.cpp:显然,这是针对题目...

    北大ACM分类 北大ACM分类

    例如,排序题目1002要求快速排序,而1007则需要实现稳定的排序算法。搜索与回溯题目如1011、1190等提供了不同难度的挑战,而1979和1980等题目对剪枝技术有较高要求。 通过这个分类体系,学生可以有针对性地训练和...

    ACM POJ 1002题解摘要

    ### ACM POJ 1002题解摘要 #### 题目背景与目标 本题目来自POJ(Pacific OpenJudge)平台上的一个经典问题,编号为1002。题目要求解决的是电话号码标准化的问题,即如何将各种形式的电话号码转换成统一的标准格式...

Global site tag (gtag.js) - Google Analytics