Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct laptop
- {
- int l;
- int c;
- float uc;
- };
- int main()
- {
- int i=0,j=0,k=0,m=0;
- struct laptop arr[5],temp;
- for(; i<5,j<5; i++,j++)
- {
- cout<<"L : ";
- cin>>arr[j].l;
- cout<<"C : ";
- cin>>arr[i].c;
- }
- i=0;
- j=0;
- for(; k<5; k++)
- {
- arr[k].uc= (float)arr[i].c/arr[j].l;
- i++;
- j++;
- }
- cout<<endl<<" L"<<" "<<" C"<<" "<<" UC"<<endl;
- for(; m<5; m++)
- {
- cout<<" "<<arr[m].l<<" "<<arr[m].c<<" "<<arr[m].uc<<endl;
- }
- cout<<endl;
- /////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////
- int input=m,i1=0,j1;
- for(;i1<input;i1++)
- {
- for(j1=i1+1;j1<input;j1++)
- {
- if(arr[i1].uc>arr[j1].uc)
- {
- temp=arr[i1];
- arr[i1]=arr[j1];
- arr[j1]=temp;
- }
- }
- }
- cout<<endl<<endl<<"After Shorting : ";
- cout<<endl<<" L"<<" "<<" C"<<" "<<" UC"<<endl;
- int g=0;
- for(; g<5; g++)
- {
- cout<<" "<<arr[g].l<<" "<<arr[g].c<<" "<<arr[g].uc<<endl;
- }
- cout<<endl;
- int capa,u=0,store;
- int item=g;
- float ans=0;
- cout<<"How many laptop you want to buy : ";
- cin>>capa;
- store=capa;
- while (capa>0 && u<item)
- {
- if(capa>=arr[u].l)
- {
- ans+=(float)arr[u].c;
- capa-=arr[u].l;
- }
- else
- {
- ans+=capa*(float)arr[u].uc;
- capa=0;
- }
- u++;
- }
- if(capa==0)
- {
- cout<<endl<<"Minimum cost to buy"<<" "<<store<<" laptop : "<<ans<<endl;
- }
- else
- {
- cout<<"Insufficient"<<endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment