Oh, there's a story between three numbers.

Source: Internet
Author: User
Oh, the story between the three numbers... hey, the title party is coming again. My friend asked me a question. how can I judge whether the three numbers are correct? For example, 123321456654465... 901901019, that is, determining whether the three numbers are connected members based on the numerical values. my idea is to split them first and obtain the smallest of the three numbers, then, determine whether the remaining two are in it, for example, 321, get 1, and then judge the story where the three numbers are ambiguous...
Hey, the title party is coming again. My friend asked me a question. how can I judge whether the three numbers are correct? For example, 123 321 456 654 465... 901 901 019

That is, determine whether the three numbers are connected members based on the numerical value.

My idea is to split it first, get the smallest of the three numbers, and then judge whether the remaining two are in it.
For example, if 321 is used to obtain 1 and then judge whether 23 exists, you can determine if it exists.

The special 901 directly defines an array to determine whether it contains 0. if it is to determine whether it contains 9 and 1... (here it may be necessary to exclude 012)

Do you have any other easier methods?



------ Solution --------------------
It can be judged by another operator. for example, if a value is 234 and 2 is taken out, the second digit is 3 or 1. record whether it is increment or decrement, and then recursively judge the next n digits, this method can be used to determine multiple numbers.
------ Solution --------------------
My idea is: only true to the number of students:
Directly intercept it into the array! Then the array is sorted in a large order, and the numbers on both sides are compared and judged by the number in the middle;


------ Solution --------------------
Think of one. The sum of values is 10 in the special case of 019, and the sum of values in other cases is * 3. the code is as follows:
PHP code
Function foo ($ s) {$ arr = str_split ($ s); if (in_array ('9', $ arr) return (array_sum ($ arr) = 10 ); sort ($ arr, SORT_NUMERIC); return array_sum ($ arr) ==$ arr [1] * 3;} var_dump (foo ('019 ')); // bool (true) var_dump (foo ('20140901'); // bool (true)
------ Solution --------------------
Generally, for $ n, $ n, {123 321 456 654 465 ..}
You can:
$ T = str_split ($ n );
If (array_sum ($ t) = (reset ($ t) + end ($ t) * count ($ t)/2) echo 'OK ';

However, when 901 and 109 are considered continuous, they are beyond the average cognition.
------ Solution --------------------
It cannot be judged that the sum can be divided by 3, for example, 258 ..
------ Solution --------------------
PHP code
$temp = '0123456789 089 019';$samples = array(123, 321, 456, 654, 465, 901, 109, '019', 890, 908, 135, 250);foreach ( $samples as $value ) {    $ds = str_split( $value );    sort( $ds );    $ds = implode( '', $ds );    $result = ( strstr( $temp, $ds ) !== false ) ? 'yes' : 'no';    echo "{$value} - {$ds} : {$result}
";}

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.