The brute force enumeration of algorithm questions in the frequent test interview

Source: Internet
Author: User

Combined with the 2017 spring strokes and autumn strokes of the real problem, the following types of algorithms are the most frequently tested, summed up a bit:

A lot of fish!

Cattle and cows have a fish tank. There are already n fish in the tank, the size of each fish is fishsize[i] (1≤i≤n, are all positive integers), cattle and cows now want to put the newly caught fish into the tank. There is a feeding frenzy law in the aquarium. After observation, cattle and cows found a fish a size twice to 10 times times the size of the other fish B (including twice times the size and 10 times times the size), fish A will eat fish B. With this in mind, the fish that cows and cows need to put in are guaranteed:
1, put in the fish is safe, will not be eaten by other fish
2, the fish can not be eaten in the other fish
Fish that already exist in the aquarium have been together for a long time, regardless of their prey to each other. Now know the size of the new fish into the range minsize,maxsize, cattle and cows want to know how many sizes of fish can be put into this fish tank.

Input Description:
The input data includes 3 rows.
The first behavior is newly put into the fish size range minsize,maxsize (1≤minsize,maxsize≤1000), separated by a space.
Second act the number of fish in the tank is already n (1≤n≤50)
The third behavior already has the size of the fish Fishsizei, separated by a space.

Output Description:
The output of how many sizes of fish can be put into this fish tank. Consider the size of the fish is an integer representation
Example 1
Input

1 1211

Output

3
Import Java.util.random;import Java.util.scanner;public class main{public static void Main (string[] args) {Scan        NER scanner = new Scanner (system.in);        System.out.println ("minSize:");        int minSize = Scanner.nextint ();        System.out.println ("maxSize:");        int maxSize = Scanner.nextint ();        System.out.println ("Innumber:");        int innumber = Scanner.nextint ();        System.out.println ("infishsize[]:");       int [] infishsize = new Int[innumber];       for (int i=0;i<innumber;i++) {infishsize[i]=scanner.nextint ();       } int count = 0;           for (int i=minsize;i<=maxsize; i++) {Boolean flag=false; for (int j=0;j<innumber; j + +) {if ((i*2<=infishsize[j]&&infishsize[j]<=i*10) | | | (infishsize[j]*2<=i && infishsize[j]*10>=i))                   {flag=true;               Break           }} if (!flag) {count++;   }    } System.out.println (count); }}
DNA synthesis

DNA molecule is a long chain of 4 kinds of deoxidation nucleotides, these 4 kinds of deoxidation nucleotides contain a,t,c,g four kinds of base respectively. Base complementary pairing principle: A and T are paired, and C and G are paired. If the length of the two base chain is the same and the base of each position is paired, then they can be paired to the double helix structure of the DNA. Two base chains are now given, allowing for substitution on one of the following: Replacing the base of a position on a sequence with another base. How many times does it take to get two base chain pairs to succeed?
Input Description:
The input includes one line:
Includes two strings representing two chains, two strings of the same length and lengths equal to 50.
Output Description:
Outputs an integer, which is the minimum number of times required to pair two base chains successfully
Example 1
Input

ACGTTGCA

Output

0
import java.util.Scanner;public class Main{    public static void main(String[] args){        Scanner scanner = new Scanner(System.in);        System.out.println("输入碱基s1 :");        String s1=scanner.nextLine();        System.out.println("输入碱基s2 :");        String s2= scanner.nextLine();    if(s1.length()!=s2.length()||s1.isEmpty()||s2.isEmpty()){    return;    }    int count =0;    for(int i=0;i<s1.length();i++){        if(!( (s1.charAt(i)==‘A‘ && s2.charAt(i)==‘T‘) ||            (s2.charAt(i)==‘A‘ && s1.charAt(i)==‘T‘) ||            (s1.charAt(i)==‘C‘ && s2.charAt(i)==‘G‘) ||            (s2.charAt(i)==‘C‘ && s1.charAt(i)==‘G‘) )){            count++;        }    }    System.out.println(count);    }}
consecutive integers

Niu Niu's good friend sheep and Goat wrote on the paper n+1 an integer, the goat then wiped out an integer, gave niuniu to guess what figure he erased. Niuniu know that the sheep and sheep are written in a sequence of integers followed by a sequence of positive integers, and cattle cows now have to guess all the integers that may be erased. For example:
10 7 12 8 11 Then the erased integer could be only 9.
5 6 7 8 then the erased integer may be 4 or 9.
Input Description:
The input includes 2 rows:
First behavior integer n (1 <= n <= 50), which is the number of digits left after erasing a number
Second behavior n integers num[i] (1 <= num[i] <= 1000000000)
Output Description:
In a row output all may be wiped out of the number, from small to large output, separated by a space, no space at the end of the line. If there are no possible numbers, the output mistake
Example 1
Input

23 6

Output

mistake
Import Java.util.scanner;public class main{public static void Main (string[] args) {Scanner scanner=new Scanner (S       ystem.in);       System.out.println ("Number of remaining digits:");       int N=scanner.nextint ();       int [] num=new int[n];       SYSTEM.OUT.PRINTLN ("Input remaining number:");       int max=integer.min_value;       int min=integer.max_value;       int s=0;       for (int i=0;i<n;i++) {num[i]=scanner.nextint ();       S+=num[i];       if (num[i]<min) min=num[i];       if (Num[i]>max) max=num[i]; if (max-min+1 = = N) {if (s== (max+min) *N/2) {if (min>1) {System.out.print               ln ((min-1) + "" + (max+1));               }else {System.out.println (max+1);           }}else {System.out.println ("mistake!");                   }}else {if (max-min==n) {for (int j=1;j<n;j++) {if (Num[j]==num[j-1]) {               System.out.println ("mistake!"); }else if (num[j]==num[j-1]+2) {System.out.println (num[j]-1);           }}}else {System.out.println ("mistake!"); }       }    }}
Sequence and 01 Flip

Longest common continuous substring

Assembling triangles

The smallest rectangle

String classification

The beautiful palindrome string

Go to the company

Adjust formation

Collection

Painting Board

Small easy to remember words

Split biscuits

Buy a Hat

The degree of the bear Home

Looking for triangles

Funny sort of

Magic number

adding characters

Array transformations

Java代码实现

The brute force enumeration of algorithm questions in the frequent test interview

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.