Software engineering with small jobs-(c + +)

Source: Internet
Author: User

First, design ideas

My idea was to calculate the average based on the number of people upstairs and the number of floors upstairs, but I found the idea wrong. So I chose the most stupid method, the complexity of O (N2).

(1) Enter the number of people who take the elevator and the floor to go;

(2) Find the highest input floor, so as to reduce the number of traversal;

(3) Start traversing from the lowest floor to the highest level. The inner Loop calculates the sum of the layers of each person's climbing floor and compares them to find the smallest.

(4) Output results

Second, the source code

1 //Elevator. Cpp:defines The entry point for the console application.2 //3 4#include"stdafx.h"5#include"iostream.h"6#include"stdlib.h"7 #defineMAXSIZE 158 9 voidInput (int&num,intFlour[])//Enter the number and floor of the elevatorTen { Onecout<<"Please enter the number of people who take the elevator (limit 15 persons):"; ACin>>num; -     if(num> the) -     { thecout<<"input Error! "<<Endl; -Exit0); -     } -cout<<"Please enter the number of layers (up to 18 layers) Each person is going to:"; +      for(intI=0; i<num;i++) -     { +Cin>>Flour[i]; A         if(flour[i]> -) at         { -cout<<"input Error! "<<Endl; -Exit0); -         } -     } - } in voidGetsmallestflour (intNumintFlour[],int&small)//calculate the floor with the lowest number of stairs to climb - { to     intsum,temp=Small; +     inthighest,lowest; -highest=lowest=flour[0]; the      for(intI=0; i<num;i++)//find the highest floor in the floor you're going to. *     { $         if(flour[i]>highest)Panax Notoginseng         { -highest=Flour[i]; the         } +         Else                            //find the lowest floor A         { thelowest=Flour[i]; +         } -     } $      for(i=lowest;i<=highest;i++)//go from the lowest floor to the highest floor $     { -sum=0; -          for(intj=0; j<num;j++) the         { -             if((Flour[j]-i) <0)//Calculate the sum of the number of floors of stairsWuyi             { thesum=sum+i-Flour[j]; -             } Wu             Else -             { Aboutsum=sum+flour[j]-i; $             } -         } -cout<<"If you stop at"<<i<<"layer, the sum of the stair climbs is"<<sum<<Endl; -         if(sum<temp) A         { +Small=i;//record and the smallest floor number theTemp=sum;//record the smallest and -         } $     } the } the  the intMainintargcChar*argv[]) the { -     intNum,aver,flour[maxsize]; in Input (num,flour); theAver=0; the      for(intI=0; i<num;i++) About     { theaver+=Flour[i]; the     } the Getsmallestflour (num,flour,aver); +cout<<"The elevator will stop at"<<aver<<"layer. "<<Endl; -     return 0; the}

Iii. Results

Iv. Experience

In class, the teacher, told the optimization of the algorithm, and finally know what the advantages of good mathematics. Originally thought that the programming and the mathematics related, but the relation is not big, now feels the teacher to say is good, the arithmetic is mathematics. Later encountered this algorithm optimization or the algorithm requirements are relatively high, can be studied in depth.

Software engineering with small jobs-(c + +)

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.