一个比较复杂的问题
我先把问题描述一下
项目现有的状态,表中有四个字段 1C 2C 4C 6C
从页面当中接受一个值是8C ,现在的问题是如何把8C转换成2C和6C,分别取2c和6c的值
如果页面时12C,6C和6C是正确 6C和4C 和2C是错误
这样的算法有吗,请教给我。
------解决方案--------------------
得到的数据A
变量x{x|6c,4c,2c,1c} ,变量的值是由6C到1C的降
(1)如果他是大于x的,那么,可以分x,然后A = A - x;否测 x降一级,如果x = 0跳(3)。
(2)重复(1);
(3)结束;
------解决方案--------------------
PS:大于等于x的
------解决方案--------------------
能不能把,所有的情况都说一下那?
------解决方案--------------------
UP
------解决方案--------------------
- Java code
class test1{ private int[] resultArray; // split result. private int flag; test1(int length){ resultArray = new int[length]; } public static void main(String[] args){ /* opera */ int splitRes = 25; /* split */ test1 split = new test1(splitRes/6 + 2); split.split(splitRes); /* result output */ System.out.println("input =" + splitRes); for(int i =0; i < split.flag; i ++){ System.out.println("resultArray[" + i + "] = " + split.resultArray[i]); } } /* split */ void split(int splitRes){ if (splitRes >= 6){ splitRes -= 6; resultArray[flag] = 6; flag += 1; split(splitRes); }else if( splitRes >=4 ){ splitRes -= 4; resultArray[flag] = 4; flag += 1; split(splitRes); }else if( splitRes >=2){ splitRes -= 2; resultArray[flag] = 2; flag += 1; split(splitRes); }else if( splitRes >=1){ splitRes -= 1; resultArray[flag] = 1; flag += 1; split(splitRes); } }}
------解决方案--------------------
- Java code
public class split2 { public static void main(String[] args) { int amountOf6 = 0; // the Numbers of 6. int amountOf4 = 0; // the Numbers of 4. int amountOf2 = 0; // the Numbers of 2. int amountOf1 = 0; // the Numbers of 1. int pos = 0; /* opera*/ int splitRes = 27; //change the NO. here. int[] resultArray = new int[splitRes/6 + 2]; System.out.println("input =" + splitRes); /* count the Numbers of 6. */ amountOf6 = splitRes/6 ; splitRes = splitRes%6; for (int i =0 ; i< amountOf6 ; i++ ){ resultArray[i] = 6; } /* count the Numbers of 4. */ amountOf4 = splitRes/4 ; splitRes = splitRes%4; for (int i =0 ; i< amountOf4 ; i++ ){ resultArray[i +amountOf6] = 4; } /* count the Numbers of 2. */ amountOf2 = splitRes/2 ; splitRes = splitRes%2; for (int i =0 ; i< amountOf2 ; i++ ){ resultArray[i+ amountOf6+amountOf4] = 2; } /* count the Numbers of 1. */ if (splitRes==1){ amountOf1 = 1; resultArray[amountOf6+amountOf4+ amountOf2] = 1; } /* result output */ for(int i =0; i < resultArray.length; i++){ System.out.println("resultArray[" + i + "] = " + resultArray[i]); } }}