java:n个数中取出m个数,计算之和为S,求这几个数
来源:互联网 发布:软件开发的就业前景 编辑:程序博客网 时间:2024/05/21 07:48
背景:
现在的生活,越来越看重算法,有一天开了很多票,却只报了一部分;
也忘了报了几张,原来也不照相,只记了一个数,
算了,写个程序吧,一个一个试呗,递归算法;
来,看例子:
// 判断数组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>
好了,你也可以试试,关键是算法;
生活就是最好的老师;
0 0
- java:n个数中取出m个数,计算之和为S,求这几个数
- 求n个数选m个数的组合数
- 求n个数中最大k个数之和
- 求1到N个数中包含质因子m的数的个数
- [算法] 求排列组合: 从n个数中任选m个数组成一个新数
- 求M个数中前N个最大数(M>N)
- 从大小为n的数组中取出m个数的组合
- 从n个数中选择m个数,使其和为s
- 求n!中因子m的个数
- 关于从n个数中随机取出m个
- 输入二个数n ,m 求 m内所有之和等于n
- 从未知大小的n个数中取m个数,使各数被取出的概率相等
- //组合算法 从M个数中取出N个数,无顺序
- 想一想如何从M个数中随机等可能的取出N个数
- 求n个数和为m的组合数目
- 求1~n中与m互质的数的个数
- 求长度为n的序列中最大的m个数
- 算法面试题:在N个数中,找和为M的两个数
- 动态规划初步-完全背包问题
- App 提交 iTunes Connect 二进制无效问题解决方案
- 2个数组中遍历相同元素或不同元素
- HDU 2542 树上战争
- 这30个生活小技巧,不看后悔死……
- java:n个数中取出m个数,计算之和为S,求这几个数
- DPDK中L2fwd随笔
- HelloCharts:支持缩放、滚动和其它动画的图表库
- Log4j2 JDBCAppender的使用
- Lesson 6 Nehe
- java socket编程
- 聚类评价指标(整理)【图片看原文】
- Javamelody 监控java web项目配置
- MVC/MVP/MVVM等模式的基本概念