A Blog about Programming,Algorithms,Coding Competitions, and Discrete Mathematics!
Our First Contest, and it was a huge success. Thank you for participating.
Here are the editorial of the problems..
1. Simple Sum 4
Easiest in the Problem set.. and Almost everyone solved it.
Here is my solution code..
Here are the editorial of the problems..
1. Simple Sum 4
Easiest in the Problem set.. and Almost everyone solved it.
Here is my solution code..
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int n;
while ( scanf("%d",&n) != EOF )
{
for( int i = 0 ; i < n ; i++)
{
int x ,y;
scanf("%d %d",&x,&y);
printf("%d\n",x+y);
}
}
return 0;
}
2 . Kazi And Quadrangles
Summary : Given 4 lengths, you are to determine if these can form a square, rectangle, quadrangle or none of these.
Explanation :
Easiest way is to sort the array of lengths then check the following:
- Square: all 4 sides are equal.
- Rectangle: each 2 sides are equal.
- Quadrangle: maximum of these lengths must be less than the sum of other 3.
- None: if none of the above conditions is satisfied, then print "None".
// Accepted
#include <algorithm>
#include <cstdio>
using namespace std;
int len[4];
int main(void){
int i, t;
scanf("%d", &t);
while (t--){
int s = 0;
for (i = 0; i < 4; i++){
scanf("%d", &len[i]);
s += len[i];
}
sort(len, len+4);
if (len[0] == len[1] && len[2] == len[3]){
if (len[1] == len[2])
printf("square\n");
else
printf("rectangle\n");
} else {
bool works = true;
for (i = 0; i < 4; i++)
works &= s - len[i] > len[i];
if (works)
printf("quadrangle\n");
else
printf("None\n");
}
}
}
3. Pooja And Java
In this problem you should answer to 4 questions:
1) Can we use type byte to store N?
2) Can we use type short to store N?
3) Can we use type int to store N?
4) Can we use type long to store N?
We should check these conditions in the given order. If all these conditions are wrong, the answer is BigInteger.
The simplest way to check these conditions is to store numbers as strings and write a function to compare such strings. In Java you can use type BigInteger.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
cin >> s;
if(s.size() < 3 || (s.size() == 3 && s <= "127"))
cout <<"byte";
else if (s.size() < 5 || (s.size() == 5 && s <= "32767"))
cout <<"short";
else if(s.size() < 10 || (s.size() == 10 && s <= "2147483647"))
cout <<"int";
else if(s.size() < 19 || (s.size() == 19 && s <= "9223372036854775807"))
cout <<"long";
else cout <<"BigInteger";
}
4. Fibonacci Revisited
In this problem you need to calculate two fibonacci number which are greater and smaller then given number.
So minimum steps required is min(greater-N,N-smaller);
#include<iostream>
using namespace std;
int find(int N) {
int a = 0, b = 1;
while(b < N) {
int c = a+b;
a = b;
b = c;
}
return min(N-a, b-N);
}
}
int main() {
long long N;
cin>>N;
cout <<find(N) << endl;
return 0;
}
5. Primorial Numbers
It was challenge Problem Toughest in the given Problem set.
I'll write detailed explanation in free time..
Here is my solution.. try to under the code :D
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=200000+10;
char st[maxn];
int res[maxn];
int need[maxn];
int n;
int main()
{
scanf("%s",st+1);
n=strlen(st+1);
for (int i=n;i;i--)
if (st[i]<'4') need[i]=0;else
if (st[i]=='4') need[i]=min(1,need[i+1]);else
if (st[i]<'7') need[i]=1;else
if (st[i]=='7') need[i]=need[i+1]+1;
else need[i]=1000000000;
if (n%2==0)
{
bool ok=1;
int a=0,b=0,c=4,flag=0;
for (int i=1;i<=n;i++)
if (flag)
{
if (a+1<=n/2) res[i]=4,a++;
else res[i]=7,b++;
} else
{
if (st[i]<'4' && a+1<=n/2) res[i]=4,a++,flag=1;else
if (st[i]=='4' && a+1<=n/2 && need[i+1]<=n/2-b)res[i]=4,a++;else
if (st[i]<'7' && b+1<=n/2) res[i]=7,b++,flag=1;else
if (st[i]=='7' && b+1<=n/2) res[i]=7,b++;
else ok=0;
}
if (ok)
{
for (int i=1;i<=n;i++) printf("%d",res[i]);
printf("\n");
return 0;
}
n+=2;
} else n+=1;
for (int i=1;i<=n/2;i++) printf("4");
for (int i=1;i<=n/2;i++) printf("7");
printf("\n");
}
Thank you so much friends for participating in the contest. I hope you enjoyed. Our next Contest will be more competitive and Problems will be tricky and Tough.
Keep Practicing Till then.
Happy Coding!
:D