calculate sorted y of function y = ax^2 + bx + c given sorted x array
来源:互联网 发布:当代德军装备 知乎 编辑:程序博客网 时间:2024/05/08 14:25
给定一个一元二次方程式y = ax^2 + bx + c, 一个sorted array X, 将X中所有元素代入方程中,返回sorted的Y
solution: 看a< or > 0,,搞出两边的y们,然后merge
public class Ax2 { public static void main(String[] args) { } // x is sorted array private static ArrayList<Integer> sorted(int a, int b, int c, int[] x) { ArrayList<Integer> result = new ArrayList<Integer>(); if (a == 0) { return result; } // the tredence is decresing first then increasing if (a > 0) { ArrayList<Integer> list1 = new ArrayList<Integer>(); list1.add(getFun(a, b, c, x[0])); int i; for (i = 1; i < x.length; i++) { int y = getFun(a, b, c, x[i]); if (y > list1.get(i - 1)) { break; } list1.add(y); } //now the i is the index of first element is larger than previous ArrayList<Integer> list2 = new ArrayList<Integer>(); for (; i < x.length; i++) { int y = getFun(a, b, c, x[i]); list2.add(y); } //merge two arraylist result = mergeList(list1, list2); } else { //increasing first then decreasing ArrayList<Integer> list1 = new ArrayList<Integer>(); list1.add(getFun(a, b, c, x[0])); int i; for (i = 1; i < x.length; i++) { int y = getFun(a, b, c, x[i]); if (y < list1.get(i - 1)) { break; } list1.add(y); } //now the i is the index of first element is larger than previous ArrayList<Integer> list2 = new ArrayList<Integer>(); for (; i < x.length; i++) { int y = getFun(a, b, c, x[i]); list2.add(y); } //merge two arraylist result = mergeList(list1, list2); } return result; } private static int getFun(int a, int b, int c, int num) { return (int) (a * Math.pow(num, 2) + b * num + c); } private static ArrayList<Integer> mergeList(ArrayList<Integer> list1, ArrayList<Integer> list2) { ArrayList<Integer> result = new ArrayList<Integer>(); if (list1 == null && list2 != null) { return list2; } else if (list1 != null && list2 == null) { return list1; } int i = 0; int j = 0; while (i < list1.size() && j < list2.size()) { if (list1.get(i) < list2.get(j)) { result.add(list1.get(i)); i++; } else { result.add(list2.get(j)); j++; } } if (i != list1.size()) { for (; i < list1.size(); i++) { result.add(list1.get(i)); } } if (j != list2.size()) { for (; j < list2.size(); j++) { result.add(list2.get(j)); } } return result; }
0 0
- calculate sorted y of function y = ax^2 + bx + c given sorted x array
- 根据三个点(x1,y1)、(x2,y2)、(x3,y3),确定方程y=aX^2 + bX + c,带入X求得Y
- f(x,y)=ax*x+by+c 的抽象
- 关于ax+by=c的解x,y的min(|x|+|y|)值问题
- ax+by=c 且X∈[x1,x2] Y∈[y1,y2] 可行解组数 exgcd
- 拓展欧几里得-求AX+BY=C中X和Y的值
- 扩展欧几里德求解ax + by = c 的 最小正整数解 ( x, y)
- 求直线ax+by+c=0 上有多少个整点(x,y)满足x∈[-100,100],y∈[-100,100]
- CareerCup calculate (x^y)%z without pow();
- 【c语言】有一个函数: x < 1 --- y = x 1 <= x < 10 --- y = 2 * x - 1 x >= 10 --- y = 3 * x - 11 输
- 数学方法,证明:(x&y)+((x^y)>>1)=(x+y)/2
- 解方程: x+y=x|y
- CareerCup The number of pairs (x, y) of distinct elements with condition x + y <= Threshold
- 求ax^2+bx+c=0的根
- 求解ax^2+bx+c=0的根
- 解方程组ax^2+bx+c=0
- 解一元二次方程ax^2+bx+c=0
- 求出方程ax^2+bx+c=0的实根
- 9大内置对象、4大作用域、session的生命周期
- mysql 自定义方法创建 问题总结
- Objective-C 【格式化日期时间字符串】
- CodeForce 505C Mr. Kitayuta(DP+DFS)
- Unity3d游戏开发之C#开发(一)
- calculate sorted y of function y = ax^2 + bx + c given sorted x array
- IOS视频压缩
- Comparable 和 Comparator
- oracle修改密码永不过期(默认180天过期)
- td 边框加粗
- ANR类型及解决方法 && 基本算法 && HashMap原理
- 0.ring0-PAE-(虚拟地址转换成物理地址详细示例)
- 流量不清零:还在忽悠广大网民?
- 通过libcurl向服务端发送数据的2种方式:get/post