Examples C++


Count the number of pairs

Given an array of numbers. Count how many pairs of elements in it are equal to each other.
It is believed that any two elements that are equal to each other form one pair, which must be calculated.


  • The array "seen" is used to find duplicate numbers in a list of elements.
  • I used the formula n(n−1)/2 for the number of pairs from an n element.
For example:
Sample Input:
5
1 1 1 1 1
Sample Output:
10

C++Code
																
#include  <iostream>
#include  <vector>
using namespace std;
int main( void ) {
 int N;
 cin >> N;//Count of numbers
    
int A[N]={1, 2, 3, 2 ,3};
vector  <int> b(N);
for(int i = 0; i < N; i++)
cin >> A[i];
int seen[N];
for(int i = 0; i < N; i++)
seen[i] = 0;
for(int i = 0; i < N; i++) {
if(seen[i] == 0) {
int count = 0;
for(int j = i; j < N; j++)
if(A[j] == A[i]) {
count += 1;
seen[j] = 1;
}
b[i]=count;
}
}
int s=0;
 for (int i = 0; i < N; i++) {
        if (b[i]>1) {
            s=s+(b[i]*(b[i]-1))/2;
        }
    }
    cout  << s;
return 0;
}

Valid IPv4 addres

Get string input from the user.
If this string is a valid IP address, programm prints YES else NO.
I used two interesting functions for this code: stoi() and isdigit().

  • the stoi() function converts a string to an integer value.
  • The isdigit() function, declared in the header file in C++, is used to determine whether the provided character represents a decimal digit. It returns 1(non-zero) if the provided character is a digit, 0 otherwise

C++Code
																
#include  <iostream>
#include  <string>
using namespace std;
int main()
{
    string s;
    getline(cin, s);
    
    char p = '.';
    char d = s[0];
    int h = 0;
    for (auto d : s)
        if (d == p)
            h++;
    if (h==0) {
        cout << "NO";
        return 0;
    }

    int first_dot = s.find_first_of("."); 
    string first_oct= s.substr(0, first_dot); 
    for (int i = 0; i < first_oct.length(); i++)
    {
        if (isdigit(first_oct[i])!=1) {
            cout << "NO";
            return 0;
        }
    }
    int second_dot = s.find_first_of(".", first_dot + 1);
    
    string second_oct= s.substr(first_dot+1, (second_dot-first_dot-1)); 
    for (int i = 0; i < second_oct.length(); i++)
    {
        if (isdigit(second_oct[i])!=1) {
            cout << "NO";
            return 0;
        }
    }
    int third_dot = s.find_first_of(".", second_dot + 1);
    string third_oct= s.substr(second_dot+1, ((third_dot-second_dot)-1)); 
    
    for (int i = 0; i < third_oct.length(); i++)
    {
        if (isdigit(third_oct[i])!=1) {
            cout << "NO";
            return 0;
        }
    }
    string fourth_oct= s.substr(third_dot+1, ((third_dot-second_dot)-1));
    for (int i = 0; i < fourth_oct.length(); i++)
    {
        if (isdigit(fourth_oct[i])!=1) {
            cout << "NO";
            return 0;
        }
    }
    
int first_dig = stoi(first_oct);
int second_dig = stoi(second_oct);
int third_dig = stoi(third_oct);
int fourth_dig = stoi(fourth_oct);

  if ((first_dig<=255)&&(second_dig<=255)&&(third_dig<=255)&&(fourth_dig<=255)){
      cout <<"YES";
  }
  else {
      cout <<"NO";
  }
    
    return 0;
}

Is the point belong to the given shaded area?

Does the point A(xa,ya) belong to one of the two regions bounded by a circle and two straight lines?
Equations of the straight line y=kx+b; By the cells, we determine that the equation of a straight line at 45 degrees: y1 = -x; second: y2 = 2x+2. Equation of a circle, given that the coordinates of the center and the radius are (-1,1) and 2, respectively: (x+1)^2 + (y-1)^2 = 4

C++Code
																

#include 
using namespace std;
#include 
bool isPointInSquare(float xa, float ya) {
    bool flag;
    float part1;
    float part2;
    flag = true;
    if (ya >= 0) {
        if (
            (((xa + 1) * (xa + 1) + (ya - 1) * (ya - 1)) <= 4) and
            (ya >= -xa) and
            (ya >= ((2 * (xa)) + 2))
            )
        {
            flag = true;
        }
        else {
            flag = false;
        }
    }
    else {
        if (
            (((xa + 1) * (xa + 1) + (ya - 1) * (ya - 1)) >= 4) and
            (ya <= -xa) and
            (ya <= ((2 * (xa)) + 2))
            )
        {
            flag = true;
        }
        else {
            flag = false;
        }
    }
    return flag;
}
int main()
{
    float xa, ya;
    cin >> xa >> ya;
    (isPointInSquare(xa, ya)) ? cout << "YES" : cout << "NO";
    return 0;
}