The sword refers to the offer face question 44: The card's cis-child Java implementation __java

Source: Internet
Author: User
title: The Shun son of poker randomly draw 5 cards from the poker, judge whether it is a CIS, that is, the five cards are not continuous. 2--10 is the number itself, A is 1,j to 11,q 12,k is 13, and the size Wang any number.

Algorithm Analysis:We need to abstract the background of playing cards into computer language. It's not hard to imagine that we could see five cards as an array of five digits. The size of the king is a special number, we might as well define them as 0, so that can be separated from the other cards. The next step is to determine whether 5 numbers are sequential, and the most intuitive way is to sort the arrays. It's worth noting that since 0 can be an arbitrary number, we can use 0 to fill the blanks in the array. If the array after the sort is not contiguous, that is, the adjacent two digits are like several digits, but as long as we have enough 0 to fill the two vacant digits, the array is actually contiguous. For example, after array sorting is {0,1,3,4,5}, there is a 2 vacancy between 1 and 3, just as we have a 0, that is, we can take it as 2 to fill this vacancy. So we need to do 3 things: First, sort the array, then count the number of 0 in the array, and then the total number of vacancies between the adjacent digits in the array after the sorting. If the vacancy summary is less than or equal to 0, then the array is contiguous, otherwise discontinuous. Finally, we need to note that if a non-0 number in the array repeats, the array is not contiguous. The way to describe poker is that if a pair of cards contains a pair, it cannot be a cis.
algorithm source program:
/************************************************************** * Copyright (c) 2016, * All rights reserved. * Version Number: v1.0 * Topic Description: Card of the Shun son * from playing cards randomly out of 5 card, judge is not a shun, that is, the five cards are not continuous.
2--10 is the number itself, * A is 1,j to 11,q 12,k is 13, and the size Wang any number.
* Input Description: Please enter five card: * 0 0 3 4 7 * Program output: This is a straight poker * problem analysis: no * algorithm Description: So we need to do 3 things: first to the array, and then statistics the number of 0 in the array, the last statistical sorting after the number of adjacent numbers in the total number of vacancies.
* If the vacancy summary is less than or equal to 0, then the array is contiguous, otherwise it is discontinuous. * Date of Completion: 2016-09-27 ***************************************************************/Package

Org.marsguo.offerproject44;

Import Java.util.Scanner;
		Class solutionmethod1{public void Sortfun (int[] arrays,int start, int end) {int numofzero=0;
		if (start>=end) {//To determine whether the start and end of the array are the same, the same expression has been all queued, returns return;								int i = start;								I point to the starting bit int j = end of the array;						J points to the bottom of the array int key = Arrays[i];						Select the first bit of the array to be the keyword key, and the datum element boolean flag = true;		Set the flag bit to judge whether it is i++ or j--; This is important while (I!= j) {//if i≠j, which means that the left and right sides of the keyword are not yet the smallest and largest if (flag) {			
				if (Key>arrays[j]) {//from the backward forward traversal, find the value of less than key, swap (ARRAYS,I,J);
				Arrays[i] is exchanged with this value after finding a value less than key flag = false;
				}else{//If not found j--, forward traversal j--;				}}else{if (Key<arrays[i]) {//traverse backwards to find a value of greater than key swap (ARRAYS,I,J);
				Swap this value with ARRAYS[J] flag = true;
				}else{//If no word is found, it will i++ and traverse i++;								}}//sprint (arrays);					Prints the array sortfun (ARRAYS,START,J-1) after each sort;
		Recursive invocation, the first half of the datum element is sorted again by this method until all is finished.					Sortfun (Arrays,i+1,end);
		
		Recursive invocation, the second half of the datum element is sorted again by this method until all is finished.
		System.out.println ("Sorted array is:");
			for (int k = 0; k < arrays.length; k++) {if (arrays[k) = = 0) {numofzero++; }//system.out.print ("numofzero=" + Numofzero + ";")
			+arrays[k] + ",");
		System.out.print (Arrays[k] + ",");
	} iscontinuousfun (arrays, Numofzero);
		public void Swap (int[] Array,int i,int j) {int temp;
		temp = Array[i];
		Array[i] = Array[j];
	ARRAY[J] = temp; } public void Iscontinuousfun(int[] Array,int Numofzero)				{int numofgap = 0;
		Determine how many 0//system.out.println ("Numberofzero =" + Numofzero) are empty in the middle of the number;		for (int j = 1; J < Array.Length J + +) {int flag = Array[j]-array[j-1]; With the last digit in the sorted array-the previous number if (array[j] = = Array[j-1] && Array[j]!= 0 {//If there is a duplicate non-0 number in the array, it is not a cis, exit System.out.print
				ln ("There is a repeat number, not a CIS card");
			Return 								else if (flag!= 1 && flag!= 0 && array[j-1]!= 0) {//judgment is not a sequential number, nor 0, numofgap + = flag-1;
		Number of missing digits between 0 digits} if (Numofzero!= numofgap) {System.out.println ("This is not a cis-card");
		else{System.out.println ("This is a straight poker"); }} public class Iscontinuous {public static void main (string[] args) {Scanner Scanner = new Scanner (system.in)						;
		Scan keyboard Input System.out.println ("Please enter five card:");								String str = scanner.nextline ();									Converts keyboard input to string string[] temp = Str.split ("");
		Converts a string to an array of strings by "" Scanner.close ();								int[] array = new Int[temp.length]; Defines an integral array of arrays foR (int i = 0; i< temp.length; i++) {//force the array of strings into an integer array array[i] = Integer.parseint (temp[i));
		This method is very ingenious} SolutionMethod1 Solution1 = new SolutionMethod1 ();
		Solution1.quicksort (array, 0, array.length-1);
	Solution1.sortfun (array, 0, array.length-1);
 }
}

Program Run Result:
Related Article

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.