题目地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=97&page=show_problem&problem=406
用java的大数类处理,提交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\n",a,op,b); aNum=atof(a); bNum=atof(b); if(aNum>maxInt) printf("first number too big\n"); if(bNum>maxInt) printf("second number too big\n"); if(op=='+') { if(aNum+bNum > maxInt) printf("result too big\n"); } else { if(aNum*bNum>maxInt) printf("result too big\n"); } } return 0; }
相关推荐
判断输入字符串是否为镜像或回文串。 来源于UVaOJ - 401. 水题。
开源项目-codingsince1985-UVa#uva-online-judge-solutions-in-golang.zip,两年来每天都在解决一个uva在线裁判问题,算起来…
uva705 Slash Maze 的代码,在UVaOJ上通过
PDF试题
uva532 Dungeon Master的源代码,并且AC了
Algorithm-UVA-Solutions-in-Python.zip,python 3中各种uva(acm)问题的解决方案。,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
这是UVA133 TheDoleQueue救济金发放问题,经典的算法问题。初学算法的人要对这种算法非常熟悉并且能熟练运用。
tpcw-nyu-uva-client 客户端
leetcode 2 算法-Java UVa Online Judge(ACM-ICPC Live ...使用:数组、哈希表、链表、二分搜索、动态规划、堆栈、堆、reedy、排序、树 DFS、BFS、图、二分搜索树、递归、记忆、队列、映射等。...Uva-ACM-ICPC