M
M
maxemga2020-11-30 20:51:59
C++ / C#
maxemga, 2020-11-30 20:51:59

How to display the number of polyndromes in the input sequence?

Print the number of polyndromes in the given sequence
Input data:
The length of the sequence and the sequence.
Output:
Number of polyndromes.
------
Example:
Input d:
length: 4
Sequence: 1231

Output:
Number of polyndromes: 7

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
NIKITF, 2021-08-27
@NIKITF

Hello.
A palindrome is a number that has symmetry.
Example: 1, 55, 747, 7887.
In your example, there are definitely not 7 of them. I
offer my own solution to the problem:

#include<iostream>
#include<string>
#include<sstream>
#include<iomanip>
using namespace std;
class PalindromCH
{
private:
  bool palindrom(const string& d)
  {
    for (unsigned g = 0; g < d.size() / 2; g++)
    {
      if (d[g] != d[d.size() - g - 1])
      {
        return 0;
      }
    }
    return 1;
  }
  unsigned counter(stringstream& c, string& d)
  {
    unsigned number{ 0 }; cout << endl;
    cout << "Entered sequence of numbers:" << endl;
    while (c >> d)
    {
      cout << setw(7) << d;
      if (palindrom(d))
      {
        number++;
      }
    }
    return number;
  }
  void check_digit(string& i)
  {
    cin >> i;
    try
    {
      stod(i);
    }
    catch (exception& u)
    {
      cerr << u.what() << endl;
    }
  }
public:
  void examination()
  {
    unsigned f, t{ 0 }; stringstream v; string inp;
    cout << "Enter the number of elements: ";
    check_digit(inp);
    f = stoi(inp);
    while (f--)
    {
      inp.clear();
      cout << "Enter ["<< ++t <<"]: ";
      check_digit(inp);
      v << inp << " ";
    }
    cout << endl << endl << counter(v, inp);
    cout << " palindroms have been founded" << endl;
  }
};
int main()
{
  PalindromCH text;
  text.examination();
  return 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question