Showing posts with label C++. Show all posts
Showing posts with label C++. Show all posts

Monday, February 20, 2012

[Codechef] ATM (HS08TEST)

The problem is totally straightforward. Only possible cause for wrong answer response is if x = y. In that case you should check if x+0.5 > y or not.

#include <stdio.h>

int main()
{
    int x;
    double y;
    while (scanf("%d %lf",&x,&y)==2)
    {
        if (!(((double)x+0.5)>y) && !(x%5))
            printf("%.2lf\n",y-(double)x-0.5);
        else
            printf("%.2lf\n",y);
    }
    return 0;
}

Saturday, November 26, 2011

[UVa] 11945 - Financial Management

Too simple. It doesn't even need a search :P. But while solving this I learned of using C Locales. Look at how I printed the value. Didn't even had to put an if for the commas.
/* Faith-M */

//Headers
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <climits>
#include <clocale>
//Defines
#define pow2(i) (1<<i)
#define bit(i) (1<<i)
#define isOdd(i) (i&1)
#define isEven(i) (!(i&1))
#define isPrime(i) ((i==2) || ((i&1) && !pTest[i])) //pTest has to be the bool array's name
#define sz(i) i.size()
#define vec(type,name) vector< type > name
#define rep(i,a,b) for(int i=a ; i<=b ; i++)
#define swap(type,a,b) {type t=a; a=b; b=t;}
#define sum(a,n) ( (n*(n+1)/2) - (a-1)*a/2 )
#define iscap(i) (i>='A'&&i<='Z')
#define issmall(i) (i>='a'&&i<='z')
#define isnum(i) (i>='0'&&i<='9')
#define issymbol(i) (!(i>='a'&&i<='z') && !(i>='A'&&i<='Z') && !(i>='0'&&i<='9'))
#define mk(i,j) make_pair(i,j)
#define ERROR 1e-11
//Type Defs
typedef long long lint;
typedef unsigned long long ulint;
typedef long double ldouble;

using namespace std;

int main()
{
 /*freopen("input.txt","r+",stdin);
 freopen("output.txt","w+",stdout);/**/

 //    TEST CASE     //
 int kase=1, kounter=1;/**/
 int i;
 double sum, blnc;

 scanf("%d",&kase);
    setlocale(LC_ALL, "en_US.UTF-8");

 while (kase--)
 {
     for (i=1, sum=0 ; i<=12 ; i++)
     {
         scanf("%lf",&blnc);
         sum += blnc;
     }
        printf("%d $%'.2lf\n", kounter++, sum / 12.0);

 }
 return 0;
}

Saturday, October 22, 2011

[UVa] 11850 - Alaska

With this, I finish 300 solves in UVa. :) Took too long.
My stats at the moment:
Submissions: 1701
Problems tried: 322
Problems solved: 300
First submission: 2009-11-17
Last submission: 2011-10-21
And details on the last submission:
Problem number: 11850
Rank of solution: 225
Submission number: 9395891
Date: 2011-10-21
Time: 20:54:39
Runtime: 0.008

Method:
1. Take the input and sort the array.
2. Check if between two consecutive stations the distance is > 200. If so IMPOSSIBLE
3. At last she has to go to the destination and come back to the last station, so check if (1422-[last station])X2 > 200.

#include <stdio.h>
#include <stdlib.h>

int cmp(const void *a, const void *b)
{
    return (*(int*)a - *(int*)b);
}
int miles[4000];
int main()
{
    int n, i, set;
    while (scanf("%d",&n) && n)
    {
        for (i=0 ; i<n ; i++)
            scanf("%d",&miles[i]);
        qsort(miles,n,sizeof(int),cmp);
        set=1;

        for (i=1 ; i<n && set ; i++)
        {
            if (miles[i]-miles[i-1]>200) set=0;
        }
        if (2*(1422-miles[n-1])>200) set=0;
        if (set) printf("POSSIBLE\n");
        else printf("IMPOSSIBLE\n");
    }
    return 0;
}

Sunday, October 16, 2011

[UVa] 10862 - Connect the Cable Wires

I found a nice way to treat C Structures as same (almost) as C++ Classes, having functions for their operations. While solving this problem I implemented that.

Method:
Analyze the problem with cases 1, 2, 3, 4 and if needed, 5. You'll soon find out that it's about even positioned Fibonacci numbers.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char res[1000000];
typedef struct bNum
{
    int size;
    char val[2000];
} BigNum;

void setNum(char *val, BigNum *pThis)
{
    strcpy(pThis->val,val);
    pThis->size=strlen(val);
}
char *getNum(BigNum *pThis)
{
    return pThis->val;
}
int addNum(BigNum *p1, BigNum *p2, BigNum *p3)
{
    int i, j, k, car=0, cur;

    for (i=p1->size-1, j=p2->size-1, k=0 ; i>=0 || j>=0 ; i--, j--, k++)
    {
        cur = (i>=0?p1->val[i]:'0') + (j>=0?p2->val[j]:'0') + car - 96;
        if (cur>9)
        {
            car=1;
            res[k]=(cur-10)+'0';
        } else
        {
            car=0;
            res[k]=cur+'0';
        }
    }
    if (car)
    {
        res[k++]='1';
    }
    p3->size=k;
    for (--k, i=0 ; k>=0 ; k--, i++)
    {
        p3->val[i]=res[k];
    }
    p3->val[i]='\0';
}

BigNum fibs[5000];

int main()
{
    int i, n;
    setNum("1",&fibs[1]);
    setNum("1",&fibs[2]);

    for (i=3 ; i<=4001 ; i++)
    {
        addNum(&fibs[i-1],&fibs[i-2],&fibs[i]);
    }

    while (scanf("%d",&n) && n)
    {
        printf("%s\n",getNum(&fibs[2*n]));
    }
    return 0;
}

Friday, October 14, 2011

Some tricks for C/C++

Over the years I've been into coding, I've found many techniques coders use to squeeze out more time out of a program or decrease their coding time. Some of them have been given here, for reference.

Use #define as function:
#define isCaps(i) (i>='A" && i<='Z')
int main()
{
    if (isCaps('S')) printf("S is in Caps\n");
    return 0;
}
Shorten your simple for loops:
#define FOR(i, a, b) for(i=a ; i<b ; i++)
int main()
{
    int i;
    FOR(i, 1, 985)
    {
        printf("%d\n",i);
    }
    return 0;
}
Test whether a number is even or odd:
#define isEven(j) (!(j & 0x01))
Divide a number by 2:
n = n >> 1;
You can find some nice bit tricks in this link. [ Bit twiddling hacks ]
I'll add more when I get these more organized :p.

Connect Rapoo MT750S with Linux (Tested on Manjaro)

 I bought this obvious copy of MX Master 2S in hopes of having the device switching functionality along with a lightweight body because I ha...