Showing posts with label Dev Skill. Show all posts
Showing posts with label Dev Skill. Show all posts

Friday, April 28, 2017

Solution of DevSkill DCP-316: Names

See the problem: DCP-316: Names

/*  Bismillahir Rahmanir Rahim 
    Its just a string multiplication.
*/
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a, i, j, n, t, z, sz, ar[1000001];
string s;

void multiply(int i){  // string multiplication
    int carry=0, j;
    for(j=10000; j>10000-z; j--){
        int temp=ar[j]*i+carry;
        ar[j]=temp%10;
        carry=temp/10;
    }
    while(carry){
        ar[j--]=carry%10;
        carry=carry/10;
        z++;
    }
}

int main(){

    cin>>t;
    while(t--){
        cin>>n>>s;
        sz=s.size();
        ar[10000]=1;
        n=n-sz, z=1;
        for(i=1; i<=n; i++){
            multiply(26);
        }
        for(i=10001-z; i<=10000; i++) cout<<ar[i];
        cout<<endl;
    }
    return 0;
}

Solution of Dev Skill DCP-237: What a problem!

See the problem:  DCP-237: What a problem!

/*  Bismillahir Rahmanir Rahim
    Its better to try yourself than follow others code.
    My solution Idea: 
    To store the array use one dimensional array 
    Now print according to their system. 
 */
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll i, j, n, k, cs=1, m, t, x, sz,  ar[1000006];

int main(){
     cin>>t;
     while(t--){
        cin>>n;
        k=1;
        for(i=1; i<=n; i++){
            for(j=1; j<=n; j++) cin>>ar[k++];
        } 
        cout<<"Case #"<<cs++<<": ";
        m=n+1;
        x=n*n-(n-1);
        if(n==1) cout<<ar[1];
        else{
            cout<<ar[x];
            for(i=1; ; i++){
                x++;
                cout<<" "<<ar[x];
                for(j=1; j<=i; j++){
                    x=x-m;
                    cout<<" "<<ar[x];
                }
                if(x==1||x==n*n) break;
                i++;
                x=x-n;
                cout<<" "<<ar[x];
                for(j=1; j<=i; j++){
                    x=x+m;
                    cout<<" "<<ar[x];
                }
                if(x==1||x==n*n) break;
            }
            if(x==1){
                for(i=2; ; i++){
                    x++;
                    cout<<" "<<ar[x];
                    if(x==n) break;
                    for(j=i; j<n; j++){
                        x=x+m;
                        cout<<" "<<ar[x];
                    }
                    if(x==n) break;
                    x=x-n;
                    cout<<" "<<ar[x];
                    if(x==n) break;
                    i++;
                    for(j=i; j<n; j++){
                        x=x-m;
                        cout<<" "<<ar[x];
                    }
                    if(x==n) break;
                }
            }
            else{
                for(i=2; i<n; i++){
                    if(x==n) break;
                    x=x-n;
                    cout<<" "<<ar[x];
                    for(j=i; j<n; j++){
                        x=x-m;
                        cout<<" "<<ar[x];
                    }
                    if(x==n) break;
                    x++;
                    cout<<" "<<ar[x];
                    if(x==n) break;
                    i++;
                    for(j=i; j<n; j++){
                        x=x+m;
                        cout<<" "<<ar[x];
                    }
                    if(x==n) break;
                }
            }
        }
        cout<<endl;
     }
    return 0;
}

Saturday, March 18, 2017

Solution of Dev Skill DCP-1: Big Sum

See the problem  Big Sum
/*  Bismillahir Rahmanir Rahim
    Problem-Logical.
The following code is bulky in size but logic is simple. 
 456
 789
-----
 1461 (its the answer)[add start from left to right]
*/
#include<bits/stdc++.h>
using namespace std;

int main(){
    string s;
    while(cin>>s){
        char s1[109], s2[109];
        int i, temp, c=0, fl=0, sz1, sz2, y=0, x=0;
        for(i=0; i<s.size(); i++){
            if(s[i]==',') fl=1;
            else if(!fl) s1[x++]=s[i]; // until getting ','
            else s2[y++]=s[i]; // after getting ','
        }
        fl=0;
        if(x>=y){   // if(first string >= second string)
            for(i=0; i<y; i++){
                temp=(int)(s1[i]-'0')+(int)(s2[i]-'0')+c;
                if(temp==10){
                    if(fl)cout<<0; c=1;
                }
                else if(temp>10){
                    cout<<temp%10; c=1; fl=1;
                }
                else{
                    cout<<temp; c=0; fl=1;
                }
            }
            for(i=y; i<x; i++){ // after ending of second string
                temp=(int)(s1[i]-'0')+c;
                if(temp==10){
                    if(fl)cout<<0; c=1;
                }
                else if(temp>10){
                    cout<<temp%10; c=1; fl=1;
                }
                else{
                    cout<<temp; c=0; fl=1;
                }
            }
        }
        else{  // same as before, just replace y with x & x with y
            for(i=0; i<x; i++){
                int temp=(int)(s1[i]-'0')+(int)(s2[i]-'0')+c;
                if(temp==10){
                    if(fl)cout<<0; c=1;
                }
                else if(temp>10){
                    cout<<temp%10; c=1; fl=1;
                }
                else{
                    cout<<temp; c=0; fl=1;
                }
            }
            for(i=x; i<y; i++){
                temp=(int)(s2[i]-'0')+c;
                if(temp==10){
                    if(fl)cout<<0; c=1;
                }
                else if(temp>10){
                    cout<<temp%10; c=1; fl=1;
                }
                else{
                    cout<<temp; c=0; fl=1;
                }
            }
        }
        if(c) cout<<c%10;
        cout<<endl;
    }
    return 0;
}