uva 465 - Overflow

제목 주소:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=97&page=show_problem&problem=406
 
자바 의 큰 클래스 로 처리 하고 WA 를 제출 합 니 다.원인 을 찾 지 못 해 알 이 아 픕 니 다.............................................
import java.math.BigInteger;
import java.util.Scanner;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(System.in);
		Integer maxInt = Integer.MAX_VALUE;
		BigInteger a = null;
		BigInteger b = null;
		BigInteger res = null;
		String op = null;
		BigInteger max = new BigInteger(maxInt.toString());

		while (cin.hasNext()) {
			a = new BigInteger(cin.next());
			op = cin.next();
			b = new BigInteger(cin.next());

			System.out.println(a + " " + op + " " + b);

			if (a.compareTo(max) > 0)
				System.out.println("first number too big");

			if (b.compareTo(max) > 0)
				System.out.println("second number too big");

			if (op.equals("+") && ((a.add(b)).compareTo(max) > 0))
				System.out.println("result too big");
			if (op.equals("*") && ((a.multiply(b)).compareTo(max) > 0))
				System.out.println("result too big");

		}

	}
}

 
C++코드 AC 를 훔 쳤 습 니 다.게 으 름 을 피 웠 습 니 다.문자열 로 처리 하지 않 고 double 로 저장 합 니 다.double 은 15-16 비트 정밀도 가 있 습 니 다.최대 10 비트 를 넘 지 않 는 int 에 충분 합 니 다.정수 숫자 가 크 더 라 도 double 의 정밀도 표시 범 위 를 초과 하 더 라 도 숫자의 수량 급 은 넘 칠 수 있 습 니 다.
#include<cstdio>
#include<cstdlib>
#include<cstring>


const int MAX=1000;

int main()
{
	char a[MAX];
	char b[MAX];
	
	char op;
	const int maxInt=0x7fffffff;
	double aNum;
	double bNum;



	while(scanf("%s %c %s",a,&op,b)!=EOF)
	{
		printf("%s %c %s
",a,op,b); aNum=atof(a); bNum=atof(b); if(aNum>maxInt) printf("first number too big
"); if(bNum>maxInt) printf("second number too big
"); if(op=='+') { if(aNum+bNum > maxInt) printf("result too big
"); } else { if(aNum*bNum>maxInt) printf("result too big
"); } } return 0; }

 

좋은 웹페이지 즐겨찾기