Little algorithmic problem, no place to record, it's here.

Source: Internet
Author: User
Tags greatest common divisor mul

1. Dichotomy

Int[] arr = {1,2,3,4,5,6,7,8,9,10};
int key = 2;
int min, Max, mid;
min = 0;
max = arr.length-1;
Mid = (Min+max) >>1;//(Min+max)/2
while (Arr[mid]!=key) {
if (Key>arr[mid]) {
min = mid+1;
}else if (Key<arr[mid]) {
max = mid-1;
}
if (max<min) {
Return
}
Mid = (Min+max) >>1;//(Min+max)/2
}
System.out.println (mid);

2. Bubble sort 

Int[] arr1 = {21,14,56,2,5,16,27,83,9,10};
int common;
for (int i=0;i<arr1.length;i++) {
for (int j=i+1;j<arr1.length;j++) {
if (Arr1[i]<arr1[j]) {
common = arr1[i];
Arr1[i] = Arr1[j];
ARR1[J] = common;
}
}
}
System.out.println (arrays.tostring (arr1));

3, beg greatest common divisor, least common multiple

System.out.println ("Please enter the first number:");
Scanner sc=new Scanner (system.in);
int a = Sc.nextint ();
System.out.println ("Please enter a second number:");
int b = Sc.nextint ();
int C;
int mul = 1;
int num = 1;
if (a>b) {
C=a;
A=b;
B=c;
}
for (int i=2;i<a;i++) {
if (a%i==0&&b%i==0) {
a=a/i;
b=b/i;
Mul*=i;
num = i;
}
}
SYSTEM.OUT.PRINTLN ("Least common Multiple:" +a*b*mul);
System.out.println ("Greatest common divisor:" +num);

Little algorithmic problem, no place to record, it's here.

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.