Easy Coding Contest-11 Editorial

All contest related discussion goes here. Both Dev Skill and Non Dev Skill contest related discussion is allowed. (But please keep the forum clean)

Easy Coding Contest-11 Editorial

Contest Archive Link: https://devskill.com/ContestArchive/Con ... d48d9b4474

Contest Rank List: https://devskill.com/home/contestrankli ... d48d9b4474

Problem A: Programming Language Feature in DevSkill
Problem Setter: Jaber Kibria

Here you have to count the total numbers of programming languages mentioned in the problem statement.
Code: Select all
`C++ Java    C#  PHP Python  Go  Ruby    VB.Net  Objective-C JavaScript  Perl`

So, there is total 11 languages. You have to print "11" in a line. Problem B: Even or Odd
Problem Setter: MD.Amir-Al-Fahim

In this problem,you have to figure out the sum of Nth-Mth prime is even or odd.
We know that 2 is the only prime which is even and it is the 1st prime number.
So,if N=1 then the answer is Odd if the difference between N and M is even otherwise the answer is Even.
If N>1 then the answer is Odd if the difference between N and M is odd otherwise the answer is Even.
Here difference=M-N+1.

Problem C: Power Function
Problem Setter: Ashini Singha

You are given a number x, you have to print the left-most decimal digit of 2^x. As the value of x can be maximum 60, you can handle easily using long long data type. and find the leftmost digit by converting it to string type and printing fist character. Also, you should print sign before last digit depending upon whether x is odd or even.

Problem D: Special Matrix
Problem Setter: Emrul Chowdhury

This is a straight-forward problem.
If we observe the given picture, we can see that there are four state of decreasing the value of matrix. We can easily build the matrix by giving four different conditions.

Firstly define all the value of 2D array named mat to 0. Create a pair array named ans for storing the index of every value of matrix.

Now, define l = 1, r = n, state = 1, x = n * n;

Then run a loop while l is less than or equal to r. In this loop, give four different state for building the desired matrix by running another loop inside the while loop. The condition for different states,

• If the value of state is 1, run a loop l to r.
If mat[l][i] = 0,
Replace mat[l][i] by x.
Define ans[x].first = l, ans[x].second = i.
Decrease x by 1.

• Else if the value of state is 2, run a loop l to r.
If mat[i][r] = 0,
Replace mat[i][r] by x.
Define ans[x].first = i, ans[x].second = r.
Decrease x by 1.

• Else if the value of state is 3, run a loop r to l.
If mat[r][i] = 0,
Replace mat[r][i] by x.
Define ans[x].first = r, ans[x].second = i.
Decrease x by 1.

• Else if the value of state is 4, run a loop r to l.
If mat[i][l] = 0,
Replace mat[i][l] by x.
Define ans[x].first = i, ans[x].second = l.
Decrease x by 1.

Every time after the nested loop will be finished, increase the value of state by 1. If somewhere value of state will become more than 4, make the value of state 1 again, increase l by 1 and decrease r by 1.

For each query, for given value m, print ans[m].first & ans[m].second in correct format.

Problem E: Egg, Banana and Bread
Problem Setter: Bishal Gautam

In this problem, you are given Cost of Egg, Banana and Bread, X, Y and Z respectively. You also have T taka with you . You have to utilize your taka as much possible so that amount of Egg, Banana and Bread are in increasing amount.
If we look into the limit carefully , we may see that X,Y,Z will be always greater than 10. And, total cost T is always <=1000. So, we can easily iterate by looping upto 100 and fix the amount for X,Y,Z and find out the maximum taka we may utilize within limit T, say max. So, the answer will be (T-max).  Posts: 43
Joined: Tue Jan 17, 2017 10:10 pm

Re: Easy Coding Contest-11 Editorial

My code for power function is given below which shows compile error. The screen shot of the error is also attached.
Code: Select all
`#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>int main(int argc, const char * argv[]) {    int T,x;    long long function;    char buf;    scanf("%d",&T);    char (*answer);    answer = malloc(T*2*sizeof(char));    for (int i = 0; i<T; i++) {        scanf("%d",&x);        function = pow(2, x);        sprintf(buf, "%lld",function);        if (!(x%2)) {            answer[i] = '+';            answer[i] = buf;        }        else{            answer[i] = '-';            answer[i] = buf;            }    }    free(answer);    for (int i = 0; i<T; i++) {        printf("%c%c\n",answer[i],answer[i]);    }    return 0;}`
Attachments Screen Shot 2017-06-01 at 4.49.08 PM.png (44.37 KiB) Viewed 2642 times

Posts: 28
Joined: Sat Aug 06, 2016 11:02 pm

Re: Easy Coding Contest-11 Editorial

The code given below shows WA for power function problem.
Code: Select all
`#include <stdio.h>#include <string.h>#include <stdlib.h>long long power(int);int main(int argc, const char * argv[]) {    int T,x;    long long function;    char buf;    scanf("%d",&T);    char (*answer);    answer = malloc(T*2*sizeof(char));    for (int i = 0; i<T; i++) {        scanf("%d",&x);        function = power(x);        sprintf(buf, "%lld",function);        if (!(x%2)) {            answer[i] = '+';            answer[i] = buf;        }        else{            answer[i] = '-';            answer[i] = buf;            }    }    free(answer);    for (int i = 0; i<T; i++) {        printf("%c%c\n",answer[i],answer[i]);    }    return 0;}long long power(int powerRaised){    if (powerRaised != 0)        return (2*power(powerRaised-1));    else        return 1;}`

Posts: 28
Joined: Sat Aug 06, 2016 11:02 pm

Re: Easy Coding Contest-11 Editorial

Hello arabin, As you are freeing memory before printing output. So, no output is printed.
If you move a line with "free(answer)" to appropriate place, then your code is perfect  Posts: 43
Joined: Tue Jan 17, 2017 10:10 pm

Re: Easy Coding Contest-11 Editorial

Code: Select all
`#include <stdio.h>struct myStruct {    long long x;    long long y;};void build_mat(long long mSize, long long mat[mSize][mSize], long long size, long long val, struct myStruct ans[]){    long long i, row, col;        if (size<1) {        return;    }    row = col = (mSize-size)/2;    if (size == 1) {        mat[row][col] = val;    }        for (i = 0; i<size-1; i++) {        mat[row][col] = val;        ans[val].x = row; ans[val].y = col;        col++;        val--;    }    for (i = 0; i<size-1; i++) {        mat[row][col] = val;        ans[val].x = row; ans[val].y = col;        row++;        val--;    }    for (i = 0; i<size-1; i++) {        mat[row][col] = val;        ans[val].x = row; ans[val].y = col;        col--;        val--;    }    for (i = 0; i<size-1; i++) {        mat[row][col] = val;        ans[val].x = row; ans[val].y = col;        row--;        val--;    }    build_mat(mSize, mat, size-2, val,ans);}int main(int argc, const char * argv[]) {        long long N;    long long Q;    scanf("%lld %lld",&N,&Q);    long long mat[N][N];    struct myStruct ans[N*N];    long long query[Q];    build_mat(N,mat,N,N*N,ans);    for (long long i = 0 ; i<Q; i++) {        long long M;        scanf("%lld",&M);        query[i] = M;    }    for (long long i = 0; i<Q; i++) {        printf("%lld, %lld\n",ans[query[i]].x+1,ans[query[i]].y+1);    }        return 0;}`

hi i am facing RTE for the problem special matrix can anyone help me please
Attachments Screen Shot 2017-06-11 at 3.32.31 PM.png (17.19 KiB) Viewed 2580 times

Posts: 5
Joined: Wed Jul 27, 2016 11:04 pm

Re: Easy Coding Contest-11 Editorial

Declaring too large array inside main function ,leads to RTE. You may declare large arrays globally. Hope this will help. Posts: 43
Joined: Tue Jan 17, 2017 10:10 pm

Re: Easy Coding Contest-11 Editorial

thank you Bishal vai thaks a lot. Posts: 5
Joined: Wed Jul 27, 2016 11:04 pm

Who is online
Users browsing this forum: No registered users and 1 guest 