One practice per day-longest platform Problem

Source: Internet
Author: User

Go to the hall, go down to the kitchen, write code, and flip the walls. Welcome to the unstoppable daily practice!


Question: longest platform Problem


Content: AlwaysSorted from small to largeThe platform in this array is a continuous string.Same ElementAnd the element cannot be extended.

For example, in, and, all are platforms. write a program, accept an array, and find the longest platform in the array. In this example, 3, 3, and 3 are inLongest Platform.


Note:
This program is very simple, but it is not easy to write. Therefore, you should consider the following points when writing the program:
1. Use VariableThe fewer the better
2. Is it possible to only store each element of the array Query only onceThe result is displayed.
3. Program The fewer statements, the better..

Ps: this problem has plagued David Gries, a well-known computer scientist.


My solution: I did not think much about it. I opened vs2013 and knocked it up. The problem was really simple, and it was superb in minutes .. Sorry, no!

# Include
 
  
Using namespace std; int _ tmain (int argc, _ TCHAR * argv []) {int index = 0; // array subscript index int indexEnd = 0; // target index int count = 0; // counter int tempCount = 0; // temporary counter int arrayNum [100] = {0}; // Zero is not an input element, therefore, cout <"enter a sequence of numbers separated by spaces. 0 indicates the end of the input:" <endl; while (cin> arrayNum [index]) & arrayNum [index]! = 0) ++ index; index = 0; while (arrayNum [index]! = 0) {// cout <arrayNum [index] <endl; if (arrayNum [index + 1]! = 0) {if (arrayNum [index] = arrayNum [index + 1]) {tempCount ++;} else {if (tempCount> count) {count = tempCount; indexEnd = index;} tempCount = 0 ;}++ index ;}cout <"input number sequence:" <endl; index = 0; while (arrayNum [index]! = 0) {cout <arrayNum [index]; ++ index ;}cout <endl; cout <"the biggest platform is:" <endl; cout <arrayNum [indexEnd] <
  
   
Experiment results:



After reading the answer, I instantly felt that I should consider this issue more. The solution of computer scientists is indeed much less code...

# Include
    
     
Using namespace std; int _ tmain (int argc, _ TCHAR * argv []) {int index = 0; // array subscript index // int indexEnd = 0; // destination index int length = 1; // platform length int arrayNum [100] = {0}; // Zero is not an input element, therefore, cout <"enter a sequence of numbers separated by spaces. 0 indicates the end of the input:" <endl; while (cin> arrayNum [index]) & arrayNum [index]! = 0) ++ index; index = 0; for (index = 1; arrayNum [index]! = 0; index ++) {if (arrayNum [index] = arrayNum [index-length]) length ++;} cout <"input number sequence: "<endl; index = 0; while (arrayNum [index]! = 0) {cout <arrayNum [index]; ++ index;} cout <endl; cout <"consecutive times:" <endl; cout <length <endl; getchar (); return 0 ;}
    

Experiment results:



In order to better compare the gap between me and scientists, I put the programCore codeComparison

// Scientist's for (index = 1; arrayNum [index]! = 0; index ++) {if (arrayNum [index] = arrayNum [index-length]) length ++ ;} //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ////////////////////////////// my while (arrayNum [index]! = 0) {if (arrayNum [index + 1]! = 0) {if (arrayNum [index] = arrayNum [index + 1]) {tempCount ++;} else {if (tempCount> count) {count = tempCount; indexEnd = index;} tempCount = 0 ;}++ index ;}

Because the array order has been arranged, scientists use it. One Variable directly detects the farthest point on the platformThe idea is indeed subtle. Hey, learn more ~ Come on!


-End-

References: Selecting hundreds of documents for C language naming questions




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.