Translate

Friday, 27 March 2020

Nearest Interesting Number Problemset 1183A

Question-
Polycarp knows that if the sum of the digits of a number is divisible by , then the number itself is divisible by . He assumes that the numbers, the sum of the digits of which is divisible by , are also somewhat interesting. Thus, he considers a positive integer  interesting if its sum of digits is divisible by .Help Polycarp find the nearest larger or equal interesting number for the given number a.That is, find the interesting number such that nand n is minimal.
Explanation-

1)We have to find a number whose sum is divisible by 4 if the entered number's sum is not divisible by 4 then we have to print the next number whose sum is divisible by 4.
2)Create a function which will extract the digits of the number entered and return its sum to the main function (here the function is named as digit).
3)In main function create an infinite loop which will check if the returned sum from the "digit" function is divisible by 4 or not.
4)If it is divisible then it print that number else it increment the value of entered number till next divisible number is found.

Program-


#include<iostream>

using namespace std;

int digit(int);

int main()

{

 int a;

 cin>>a;

 while(1)

 {
  int n;

  n=digit(a);

  if(n%4==0)

  break;

  else

  a++;
 }

 cout<<a;
}

int digit(int x)          //Function to extract the digits and calculate its sum.

 {
  int sum=0;

  while(x!=0)

  {
   int rem;

   rem=x%10;

   sum=sum+rem;

   x=x/10;
  }
  return sum;
 }

Link to problem on Codeforces-https://codeforces.com/problemset/problem/1183/A


Thursday, 26 March 2020

Football Problemset 96A

Question-
Petya loves football very much. One day, as he was watching a football match, he was writing the players' current positions on a piece of paper. To simplify the situation he depicted it as a string consisting of zeroes and ones.
A zero corresponds to players of one team; a one corresponds to players of another team.
If there are at least 7 players of some team standing one after another, then the situation is considered dangerous.
For example, the situation 00100110111111101 is dangerous and 11110111011101 is not.
You are given the current situation. Determine whether it is dangerous or not.

Explanation-
1)We have to find continuous seven zeroes or ones i.e., ("0000000") or ("1111111").
2)First make an array of char type size equal to 100 sunce required length of string is 100.
3)Take its input and define a pointer which will be having the value returned by strstr function
(you can read more about strstr function on geeksforgeeks or simply search on google and read on any of the sites)
4)It find you the first occurrence of ("0000000") or ("1111111") and required output will be printed.

Program-
#include<iostream>

#include<cstring> // C type string used as a standard namespace in C++

using namespace std;

int main()

{

 char s[100];

 cin>>s;

 char *p;

 if(p=strstr(s,"1111111")) // p returns a value other than zero

 cout<<"YES";

 else

   if(p=strstr(s,"0000000"))

   cout<<"YES";

  else

   cout<<"NO";

}
Link to the question on Codeforces-https://codeforces.com/problemset/problem/96/A

Bit++ Problemset 282A

Question-
The classic programming language of Bitland is Bit++. This language is so peculiar and complicated.
The language is that peculiar as it has exactly one variable, called x. Also, there are two operations:
Operation ++ increases the value of variable x by 1.
Operation -- decreases the value of variable x by 1.
A statement in language Bit++ is a sequence, consisting of exactly one operation and one variable x.
The statement is written without spaces, that is, it can only contain characters "+", "-", "X".
Executing a statement means applying the operation it contains.
A programme in Bit++ is a sequence of statements, each of them needs to be executed.
Executing a programme means executing all the statements it contains.
You're given a programme in language Bit++. The initial value of x is 0. Execute the programme and find its final value (the value of the variable when this programme is executed).

Explanation-
1)The first input will be of the number inputs user will give in the form of ++X,X++,X-- or --X.
2)So create an empty string which will take input ++X,X++,X-- or --X.
3)Create another four different strings which will have these bits in it.
4)Loop will be for taking the input by user one after the another as these bits.
5)So when the user input string will match our predefined string, then print the required output.
6)++X or X++ will increment the value of 'x' by one and X-- or --X will decrement the value of 'x' by one. Default value of 'x' is 0.

Program-
#include<iostream>

using namespace std;

int main()

{

 int n;

 cin>>n;

 int x=0;

 string s1;

 string s2("++X");

 string s3("X++");

 string s4("--X");

 string s5("X--");

 for(int i=0;i<n;i++)

 {

  cin>>s1;

  if(s1==s2||s1==s3)

  x=x+1;

  else

  x=x-1;

 }

 cout<<x;
}
Link to problem on Codeforces-https://codeforces.com/problemset/problem/282/A