The most valuable objects, the solution of the task
#include <iostream>
#include<stdio.h>
/*Algoritmi osnovni nivo-> nizovi->sortiranje->sortiranje*/
using namespace std;
int main()
{
double money,rest;
int N;
cin>>money>>N;
string passw[N];
double thePrice[N];
for(int i=0; i<N; i++)
{
cin>>passw[i]>>thePrice[i];
}
int j;
/*sorting by insertion method*/
for(int i=1; i<N; i++)
{
double b=thePrice[i];
string p=passw[i];
for(j=i-1; j>=0; j--)
{
if(thePrice[j]<b)
{
thePrice[j+1]=thePrice[j];
passw[j+1]=passw[j];
}
else
{
break;
}
}
thePrice[j+1]=b; //put the number b between the larger and the smaller
passw[j+1]=p; //put the number b between the larger and the smaller
}
/* cout<<"sorted string:"<<endl;
for(int i=0;i<N;i++){
cout<<thePrice[i]<<endl;
}*/
double currentPrice;
rest=money;
currentPrice=thePrice[0];
for(int i=0; i<N; i++)
{
if(rest< thePrice[i])
{
continue;
}
tekCena=thePrice[ i ];
cout<<passw[i]<<" ";
printf("%.2f\n", currentPrice);
rest=rest-currentPrice;
}
if(rest>0)
{
printf("%.2f", rest);
}
return 0;
}
#include<stdio.h>
/*Algoritmi osnovni nivo-> nizovi->sortiranje->sortiranje*/
using namespace std;
int main()
{
double money,rest;
int N;
cin>>money>>N;
string passw[N];
double thePrice[N];
for(int i=0; i<N; i++)
{
cin>>passw[i]>>thePrice[i];
}
int j;
/*sorting by insertion method*/
for(int i=1; i<N; i++)
{
double b=thePrice[i];
string p=passw[i];
for(j=i-1; j>=0; j--)
{
if(thePrice[j]<b)
{
thePrice[j+1]=thePrice[j];
passw[j+1]=passw[j];
}
else
{
break;
}
}
thePrice[j+1]=b; //put the number b between the larger and the smaller
passw[j+1]=p; //put the number b between the larger and the smaller
}
/* cout<<"sorted string:"<<endl;
for(int i=0;i<N;i++){
cout<<thePrice[i]<<endl;
}*/
double currentPrice;
rest=money;
currentPrice=thePrice[0];
for(int i=0; i<N; i++)
{
if(rest< thePrice[i])
{
continue;
}
tekCena=thePrice[ i ];
cout<<passw[i]<<" ";
printf("%.2f\n", currentPrice);
rest=rest-currentPrice;
}
if(rest>0)
{
printf("%.2f", rest);
}
return 0;
}
© 2019 by Slobodan izprogramiranja.weebly.com
All rights reserved