uva 465 - Overflow
자바 의 큰 클래스 로 처리 하고 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;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.