|
亲!马上注册或者登录会查看更多内容!
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
题目链接: http://www.meetcoder.com/problem.php?id=48。
题目要求 x + y is less than C,应该是小于不等于。
测试 input 中,x 取自 {0,1},y 取自 {0,1}, c 等于 0。只有 x = 0, y = 0 时才有 x+y < c。测试 out 应为 1/4,而不是1/2。
我按照我的理解写出来的code可以通过我自己的test case,但提交会报错。希望大神们帮我看看,谢谢!
- class Solution {
- public int[] calculate(int a,int b,int c) {
- int [] ret = new int[2];
- int l = Math.max(a, b);
- int s = Math.min(a, b);
- int total = (l+1) * (s+1);
- int count = 0;
- if (c > s+l) {
- count = total;
- }else if ( c <= s ) {
- count = (s+1) * s / 2;
- } else if( c <= l) {
- count = (s+1) * s / 2 + (c-s) * (s+1);
- } else {
- count = total - (l+s-c+2) * (l+s-c+1) / 2;
- }
-
- int gcd = gcd(total, count);
- ret[0] = count / gcd;
- ret[1] = total / gcd;
- return ret;
- }
-
- int gcd(int a, int b) {
- int c = a % b;
- if ( c == 0 )
- return b;
- else
- return gcd(b, c);
- }
- }
复制代码
|
|