Remember the algorithm exercise

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Algorithms have always been their own weaknesses. Learn the language by algorithm

Java Edition:

/** * Algorithm Exercise 1 * Created by Exile on 2017/5/25. * 1 N (n is even) number, divide the number into N/2 group, make each group of numbers and equal. */public class ArithmeticTest1 {//topic 1 even number of arrays public static void main (string[] args) {int[] Evennumberarry = n        EW Int[]{1, 7, 2, 7, 6, 5, 2, 8, 3, 4};        int arrynum = evennumberarry.length/2;//1 calculates sum;        int sum = 0;        for (int i:evennumberarry) {sum = sum + i;        }//2 calculates the number of each group and how many int arrymax = Sum/arrynum;            for (int i = 0; i < evennumberarry.length; i++) {int value = Arrymax-evennumberarry[i]; int temp;//assumes that there must be a set of for (int j = i + 1; j < Evennumberarry.length; J + +) {if (even                    NUMBERARRY[J] = = value) {temp = evennumberarry[i + 1];                    Evennumberarry[i + 1] = value;                EVENNUMBERARRY[J] = temp;        }}} for (int i:evennumberarry) {System.out.println (i);   } }}import java.util.arraylist;import java.util.random;/** * Algorithm Exercise 2 * Created by Exile on 2017/5/25. * NXN chess board, each lattice can put a car, there are M cars, Q: After each car put out, how many grid is left out of all the car attack range?        * Cars can overlap */public class ArithmeticTest2 {public static void main (string[] args) {//n and M int m = 3;        int n = 3;        arraylist<model> modellist = createmodellist (m, n);        arraylist<model> manlist = new arraylist<> ();        for (model Model:modellist) {System.out.println ("Current model:" + model.tostring ());                }//Method 1: Repeat from start to end (int i = 0; i < n; i++) {for (int j = 0; J < N; j + +) {            Manlist.add (New Model (I, j)); }} System.out.println ("Pre-Filter:" + manlist.size ());//The problem is too fast for (int i = 0; i < modellist . Size (); i++) {for (int j = 0; J < Manlist.size (); j + +) {if (!modellist.get (i). Getattackoutcall (manl       Ist.get (j))) {Manlist.remove (j);         }}}//} System.out.println ("Filtered:" + manlist.size ());        for (Model model:manlist) {System.out.println (model.tostring ()); }}/** * generates a corresponding number of M objects based on the MN input * * @param m * @param n * @return */private static ArrayList        <Model> createmodellist (int m, int n) {arraylist<model> models = new arraylist<> ();        for (int i = 0; i < m; i++) {Models.add (new Model (n));    } return models;        } public static class Model {private int x;        private int y;            public Model (int n) {random random = new random ();            this.x = Random.nextint (n);        This.y = Random.nextint (n);            } public Model (int x, int y) {this.x = x;        This.y = y;        } public int GetX () {return x;        } public int GetY () {return y; } @Override PubliC String toString () {return "model{" + "x=" + x + ", y=" + y +        '}'; } public boolean Getattackoutcall (Model mainmodel) {if (this.getx () = = Mainmodel.getx () | | this.gety ()            = = Mainmodel.gety ()) {return false;        } return true; }    }}

Version Golang:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.