选择算法java实现代码 算法最好使用java进行

JAVA从数组中选取N个数,使其和为M实现算法?

1、功能:随机生成n个数并求和

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:国际域名空间、雅安服务器托管、营销软件、网站建设、偏关网站维护、网站推广。

2、以下是代码(代码里取n=10):

public class Sum {

public static void main(String[] args) {

double i;//随机生成的数字

double sum=0;

for(int j=0;j10;j++)

{

i=(double) Math.random();

System.out.println("第"+(j+1)+"次生成的数字是"+i);

sum+=i;

}

System.out.println("它们的和是:"+sum);

}

}

JAVA选择排序算法方法

下面是我自己定的一个int数组排序的工具,希望对你有帮助。

package net.ftng.util.Order;

public class IntArrayOrder {

/**

* @param args

*/

public static void main(String[] args) {

int[] a = { 12, 6, 14, 7, 18, 9 };

System.out.print("noOrderByAnything====:");

for (int t : a) {

System.out.print(t + " ");

}

System.out.println();

System.out.print("orderByAscend====:");

int[] temp = lightOrderByAscend(a);

for (int t : temp) {

System.out.print(t + " ");

}

System.out.println();

System.out.print("orderByDescend====:");

temp = lightOrderByDescend(a);

for (int t : temp) {

System.out.print(t + " ");

}

}

static public int[] lightOrderByAscend(int[] args) {

try {

int temp;

int[] tempargs = args.clone();

int argsLength = tempargs.length;

for (int i = 0; i argsLength; i++) {

for (int j = i + 1; j argsLength; j++) {

if (tempargs[i] tempargs[j]) {

temp = tempargs[i];

tempargs[i] = tempargs[j];

tempargs[j] = temp;

}

}

}

return tempargs;

} catch (Exception e) {

return null;

}

}

static public int[] deepOrderByAscend(int[] args) {

try {

int temp;

int argsLength = args.length;

for (int i = 0; i argsLength; i++) {

for (int j = i + 1; j argsLength; j++) {

if (args[i] args[j]) {

temp = args[i];

args[i] = args[j];

args[j] = temp;

}

}

}

return args;

} catch (Exception e) {

return null;

}

}

static public int[] lightOrderByDescend(int[] args) {

try {

int temp;

int[] tempargs = args.clone();

int argsLength = tempargs.length;

for (int i = 0; i argsLength; i++) {

for (int j = i + 1; j argsLength; j++) {

if (tempargs[i] tempargs[j]) {

temp = tempargs[i];

tempargs[i] = tempargs[j];

tempargs[j] = temp;

}

}

}

return tempargs;

} catch (Exception e) {

return null;

}

}

static public int[] deepOrderByDescend(int[] args) {

try {

int temp;

int argsLength = args.length;

for (int i = 0; i argsLength; i++) {

for (int j = i + 1; j argsLength; j++) {

if (args[i] args[j]) {

temp = args[i];

args[i] = args[j];

args[j] = temp;

}

}

}

return args;

} catch (Exception e) {

return null;

}

}

}

初学者:用java程序写一个选择排序算法!

选择排序法:

public class TSort{

public static void main(String args[]){

int a[]={12,45,2,5,26,56};

for(int i=0;ia.length-1;i++){

int t;

for(int j=i+1;ja.length;j++){

if(a[i]a[j]){

t=a[i];a[i]=a[j];a[j]=t;

}

}

}

for(int i=0;ia.length;i++){

System.out.print(a[i]+" ");

}

}

}

JAVA 实现算法

package huda.laogao.ON_20121216;

import java.util.*;

public class GetEquation {

ArrayListInteger params = null;// 进行加减的数,长度设为n

int result = 0;// 结果

public GetEquation(ArrayListInteger params, int result) {

this.params = params;

this.result = result;

}

public void run() {

int size = params.size();

if (size == 1) {

if (params.get(0) == result)

System.out.println(result + "=" + result);

else

System.out.println("Invalid");

return;

}

int[][] matrix = getMatrix(size - 1);

int m = matrix.length;// 行数

int count = 0;// 记录符合结果的组合数

for (int i = 0; i m; i++) {

int now = params.get(0);// 进行加减的结果,初始为第一个值

// 从第二个数开始进行加或减,加的话就相当于该数乘上1,然后加到结果上,

// 减的就相当于该数乘上-1,然后加到结果上

for (int j = 1; j size; j++) {

now += matrix[i][j - 1] * params.get(j);

}

// System.out.println(now);

// 判断结果

if (now == result) {

count++;// 组合数加1

System.out.print(params.get(0));

for (int j = 1; j size; j++) {

if (matrix[i][j - 1] == 1)

System.out.print("+" + params.get(j));

else

System.out.print("-" + params.get(j));

}// for j

System.out.println("=" + result);

}// if

}// for i

if (count == 0)

System.out.println("Invalid");

}

public int[][] getMatrix(int n) {

int m = (int) Math.pow(2, n);

int matrix[][] = new int[m][n];

for (int i = 0; i n; i++) {

int num = (int) Math.pow(2, i + 1);

// 总共分 2^(i+1)块,如i=0,即该矩阵的第一列,可分为2块,上一块取1,下一块取-1

int size = (int) Math.pow(2, n - i - 1);

// 每块的大小为2^(n-i-1),如n=3,i=0,每块的大小为4,即前4个为1,后4个为-1

int flag = 1;// 先为+

for (int j = 0; j num; j++) {

for (int k = 0; k size; k++) {

matrix[k + j * size][i] = flag;

}

flag *= -1;

}

}

return matrix;

}

public void print(int[][] matrix) {

for (int i = 0; i matrix.length; i++) {

for (int j = 0; j matrix[i].length; j++) {

System.out.print(matrix[i][j] + "\t");

}

System.out.println();

}

}

public static void main(String args[]) {

ArrayListInteger params = new ArrayListInteger();

params.add(1);

params.add(2);

params.add(3);

params.add(4);

int result = 10;

GetEquation ge = new GetEquation(params, result);

ge.run();

}

}

运行结果为:1+2+3+4=10

注:难点为获取加减矩阵,可辅助print()函数帮助理解

望采纳

35选7 用java代码实现

public class mainClass {

public mainClass() {

}

public static void main(String []args){

int [] num = new int[35];

boolean [] flag = new boolean[35];

int count = 0;

for(int i=0;i35;i++)

{

num[i] = i + 1;

flag[i] = true;

}

while(count!=7){

int k=(int)(Math.random()*35);

if(flag[k]){

System.out.println(num[k]);

flag[k]=false;

count++;

}

}

}

}

求java实现C语言常用算法的程序(如各种排序算法),以及Java考题

Java 算法:

public class Sort {

public void swap(int a[], int i, int j) {

int tmp = a[i];

a[i] = a[j];

a[j] = tmp;

}

public int partition(int a[], int low, int high) {

int pivot, p_pos, i;

p_pos = low;

pivot = a[p_pos];

for (i = low + 1; i = high; i++) {

if (a[i] pivot) {

p_pos++;

swap(a, p_pos, i);

}

}

swap(a, low, p_pos);

return p_pos;

}

public void quicksort(int a[], int low, int high) {

int pivot;

if (low high) {

pivot = partition(a, low, high);

quicksort(a, low, pivot - 1);

quicksort(a, pivot + 1, high);

}

}

public static void main(String args[]) {

int vec[] = new int[] { 37, 47, 23, -5, 19, 56 };

int temp;

//选择排序法(Selection Sort)

long begin = System.currentTimeMillis();

for (int k = 0; k 1000000; k++) {

for (int i = 0; i vec.length; i++) {

for (int j = i; j vec.length; j++) {

if (vec[j] vec[i]) {

temp = vec[i];

vec[i] = vec[j];

vec[j] = temp;

}

}

}

}

long end = System.currentTimeMillis();

System.out.println("选择法用时为:" + (end - begin));

//打印排序好的结果

for (int i = 0; i vec.length; i++) {

System.out.println(vec[i]);

}

// 冒泡排序法(Bubble Sort)

begin = System.currentTimeMillis();

for (int k = 0; k 1000000; k++) {

for (int i = 0; i vec.length; i++) {

for (int j = i; j vec.length - 1; j++) {

if (vec[j + 1] vec[j]) {

temp = vec[j + 1];

vec[j + 1] = vec[j];

vec[j] = temp;

}

}

}

}

end = System.currentTimeMillis();

System.out.println("冒泡法用时为:" + (end - begin));

//打印排序好的结果

for (int i = 0; i vec.length; i++) {

System.out.println(vec[i]);

}

//插入排序法(Insertion Sort)

begin = System.currentTimeMillis();

for (int k = 0; k 1000000; k++) {

for (int i = 1; i vec.length; i++) {

int j = i;

while (vec[j - 1] vec[i]) {

vec[j] = vec[j - 1];

j--;

if (j = 0) {

break;

}

}

vec[j] = vec[i];

}

}

end = System.currentTimeMillis();

System.out.println("插入法用时为:" + (end - begin));

//打印排序好的结果

for (int i = 0; i vec.length; i++) {

System.out.println(vec[i]);

}

//快速排序法(Quick Sort)

Sort s = new Sort();

begin = System.currentTimeMillis();

for (int k = 0; k 1000000; k++) {

s.quicksort(vec, 0, 5);

}

end = System.currentTimeMillis();

System.out.println("快速法用时为:" + (end - begin));

//打印排序好的结果

for (int i = 0; i vec.length; i++) {

System.out.println(vec[i]);

}

}

}

以下是运行结果:

选择法用时为:234

56

47

37

23

19

-5

冒泡法用时为:172

56

47

37

23

19

-5

插入法用时为:78

56

47

37

23

19

-5

快速法用时为:297

56

47

37

23

19

-5


网站标题:选择算法java实现代码 算法最好使用java进行
分享网址:http://ybzwz.com/article/hgpsgs.html