日期:2014-05-20  浏览次数:20821 次

求教,关于一取整算法的思路?
max,min两个数
1.max要求向上取整,min   要求向下取整,
2.max,min要求是以0.5,5。00,结尾
3.(max-min)/int(x)   的值是   0.5,5   ,00结尾的

如:max   =123   向上取整   125

min   =17   向下取整   15

如果是小数   max   =0.7   取整1.0,

min   =0.2向下取整0.0


------解决方案--------------------
int i = 1;
int j= 1;
while(max < 1)
{
max = max *10;
i = i * 10;
}
max = ((max/5 + 1) * 5) / i;
while(min < 1)
{
min = min * 10;
j= j*10;
}
min = ((min/5 - 1) * 5)/j;


我不是用java写的,对于int,double,BigDecimal考虑得也不多,就是给这么个思路,也不知道对不对,如果能给你点启发也行,楼下的给出好代码啊

------解决方案--------------------
int i=0;
while(max <0){
int ga=rount(max);
if(ga==1||ga==0){
break;
}
}
while(max> 0){
if(max%5 <3){
max=max-i;
if(max%5==0){
break;
}
}else if(max%5> 2){
max=max+i;
if(max%5==0){
break;
}
}
i++;
}

这样不知可不可以试试吧!

------解决方案--------------------
public class QuZheng {

public static void main(String[] args) {

String input = "123.45 ";//enter a number
boolean isXiaoShu = false;
int point = 0;

int length = input.length();
for (int i = 0; i < length; i++) {
if (input.charAt(i) == '. ') {
isXiaoShu = true;
point = i;
break;
}
}
if (isXiaoShu) {
double max, min;
double doubleInput = Double.parseDouble(input);
String subInput = input.substring(point);
String zhengInput = input.substring(0, point);
double zhengDoubleInput = Double.parseDouble(zhengInput);
double subDoubleInput = Double.parseDouble(subInput);
if (subDoubleInput > 0 && subDoubleInput < 0.5) {
max = zhengDoubleInput + 0.5;
min = zhengDoubleInput;
} else if (subDoubleInput > 0.5 && subDoubleInput < 1.0) {
max = zhengDoubleInput + 1;
min = zhengDoubleInput + 0.5;

} else {
max = zhengDoubleInput + 0.5;
min = zhengDoubleInput - 0.5;
}

System.out.println( "max = " + max + " min = " + min);
}

else {
int max, min;
int intInput = Integer.parseInt(input);
int last = intInput % 10;
if (last > 5 && last < 10) {
max = intInput + (10 - last);
min = intInput - (last - 5);

} else if (last > 0 && last < 5) {
max = intInput + (5 - last);
min = intInput - last;
} else {
max = intInput + 5;
min = intInput - 5;
}
System.out.println( "max = " + max + " min = " + min);
}

}
}

这个方案可以
------解决方案--------------------
public class InAttentionTo {
public static void main(String[] args){
String max = "123.5 ";
String min = "234.0 ";
double powerN = 1;

if(max.indexOf( ". ") != -1){
int len = max.length() - max.indexOf( ". ") - 1;
powerN = Math.pow(10,len);
}
max = " " + getRtn(Double.parseDouble(max) * powerN, true) / powerN;

powerN = 1;
if(min.indexOf( ". ") != -1){