Код
#include <iostream>#include <vector>int main() { int n; std::vector<int> sequence; std::cin >> n; for(auto i = 0; i < n; ++i) { int number; std::cin >> number; sequence.push_back(number); } int as = 0, bs = 0, cs = 0; for (auto &el : sequence) { switch (el) { case 1: ++as; break; case 2: ++bs; break; case 3: ++cs; break; default: throw std::runtime_error("All elements shall be into [1;3] range."); } } int an_answer = std::min({bs + cs, as + cs, bs + cs}); std::cout << an_answer << std::endl; return 0;}
Код, приведенный ниже, работает не только со словами, но и со строками.
Например, "На в лоб, Болван" определяется как полиндром.
using System;
using System.Linq;
using System.Text.RegularExpressions;
namespace PolyndromeFinder
{
class Program
{
//
// [START] Тот самый участок кода
//
static bool IsPolyndrome(string victim)
{
victim = victim // Нормализуем строку
.Replace(" ", "")
.Replace(",", "")
.Replace(".", "")
.Replace("?", "")
.Replace("!", "")
.ToLower();
var check = new Regex(victim); // Создаём на её основе регулярное выражение
return check.IsMatch(new string(victim.Reverse().ToArray())); // Переворачиваем строку и сверяем с регуляркой
}
//
// [END] Тот самый участок кода
//
static void Main(string[] args)
{
bool answer;
while (true)
{
Console.Write("Введите строку: ");
string superPositionPolyndrome = Console.ReadLine();
Console.WriteLine("Это{0} полиндром!\n", (answer = IsPolyndrome(superPositionPolyndrome))? "": " НЕ");
}
}
}
}