Problem C: full yet

Title Description LZY after three years of university study hard, on the eve of junior school, and finally the entry takeaway industry giants - full yet? As yet full of budding JAVA engineers, project managers assigned a difficult task for him, hoping he completed a restaurant filter. Now give a list of restaurant information LZY, information about each restaurant, including id, rating, veganFriendly, price , distance, representing the restaurant id, restaurant ratings, whether vegetarian-friendly restaurant, average price, and distance.

Filter rules are as follows:
where the value of vegetarian-friendly filter veganFriendly can be true or false, if true it means that you should leave only include veganFriendly is true of restaurants, false means may include any restaurant. In addition, we have a maximum price and the maximum distance maxDistance maxPrice two filters, which are considered the maximum price factor and distance factor of the restaurant.

After filtration restaurant information output meets the requirements according to rating descending order. If the same rating, then sorted by descending id. For simplicity, veganFriendly the value of 1 is true, is false, the value is 0.
Input test data from the plurality of sets of test samples composed.
Each test input line of four integers, n number of restaurants (1 <= n <= 10000 ), vegetarian-friendly mark a (a == 0 or a == 1), the maximum price b (0 <= b <= 10000), the maximum distance c (0 <= c <= 10000)
next n lines are inputted an integer of 5 restaurants id q (1 <= q < = 10000), restaurants score w (1 <= w <= 10000), whether vegetarian restaurants Friendly (e == 0 or e == 1 ), the average price restaurant r (1 <= r <= 10000) and from restaurants (1 <= g <= 10000 )

Output through the filtered list of restaurants, each line of output a restaurant information. 0 50 10 Sample input COPY5
. 1. 4. 1 40 10
2 0. 8 50. 5
. 3 30. 4. 8. 1
. 4 10. 3 10 0
. 5. 1. 1. 1 15
sample output. 3 10 0 10 COPY4
. 3 30. 4. 8. 1
2 0. 8 50. 5
10. 4 40. 1. 1
. 5 15. 1. 1. 1
Tip because the test sample data is too large, and printf scanf recommended for input and output
structures sorting
1. error codes

#include<bits/stdc++.h>
using namespace std;
struct node{
 int q,w,e,r,g;
}f[11000];
bool cmp(node x,node y){
 if(x.w !=y.w ) return x.w >y.w ;
 return x.q >y.q ;
}
int main(){
 int n,a,b,c;
 int sum;
 while(cin>>n>>a>>b>>c){
  for(int i=0;i<n;i++){
   cin>>f[i].q >>f[i].w >>f[i].e >>f[i].r >>f[i].g ;
   if(f[i].e ==1){
    sum++; 
   }
  }
  sort(f,f+n,cmp);
  for(int i=0;i<n;i++){
   if(a==0){
    if(f[i].r <=b&&f[i].g <=c){
     cout<<f[i].q <<f[i].w <<f[i].e <<f[i].r <<f[i].g <<endl;
    }
   }
   else if(a==1){
    if(f[i].e ==1&&f[i].r <=b&&f[i].g <=c){
     cout<<f[i].q <<f[i].w <<f[i].e <<f[i].r <<f[i].g <<endl; 
    }
   }
  }
 }
 return 0;
}

Output format error
2. correct codes

#include<bits/stdc++.h>
using namespace std;
struct node{    
int q,w,e,r,g;
}f[11000];
bool cmp(node x,node y){    
if(x.w !=y.w ) return x.w >y.w ;    
return x.q >y.q ;}
int main(){    
int n,a,b,c;    
int sum=0;    
while(cin>>n>>a>>b>>c){        
for(int i=0;i<n;i++){            
cin>>f[i].q >>f[i].w >>f[i].e >>f[i].r >>f[i].g ;            
if(f[i].e ==1){                
sum++;              
}        
}        
sort(f,f+n,cmp);        
for(int i=0;i<n;i++){            
if(a==0){                
if(f[i].r <=b&&f[i].g <=c){                    
cout<<f[i].q <<" "<<f[i].w<<" "<<f[i].e<<" "<<f[i].r <<" "<<f[i].g <<endl;                }            
}            
else if(a==1){                
if(f[i].e ==1&&f[i].r <=b&&f[i].g <=c){                    
cout<<f[i].q <<" "<<f[i].w<<" "<<f[i].e<<" "<<f[i].r <<" "<<f[i].g <<endl;                  }            
}        
}    
}    
return 0;
}
Published 12 original articles · won praise 0 · Views 122

Guess you like

Origin blog.csdn.net/weixin_45987032/article/details/104524737