背景:
现在的生活,越来越看重算法,有一天开了很多票,却只报了一部分;
也忘了报了几张,原来也不照相,只记了一个数,
算了,写个程序吧,一个一个试呗,递归算法;
来,看例子:
// 判断数组b对应的数组a中的元素之和是否为s
public static boolean isSum(int a[], int b[], int n, int s) {
int ret = 0;
for (int i = 0; i < n; i++) {
ret += a[b[i]];
}
return s == ret;
}
// a:初始数组
// n:a包含的元素个数
// m:选出元素的个数
// b:存放结果的数组
// bn:数组b的元素个数,等于m
public static void combine(int a[], int n, int m, int b[], int bn, int s) {
for (int i = n; i >= m; i--) {
b[m - 1] = i - 1;
//System.out.println("数据为m"+m+"数据b[m - 1]"+b[m - 1]);
if (m > 1) { // 一共要选m个数,故m = 1时才结束递归
combine(a, i - 1, m - 1, b, bn, s);
} else {
// for (int j = bn - 1; j >= 0; j--) {
// System.out.println("判断之前数据为" + a[b[j]]);
//
// }
//
if (isSum(a, b, bn, s)) {
for (int j = bn - 1; j >= 0; j--) {
System.out.println("数据为" + a[b[j]]);
}
System.out.println("数据为空");
}
}
}
}
// 从包含n个元素的数组a中任选m个数,使其各为s,输出所有的组合
public static void combine(int a[], int n, int m, int s) {
int b[] = new int[m];
combine(a, n, m, b, m, s);
}
public static void main(String[] args) {
int[] asjy = { 9483, 9886, 9543, 9750, 8645, 8958, 8725, 8629, 9770, 9756, 8890, 8943, 8639, 9365, 8190,
9468 };
//combine(asjy, 16, 4, new int[4], 4, 46437);
combine(asjy, 16, 5, new int[5], 5, 46437);
}
为了方便,后期用,我就写了一个Android的,按在手机上,挺方便;
java代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtInpN = (EditText) findViewById(R.id.edt_input);
edtInpM = (EditText) findViewById(R.id.edt_input_M);
edtOutS = (EditText) findViewById(R.id.edt_output_sum);
tvwRus = (TextView) findViewById(R.id.tvw_result);
lsRus = (ListView) findViewById(R.id.ls_result);
btnSt = (Button) findViewById(R.id.btn_start);
btnSt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!edtInpM.getText().toString().equals("") && !edtOutS.getText().toString().equals("")) {
int m = Integer.valueOf(edtInpM.getText().toString());
int s = Integer.valueOf(edtOutS.getText().toString());
String inputN = edtInpN.getText().toString();
String[] ss = inputN.split(":");
int[] b = new int[ss.length];
for (int i = 0; i < ss.length; i++) {
b[i] = Integer.valueOf(ss[i]);
}
combine(b, b.length, m, s);
StringBuilder buffer = new StringBuilder();
if (content.size()!=0) {
for (int i = 0; i < content.size(); i++) {
buffer.append(content.get(i) + ";");
}
tvwRus.setText(buffer);
}else{
tvwRus.setText("未找到结果");
}
}
}
});
// SimpleAdapter adapter=new SimpleAdapter(getApplicationContext(),
// content, andr, from, to)
}
xml代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.mathsu.MainActivity" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="大苏计算器" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="输入N个数,用':'隔开,但是不可以换行" />
<EditText
android:id="@+id/edt_input"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@android:color/holo_blue_light"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="计算其中几个数:" />
<EditText
android:id="@+id/edt_input_M"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/shape"
android:inputType="number|text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="计算之和为:" />
<EditText
android:id="@+id/edt_output_sum"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/shape"
android:inputType="number|text"
android:singleLine="true"
android:textColor="#FFFAFA" />
</LinearLayout>
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tvw_result"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left"
android:text="计算结果为:" />
<ListView
android:id="@+id/ls_result"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
好了,你也可以试试,关键是算法;
生活就是最好的老师;