Showing posts with label Recursion. Show all posts
Showing posts with label Recursion. Show all posts

Friday, May 15, 2015

Show all combinations of numbers from 1 to 9 that add up to 100 by adding, subtracting and concatenating

So, this week I saw a blog post somewhere where the poster claimed that if you can't solve the mentioned 5 problems each in under and hour, you ain't no programmer, dev or software engineer. So, I went in and read the problems. 1-4 were easy but number 5 got me a worrying a little. And expected, I failed to solve that in under 1 hour yesterday. But today morning I solved that in less than 30 minutes. But whatever, I can't call myself a programmer anymore :(
Here's my approach though, using recursion:
Each i (from 1 through 9) has 2 different ways of connecting to the sequence.
1. Add itself to the closest sum (1 + 23 + 4 + .......)
2. Or concatenate itself (1 + 234 + ......)
So if we either add an i to the sequence that has reached it or we concatenate. Examples: .......8 + 9
.......89
Now the part before 8 has the same behavior with 8
.......7 + 8
.......78
This goes all the way back to 1 and 2
1 + 2
12
So, the program works something like this
Each branch ultimately ends on the leftmost call.
Code:
#include <cstdio>
#include <iostream>
#include <sstream>

using namespace std;

string i2s(int i) {
    stringstream op;
    op << i;
    return op.str();
}

int solveS(int i, int sum, string p) {

    if (i >= 10) {
        if (sum == 100) {
            cout << p + " = " << sum << endl;
        }
        return 0;
    }

    int iSum = 0;
    for (int j = i ; j<=9 ; j++) {
        iSum = iSum * 10 + j;
        if (i == 1) {
            solveS(j+1, iSum, i2s(iSum));
        } else {
            solveS(j+1, sum+iSum, p + " + " + i2s(iSum));
            solveS(j+1, sum-iSum, p + " - " + i2s(iSum));
        }
    }

    return 0;
}

int main() {

    //freopen("output.txt", "w+", stdout);

    solveS(1, 0, "");

    return 0;
}


Thursday, December 08, 2011

[UVa] 11150 - Cola

It was pretty disgusting for me all this time that I couldn't solve this one. I've been a pro at all of it's similar ones. This one actually gave me a very generalized approach.
I exhaustively checked for every possible borrowing. For one thing to be noted, you can borrow a max of 2 bottles. How? Just examine the 200 case without borrowing. In the end you'll have 2 unused bottles. Since it's the highest case, so there can be a max of 2 unused bottles, so a max of 2 returnable bottles/borrowable bottles. :|

/* 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 proc(int n, int borrow)
{
    int cola = 0-borrow;
    while (n>=3)
    {
        cola += n;
        cola -= n%3;
        n = (n/3 + (n%3));
    }
    cola += n;
    if (n<borrow) return -1;
    else return cola;
}

int results[300];

int main()
{
    int i, res1, res2, res3;

    for (i=0 ; i<=200 ; i++)
    {
        res1 = proc(i, 0);

        res2 = proc(i+1, 1);

        res3 = proc(i+2, 2);


        results[i] = max(max(res1,res2),max(res2,res3));
    }

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

 //    TEST CASE     //
 /*int kase=1, kounter=1;/**/

 while (scanf("%d",&i)==1)
 {
     printf("%d\n",results[i]);
 }
 return 0;
}

Friday, November 11, 2011

[UVa] 167 - The Sultan's Successors

One thing's for sure, Backtracking is damn fun!!! :D
I liked this one a lot. Though I had to take some help for this one. But it still increased a lot of knowledge about Backtracking.
#include <cstdio>
#include <iostream>
#include <iomanip>
using namespace std;

bool col[100], d1[100], d2[100];

class c
{
    public:
    int x, y;
};

c crd[100];

int knt=0, board[100][100];

c lst[1000];

int qplace(int n)
{
    if (n>8)
    {
        for (int i=1 ; i<=8 ; i++)
        {
            lst[knt++]=crd[i];
        }
        return 0;
    }
    for (int k=1 ; k<=8 ; k++)
    {
        if (!col[k] && !d1[n+k] && !d2[n-k+8])
        {
            col[k]=d1[n+k]=d2[n-k+8]=true;
            crd[n].x = n;
            crd[n].y = k;
            qplace(n+1);
            col[k]=d1[n+k]=d2[n-k+8]=false;
        }
    }
    return 0;
}

int main()
{
    int test, i, j, sum, max;
    knt = 0;
    for (i=0 ; i<=100 ; i++)
        col[i]=d1[i]=d2[i]=false;
    qplace(1);
    //cout << knt << endl;



    scanf("%d",&test);
    while (test--)
    {
        for (i=1 ; i<=8 ; i++)
        {
            for (j=1 ; j<=8 ; j++)
            {
                scanf("%d",&board[i][j]);
            }
        }
        max = 0;
        for (i=0 ; i<knt ; i+=8)
        {
            for (j=i, sum=0 ; (j-i)<8 ; j++)
            {
                sum += board[lst[j].x][lst[j].y];
            }
            if (sum>max)
                max = sum;
        }
        cout << setw(5) << max << endl;
    }
    return 0;
}

Saturday, October 22, 2011

[UVa] 374 - Big Mod

Used a recursive method from Art of Programming Contest
LL bigMod(LL b, LL p, LL m)
{
    if (!p) return 1;
    else if (!(p&0x01)) return power(bigMod(b,p/2,m),2)%m;
    else return (b%m * bigMod(b,p-1,m))%m;
}

#include <stdio.h>
#define LL long
LL power(LL a, LL p)
{
    int ret=1;
    while (p--)
        ret*=a;
    return ret;
}
LL bigMod(LL b, LL p, LL m)
{
    if (!p) return 1;
    else if (!(p&0x01)) return power(bigMod(b,p/2,m),2)%m;
    else return (b%m * bigMod(b,p-1,m))%m;
}

int main()
{
    LL b, p, m;
    while (scanf("%ld %ld %ld",&b,&p,&m)==3)
    {
       printf("%ld\n",bigMod(b,p,m));
    }
    return 0;
}

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...