SRM 144 DIV2
1.*200 --- 60 read
#include<string>
#include<iostream>
#include<sstream>
using namespace std;
class Time {
public:
string whatTime(int seconds);
private:
int hr,min,sec;
};
string Time::whatTime (int seconds) {
hr=seconds/3600;
min=(seconds-hr*3600)/60;
sec=seconds-hr*3600-min*60;
stringstream ss;
ss << hr << ":" << min << ":" << sec;
return ss.str();
}
How to convert int into string. Important!2.*550 --- 168 read
#include<string>
#include<vector>
#include<iostream>
using namespace std;
class BinaryCode {
public:
vector<string> decode(string message) {
int n=message.size();
string sa="0", sb="1";
for(int i=0; i<n-1; ++i) {
if(i==0) sa += message[0];
else sa += message[i] - sa[i] - (sa[i-1]-'0')+'0';
if(sa[i+1]<'0'||sa[i+1]>'1'){
sa="NONE";
break;
}
}
for(int i=0; i<n-1; ++i) {
if(i==0) sb += message[0]-1;
else sb += message[i] - sb[i] - (sb[i-1]-'0')+'0';
if(sb[i+1]<'0'||sb[i+1]>'1'){
sb="NONE";
break;
}
}
if(message[n-1]+'0'-sa[n-1]-sa[n-2]!=0) sa="NONE";
if(message[n-1]+'0'-sb[n-1]-sb[n-2]!=0) sb="NONE";
vector<string> result;
result.push_back(sa);
result.push_back(sb);
return result;
}
};
The return value of [] is a char, remember how to calc char with int.SRM 145 DIV2
1.*250 --- 75
SRM 146 DIV2
1.*250 --- 118
2.*500 --- 461
SRM 147 DIV2
1.*250 --- 241
SRM 147 DIV1
1.*500 --- 195
read
#include <iostream>
#include <vector>
#include <sstream>
#include <string>
using namespace std;
class Dragons
{
public:
string snaug(vector <int> initialFood, int rounds)
{
long long denominator = 4;
long long Fn[6];
long long Fo[6]={initialFood[0],initialFood[1],initialFood[2],initialFood[3],initialFood[4],initialFood[5]};
Fn[0]=Fn[1]=Fo[2]+Fo[3]+Fo[4]+Fo[5];
Fn[2]=Fn[3]=Fo[0]+Fo[1]+Fo[4]+Fo[5];
Fn[4]=Fn[5]=Fo[0]+Fo[1]+Fo[2]+Fo[3];
--rounds;
for(int i=1;i<=rounds;++i)
{
denominator *=2;
for(int j=0;j<5;j+=2)
Fo[j]=Fn[j];
Fn[0]=Fo[2]+Fo[4];
Fn[2]=Fo[0]+Fo[4];
Fn[4]=Fo[0]+Fo[2];
//Try to divide by 2 to save time
if(Fn[0]%2==0 && Fn[2]%2==0 && Fn[4]%2==0)
{
Fn[0]/=2;
Fn[2]/=2;
Fn[4]/=2;
denominator/=2;
}
}
//calc final result
long long nominator=Fn[2];
long long a,b,tmp;
a=nominator;
b=denominator;
while(a%b!=0)
{
tmp=a%b;
a=b;
b=tmp;
}
nominator /= b;
denominator /= b;
// convert from int to string
stringstream ss;
if(nominator%denominator==0)
ss<<(nominator/denominator);
else
ss << nominator << "/" << denominator;
return ss.str();
}
};
1. should use "long long" when the value may exceed the boundary
2. should try to divide by 2 to save time!
3. convert from int to string
SRM 148 DIV2
1.*250 --- 81
SRM 149 DIV2
1.*500 --- 451
2.*1000 --- 405 read
#include <iostream>
#include <vector>
using namespace std;
class Pricing
{
public:
int maxSales(vector <int> price)
{
int Max = 0;
// corner case
if (price.size()<=4)
{
for(vector<int>::iterator iter = price.begin(); iter!=price.end();++iter)
Max += *iter;
return Max;
}
// valid case
vector<int> sprice;
int TotalNum = price.size();
for(int i=0; i<TotalNum;++i)
{
//Min should be 1000, not 0!!!
int Min=1000;
vector<int>::iterator Miter;
for(vector<int>::iterator iter = price.begin(); iter!=price.end();++iter)
{
if(Min>*iter)
{
Min = *iter;
Miter = iter;
}
}
sprice.push_back(Min);
price.erase(Miter);
}
int tmp;
for(int i=0;i<=TotalNum;++i)
for(int j=i;j<TotalNum; ++j)
for(int k=j;k<TotalNum; ++k)
for(int l=k;l<TotalNum; ++l)
{
tmp = sprice[i]*(j-i) + sprice[j]*(k-j)+sprice[k]*(l-k)+sprice[l]*(TotalNum-l);
if(tmp > Max)
Max = tmp;
}
return Max;
}
};
When try to find the Max, we should set the initial value of Max to minimum value;
When try to find the Min, we should set the initial value of Min to maximum value;
SRM 150 DIV2
1.*250 --- 219
2.*500 --- 435
SRM 151 DIV2
1.*250 --- 187
2.*500 --- 384
SRM 152 DIV2
1.*250 --- 234
2.*500 --- 409
3.*1000 --- 490
SRM 153 DIV2
1.*250 --- 228
2.*500 --- 370
#include <iostream>
#include <vector>
using namespace std;
class Inventory
{
public:
int monthlyOrder(vector <int> sales, vector <int> daysAvailable)
{
double total=0;
int size = sales.size();
int emp = 0;
for(int i=0;i<size;++i)
{
if(daysAvailable[i]!=0)
total+= sales[i]*30/double(daysAvailable[i]); // must transfer to double to get double result
else
++emp;
}
size -= emp;
if(size !=0 )
{
total = total/size;
int round = total;
if (total != round)
++round;
return round;
}
else
return 0;
}
};
when trying to increase the accuracy, we must ensure not only the L-value is double, the right side expression should also be double.(when there is int denominator, we should change it into double type to ensure the
accuracy)3.*1000 --- 490
SRM 154 DIV2
1.*300 --- 171.58
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
class
ProfitCalculator
{
public:
int percent(vector <string> items)
{
stringstream s;
double cost, price, Tcost, Tprice;
Tcost = 0;
Tprice = 0;
for(vector<string>::iterator iter = items.begin(); iter != items.end(); ++iter)
{
s << *iter;
s >> price >> cost;
Tcost += cost;
Tprice += price;
s.str(""); //clear buffer
s.clear(); //reset error state
}
double Result =( Tprice - Tcost )/Tprice;
return int(Result*100);
}
};
How to transfer from string to int/double.
When we want to reuse a stringstream object, we need to:
1. clear buffer
2. reset the error state