穷举法应用—售货员

 问题描述:叶卡特琳堡有很多公共汽车,因此也有很多市民当上售票员,如果在所有的市民当中,售票员的人数超过p%而不到Q%,那么叶卡特琳堡有多少公民? 例如,如果p=13,Q=14.1,那么至少有15个公民.

用穷举解决问题

please input the downline P%
15
please input the upline Q%
16.3
the Answer is-->13
Press any key to continue

源程序:

#include <iostream>
using namespace std;

void main()
{
 float p,q;    //所输入的比例
 cout<<"please input the downline P%"<<"/n";
 cin>>p;
 cout<<"please input the upline Q%"<<"/n";
 cin>>q;

 float x(1);
 bool test(true);

 while(test)
 {
  for(float i = 1;i<=x;i++)
  {
   if((i/x)<(p/100))
    continue;
   else if((i/x)>(q/100))
    continue;
   else
   {
    test = false;
    cout<<"the Answer is-->"<<x<<"/n";
   }
  }
  x++;
 }
}

 

你可能感兴趣的:(input,float,iostream)