Pages

Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Saturday, June 2, 2012

11340 - Newspaper


//
// main.cpp
// 11340 - Newspaper
//
// Created by Panks on 31/05/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include <iostream>
#include <map>
#include <cstdio>
using namespace std;
int main (int argc, const char * argv[])
{

int t,in;
double answer;
char letter;
int vals;
string line;
map<char, int> values;

cin>>t;
while (t--) {

answer=0;
cin>>in;
while (in--) {
cin>>letter>>vals;
values.insert(pair<char, int>(letter, vals));
}
cin>>in;
getchar();
while (in--) {
getline(cin, line);
for (int i=0; i<line.size() ; i++) {
if (values[line[i]]) {
answer+=values[line[i]];
}
}
}
answer/=100;
printf("%.2f$\n", answer);

values.clear();

}
return 0;
}


11150 - Cola

Just scribe a little on paper, you would get it. ;)

//
// main.cpp
// 11150 - Cola
//
// Created by Panks on 29/05/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include <iostream>
using namespace std;
int main (int argc, const char * argv[])
{

int a, tmp;
while (cin>>a) {
if (a==1||a==0) {
cout<<a<<endl;
continue;
}
tmp=a;
a=a+a/2;
a=a-a%3;
cout<<tmp+(a/3)<<endl;
}
return 0;
}

11044 - Searching for Nessy


//
// main.cpp
// 11044 - Searching for Nessy
//
// Created by Panks on 29/05/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include <iostream>
using namespace std;
int main (int argc, const char * argv[])
{

int t;
double a,b, froma, fromb;
int ifroma, ifromb;
cin>>t;
while (t--) {
cin>>a>>b;
a-=2;
b-=2;
froma=a/3.0;
fromb=b/3.0;

if (froma-(int)froma!=0) {
ifroma=(int)froma+1;
}else{
ifroma=(int)froma;
}


if (fromb-(int)fromb!=0) {
ifromb=(int)fromb+1;
}else{
ifromb=(int)fromb;
}

cout<<ifroma*ifromb<<endl;
}
return 0;
}

10683 - The decadary watch


//
// main.cpp
// 10683 - The decadary watch
// Created by Panks on 28/05/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include <iostream>
#include <sstream>
using namespace std;
long double mult = 10000000.0/8640000.0;
long long int secs1;
int ans;
string convertInt(int number);
string convertInt(int number)
{
stringstream ss; //create a stringstream
ss << number; //add number to the stream
return ss.str(); //return a string with the contents of the stream
}

int main (int argc, const char * argv[])
{

string input;
int h1, m1, s1, c1, h2, m2, s2, c2;
while (getline(cin, input)) {
h1=(input[0]-'0')*10+(input[1]-'0');
m1=(input[2]-'0')*10+(input[3]-'0');
s1=(input[4]-'0')*10+(input[5]-'0');
c1=(input[6]-'0')*10+(input[7]-'0');
secs1=h1*360000+m1*6000+s1*100+c1;

ans=secs1*mult;
string tocheck=convertInt(ans);
int len=7-(tocheck.size());
while (len--) {
cout<<'0';
}

cout<<ans<<endl;
}
return 0;
}

10281 - Average Speed


//
// main.cpp
// 10281 - Average Speed
//
// Created by Panks on 27/05/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
double cspeed=0, tdistane=0;
int gh=0, gm=0, gs=0;
string input;


void updatevalues();
void updatevalues(){
int h=0,m=0,s=0, speed=0;
int i=0;
while (input[i]!=':') {
h=h*10+input[i]-'0';
i++;
}
i++;
while (input[i]!=':') {
m=m*10+input[i]-'0';
i++;
}
i++;
while (input[i]!=' ') {
s=s*10+input[i]-'0';
i++;
}
i++;
while (input[i]!='\0') {
speed=speed*10+input[i]-'0';
i++;
}
double time=(h-gh)+((m-gm)/60.0)+((s-gs)/3600.0);
double dis=time*cspeed;
tdistane=dis+tdistane;
cspeed=speed;
gh=h;
gm=m;
gs=s;
}



int main (int argc, const char * argv[])
{

// insert code here...
int h=0, m=0, s=0;
while (getline(cin, input)) {
if (count(input.begin(), input.end(), ' ')) {
updatevalues();
}else{
int i=0;
h=0; m=0; s=0;
while (input[i]!=':') {
h=h*10+input[i]-'0';
i++;
}
i++;
while (input[i]!=':') {
m=m*10+input[i]-'0';
i++;
}
i++;
while (input[i]!='\0') {
s=s*10+input[i]-'0';
i++;
}

double dist=((h-gh)+((m-gm)/60.0)+((s-gs)/3600.0))*cspeed;
double totaldis=tdistane+dist;
printf("%s %.2f km\n", &input[0], totaldis);


}
}

}


10082 - WERTYU

You can combine all the arrays in one and can do a lot better :) , I was just rushing through when I coded these.


//
// main.cpp
// WERTY
//
// Created by Panks on 27/05/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;



#define ull unsigned long long
#define ll long long

#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define REP(i,n) FOR(i,0,n-1)

#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define REPD(i,n) FOR(i,n-1,0)

#define testcase(t) int t;scanf("%d",&t);while(t--)
#define s(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define ss(x) scanf("%s",x)

#define p(x) printf("%d",x)
#define pl(x) printf("%lld",x)
#define ps(x) printf("%s",x)

#define pn(x) printf("%d\n",x)
#define pln(x) printf("%lld\n",x)
#define psn(x) printf("%s\n",x)

#define all(c) c.begin(),c.end()




string row1="`1234567890-=", row2="QWERTYUIOP[]\?", row3="ASDFGHJKL;'", row4="ZXCVBNM,./<";

void replaceit(char a);
void replaceit(char a){
if (a==' ') {
cout<<" ";
}
string::iterator pos;

if(count(row1.begin(), row1.end(), a)){
pos=find(all(row1), a);
pos--;
cout<<*pos;
}else if(count(row2.begin(), row2.end(), a)){
pos=find(all(row2), a);
pos--;

cout<<*pos;
}else if(count(row3.begin(), row3.end(), a)){
pos=find(all(row3), a);
pos--;

cout<<*pos;
}else if(count(row4.begin(), row4.end(), a)){
pos=find(all(row4), a);
pos--;
cout<<*pos;
}
}

int main (int argc, const char * argv[])
{
string input;
while(getline(cin, input)){
for_each(input.begin(), input.end(), replaceit);
cout<<endl;
}

}


941 - Permutations



/* ******************************************* */
// \_/\_/\_/\_/---Coded by Panks---\_/\_/\_/\_/ //
/* ******************************************* */


// **********************Headers******************

//C headers
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cstdio>
#include<ctime>
#include<cctype>
#include<cassert>
#include<climits>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<clocale>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cwchar>
#include<cwctype>

//containers
#include<vector>
#include<list>
#include<map>
#include<queue>
#include<deque>
#include<set>
#include<complex>
#include<string>
#include<stack>
#include<bitset>
#include<istream>
#include<valarray>

//IOs
#include<iostream>
#include<sstream>
#include<iomanip>
#include<fstream>
#include<exception>
#include<ios>
#include<iosfwd>
#include<ostream>
#include<iterator>
#include<stdexcept>
#include<streambuf>

//algorithm & miscellaneous
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<limits>
#include<locale>
#include<memory>
#include<new>
/* ********************************************** */


////////////////////SHORTHANDS\\\\\\\\\\\\\\\\\\\\\

#define ull unsigned long long
#define ll long long

#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define REP(i,n) FOR(i,0,n-1)

#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define REPD(i,n) FOR(i,n-1,0)

#define testcase(t) int t;scanf("%d",&t);while(t--)
#define s(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define ss(x) scanf("%s",x)

#define p(x) printf("%d",x)
#define pl(x) printf("%lld",x)
#define ps(x) printf("%s",x)

#define pn(x) printf("%d\n",x)
#define pln(x) printf("%lld\n",x)
#define psn(x) printf("%s\n",x)

#define all(c) c.begin(),c.end()
//////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\


// ---------------Fast IO using getchar-------------- \\
// will accept values 9-digits in int and 19-digits in long long on my system

int getint();
int getint(){
int c = 'a';
while (!(c >= '0' && c <= '9') && c != '-')
c = getchar();
int c2;
if (c != '-')
c2 = c;
else
c2 = '0';
int res = 0;
while((c2 >= '0' && c2 <= '9')){
res = (res << 3) + (res << 1) + (c2 - '0');

c2 = getchar();
}
return res * (c == '-' ? -1 : 1);
}
// ------------------------------------------------- \\

//------------------Actual Code--------------------- \\


using namespace std;

bool compit (char a, char b);
bool compit (char a, char b){
return int(a)<int(b);
}

long int factorial(long int n);
long int factorial(long int n){
int a=1;
while (n!=1) {
a*=n;
n--;
}
return a;
}

int main(){

int t;
string input="";
long int no, fact;
cin>>t;
while (t--) {
cin>>input>>no;
sort(all(input), compit);
int a=0;
fact=1;
int long factor=factorial(input.size());
no=no%factor;

if (no!=0) {
while (fact<=no) {
a++;
fact=fact*a;
}
fact/=a;
a--;

}else{
fact=0;
a=0;

}

string ans="";
while (a--) {
ans+=char(int(no/fact)+48);
no=no%fact;
fact/=(a+1);
}
ans+='0';

int long diff=input.size()-ans.size();
if (diff<0) {
diff=0;
}
while (diff--) {
ans='0'+ans;
}


long int len=input.size();
int i=0;
len--;
string final="";
while (len--) {
final+=input[ans[i]-48];
input.erase(input.begin()+ans[i]-48);
i++;
}
cout<<final+input<<endl;

}

}

739 - Soundex Indexing


//
// main.cpp
// Soundex Indexing
//
// Created by Panks on 24/05/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include <iostream>
using namespace std;



int getcode(char a);

int main (int argc, const char * argv[])
{

string input;
bool done=false;
int i;
string code, ans;
cout<<" NAME SOUNDEX CODE"<<endl;
while (cin>>input) {
done=false;
i=0;
code="";
while (i<input.length()&&!done) {
if(i>0&&(input[i]=='A'||input[i]=='E'||input[i]=='I'||input[i]=='O'||input[i]=='U'||input[i]=='Y'||input[i]=='W'||input[i]=='H'||getcode(input[i])==getcode(input[i-1]))){
i++;
continue;
}
if (code.length()==4) {
done=true;
continue;
}else if(code.length()>0&&code.length()<4){
code=code+char(getcode(input[i])+48);
i++;
}else{
code=input[i];
i++;
}
}
if (code.length()!=4) {
while (code.length()!=4) {
code=code+'0';
}
}
ans="";
ans=" "+input;
while (ans.length()!=34) {
ans=ans+' ';
}
ans=ans+code;
cout<<ans<<endl;
}
cout<<" END OF OUTPUT"<<endl;
return 0;
}

int getcode(char a){
if(a=='B'||a=='P'||a=='F'||a=='V'){
return 1;
}else if(a=='D'||a=='T'){
return 3;
}else if(a=='L'){
return 4;
}else if(a=='M'||a=='N'){
return 5;
}else if(a=='R'){
return 6;
}else if(a=='C'||a=='S'||a=='K'||a=='G'||a=='J'||a=='Q'||a=='X'||a=='Z'){
return 2;
}else{
return -1;
}
}

661 - Blowing Fuses



//
// main.cpp
// Blowing Fuses
//
// Created by Panks on 23/05/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include <iostream>
#include <vector>
using namespace std;
int main (int argc, const char * argv[])
{


int n,m,c;
int getit;
int count=0;

int* devpowr=new int[20];
bool* devstat=new bool[20];

cin>>n>>m>>c;
while (n!=0) {
count++;
for (int i=0; i<n; i++) {
cin>>getit;
devpowr[i]=getit;
devstat[i]=false;
}
int total=0,mtotal=0;
int tmp;
for (int i=1; i<=m; i++) {
cin>>tmp;
tmp--;
if(devstat[tmp]==false){
devstat[tmp]=true;
total+=devpowr[tmp];
mtotal=max(mtotal, total);
if(total>c){
cout<<"Sequence "<<count<<"\nFuse was blown.\n\n";
for(int r = i + 1; r <= m; r++) {
cin >> tmp;}

break;
}
}else{
devstat[tmp]=false;
total-=devpowr[tmp];
}

if(i==m){
cout<<"Sequence "<<count<<"\nFuse was not blown.\nMaximal power consumption was "<<mtotal<<" amperes.\n\n";
break;
}
}
cin>>n>>m>>c;
}
return 0;
}


573 - The Snail



//
// main.cpp
// The Snail
//
// Created by Panks on 23/05/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include <iostream>
#include <cmath>
using namespace std;
int main (int argc, const char * argv[])
{


double h, u, d;
int f;
cin>>h>>u>>d>>f;

while (h!=0) {
double tmp=0;
int count=0;
double df=u*f/100.0;
while (1) {
tmp=tmp+u;
count++;
if(tmp>h){
cout<<"success on day "<<count<<endl;
break;
}
tmp-=d;
if(tmp<0&&count>0){
cout<<"failure on day "<<count<<endl;
break;
}
u=u-df;
if (u<0) {
u=0;
}
}

cin>>h>>u>>d>>f;
}

return 0;
}


100 - The 3n + 1 problem



//
// main.cpp
// UVa
//
// Created by Panks on 06/05/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include <iostream>
#include <cstdio>
using namespace std;

int getDist(int num);

int main (void)
{
int a,b,maxi=0,c,d;
while(cin>>a>>b){
maxi=0;
c=max(a,b);
d=c==a?b:a;
for (int i=d; i<=c; i++) {
maxi=max(maxi, getDist(i));
}
printf("%d %d %d\n", a,b,maxi);
}
return 0;
}

int getDist(int num){
int dist=1;
if(num==1)
return 1;
while(1){
if(num%2==0){
num=num/2;
dist++;
if(num==1)
break;
}
else{
num=num*3+1;
dist++;
}
}
return dist;
}