#include <iostream>
bool is_prime(int n) {
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) return false;
}
return true;
}
int main()
{
int count = 10001, prime = 0, res = 0;
for(int i = 2; i < 1000000; ++i) {
if(is_prime(i)) ++prime;
if(count == prime) {
res = i;
break;
}
}
std::cout << res;
return 0;
}
#include <iostream>
#include <string>
#include <cctype>
std::string checkRepeat(std::string& str) {
if(!std::isdigit(str[0]) || !std::isdigit(str[str.length()-1])) return "Error!";
std::string symbols{"*/+-"}, res{"Ok"};
for(int i{0}; i < str.length(); ++i) {
bool flag{false}, flag2{false};
for(char ch : symbols) {
if(str[i] == ch) flag = true;
}
if(flag && i < str.length()-1) {
for(char ch : symbols) {
if(str[i+1] == ch) flag2 = true;
}
}
if(flag && flag2) res = "Error!";
}
return res;
}
int main()
{
std::string s{"43+2/4**2-5"};
std::cout << checkRepeat(s);
return 0;
}
Вот придумал покороче:
#include <iostream>
#include <string>
std::string checkRepeat(std::string& str) {
std::string symbols{"+-*/"};
for(int i{0}; i < str.length(); ++i) {
int ind = symbols.find(str[i]);
int ind2 = -1;
if(ind > -1 && i < str.length()-1) ind2 = symbols.find(str[i+1]);
if(ind2 > -1) return "Error!";
}
return "Ok";
}
int main() {
std::string s{"2+4/4**2-1"};
std::cout << checkRepeat(s);
return 0;
}
#include <iostream>
int main() {
int arr[]{1,2,3,4};
int arr2[]{5,6,7,8,9,10};
int len = std::size(arr);
int len2 = std::size(arr2);
int len3 = len > len2 ? len : len2;
int res[len3];
int sum{0};
for(int i{0}; i < len3; ++i) {
sum = i < len ? arr[i] : 0;
sum += i < len2 ? arr2[i] : 0;
res[i] = sum;
}
for(int el : res) std::cout << el << ' ';
return 0;
}
#include <iostream>
int main() {
int arr[]{2,1,2,2,0,7,1,3,7,5,7,0,1,2};
int len = std::size(arr);
int max = arr[0], count{0}, ind{0}, sum{0};
for(int i{0}; i < len; ++i) {
if(max < arr[i]) max = arr[i];
if(i == len-1) {
for(int j{0}; j < len; ++j) {
if(arr[j] == max) { ++count; ind = j; }
}
while(++ind < len) { sum += (arr[ind] * arr[ind]); }
}
}
std::cout << '\n' << max << ' ' << count << ' ' << sum;
return 0;
}
#include <iostream>
#include <string>
int main() {
std::string str{"first word and last"};
int ind = str.find(' ');
std::string first_word = str.substr(0, ind);
int ind2 = str.rfind(' ');
std::string last_word = str.substr(ind2+1);
str.replace(0, ind, last_word);
str.replace(ind2, last_word.length(), first_word);
std::cout << str;
return 0;
}
#include <iostream>
int main() {
int arr[]{5,2,1,3,1,4,2,1,6,2,6};
int res{0}, max{0};
for(int i : arr) {
int count{0};
for(int j : arr) {
if(i == j) ++count;
}
if(max < count) { res = i; max = count; }
}
std::cout << "Most encountered number in this array is number - "
<< res << ", which encountered " << max << " times";
return 0;
}
#include <iostream>
#include <string>
int main() {
std::string str{" Hi Bro! How are you? ";
std::string res{""}, tmp{""};
int len = str.length();
for(int i{0}; i < len; ++i) {
if(str[i] != ' ') tmp += str[i];
if(str[i] == ' ' || i == len-1) {
if(res.length() < tmp.length()) res = tmp;
tmp = "";
}
}
std::cout<<res;
return 0;
}
#include <iostream>
int main() {
int arr[5]{};
for(int& i : arr) {
std::cout << "Enter number: ";
std::cin >> i;
}
int sum{0};
for(int i : arr) sum += i < 0 ? i : 0;
std::cout << "Sum negative numbers in array is: " << sum;
return 0;
}
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string> split(std::string str, char delimiter) {
std::string tmp{""};
std::vector<std::string> vec;
for(int i{0}; i < str.length(); ++i) {
if(str[i] == delimiter) { vec.push_back(tmp); tmp = ""; continue; }
tmp += str[i];
}
vec.push_back(tmp);
return vec;
}
int main()
{
std::string s{"name^age"};
std::vector<std::string> res = split(s, '^');
std::cout << "Result is: ";
for(auto i : res) std::cout << i << ' ';
return 0;
}
#include <iostream>
#include <ctime>
void fillRandom(int* arr, int size, int min, int max) {
for(int i{0}; i < size; ++i) {
arr[i] = min + (rand() % ((max - min) + 1));
}
}
int main() {
srand(time(NULL));
int len{10}, min{10}, max{99};
int array[len];
fillRandom(array, len, min, max);
for(int& i : array) std::cout << i <<' ';
return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
void fill_random(std::vector& v, int min, int max) {
int i{0};
auto lambda = [&](int index){ v[i] = min + (rand() % ((max - min) + 1)); ++i; };
std::for_each(v.begin(), v.end(), lambda);
}
int main() {
srand(time(NULL));
int size{10}, min{10}, max{99};
std::vector<int> vec(size);
fill_random(vec, min, max);
for(int& i : vec) std::cout << i << ' ';
return 0;
}
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
int main() {
std::string str{"This string in file"};
std::stringstream s(str);
std::vector<std::string> vec;
std::string word{""};
while(s >> word) vec.push_back(word);
for(auto &i : vec) std::cout << i << '\n';
return 0;
}
#include <iostream>
#include <regex>
#include <string>
#include <vector>
int main() {
std::string str{"Hello! This string of your file"};
std::regex reg{"\\S+"};
std::vector<std::string> vec;
std::smatch sm;
while(std::regex_search(str, sm, reg)) {
vec.push_back(sm.str());
str = sm.suffix();
}
for(auto &i : vec) std::cout << i <<' \n';
return 0;
}
#include <iostream>
#include <string>
#include <vector>
void split(std::string& str, std::vector<std::string>& vec) {
std::string tmp{""}, delimiter{".!?"};
int pos{0};
for(int i{0}; i < str.length(); ++i) {
tmp += str[i];
pos = delimiter.find(str[i]);
if(pos > -1) {
vec.push_back(tmp);
tmp = "";
while(str[i+1] == ' ') ++i;
}
}
}
int main() {
std::string s{"Hi bro! How are you? This your string of file."};
std::vector<std::string> res;
split(s, res);
for(auto& el : res) std::cout << el << '\n';
return 0;
}
#include <iostream>
int main() {
int arr[]{1,1,1,3,4,4,2,8,7,9};
int sample[]{1,2,3,4,5,6,7,8,9,10};
int count{0}, res{0};
for(int& i : sample) {
for(int& j : arr) {
if(i == j) ++count;
}
if(count > 1) ++res;
count = 0;
}
std::cout << res;
return 0;
#include <iostream>
#include <set>
int main() {
int arr[]{1,1,1,3,4,4,2,8,7,9};
std::set<int> set;
for(int i : arr) set.insert(i);
int count{0}, res{0};
for(int el : set) {
for(int j : arr) {
if(el == j) ++count;
}
if(count > 1) ++res;
count = 0;
}
std::cout << res;
return 0;
#include <stdio.h>
int main() {
int arr[10] = {-1, 0, 5, -9, 8, -3, 5, 0, -7, 4};
int tmp = 0;
for(int i = 0; i < 9; ++i) {
for(int j = 0; j < 9; ++j) {
if(arr[j] < 1) {
tmp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = tmp;
}
if(arr[j] < 0) {
tmp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = tmp;
}
}
}
for(int i = 0; i < 10; ++i) printf("%d%c", arr[i], ' ');
return 0;
}
std::vector<std::string> explode(std::string separator, std::string input) {
std::vector<std::string> vec;
for(int i{0}; i < input.length(); ++i) {
int pos = input.find(separator, i);
if(pos < 0) { vec.push_back(input.substr(i)); break; }
int count = pos - i;
vec.push_back(input.substr(i, count));
i = pos + separator.length() - 1;
}
return vec;
}
#include <iostream>
int main() {
int arr[]{-1, 2, -3, 4, -6, -6, 7, -8, 9, 1};
for(int i{0}; i < std::size(arr); ++i) {
if(arr[i] > 0 && arr[i+1] < 0) {
std::cout << arr[i] << ' ' << arr[i+1] << ' ';
++i;
}
if(arr[i] < 0 && arr[i+1] > 0) {
std::cout << arr[i] << ' ' << arr[i+1] << ' ';
++i;
}
}
return 0;
}
#include <iostream>
template <typename T>
void commonElem(T a[], T a2[], T a3[], int len, int len2, int len3) {
std::cout<<"Common elements arrays is: ";
for(int i{0}; i < len; ++i) {
bool f2{false}, f3{false};
for(int j{0}; j < len2; ++j) if(a[i] == a2[j]) f2 = true;
for(int k{0}; k < len3; ++k) if(a[i] == a3[k]) f3 = true;
if(f2 && f3) std::cout << a[i] << ' ';
}
}
int main() {
int arr[]{23,12,54,2,7}, arr2[]{2,23,1,65}, arr3[]{43,2,76,4,23,8,96};
int l = std::size(arr), l2 = std::size(arr2), l3 = std::size(arr3);
commonElem(arr, arr2, arr3, l, l2, l3);
return 0;
}
#include <iostream>
#include <string>
#include <sstream>
int main() {
std::string str;
std::cout << "Enter string: ";
std::getline(std::cin, str);
std::stringstream s(str);
std::string tmp, res;
int count = 0;
while(s >> tmp) {
res += tmp + ' ';
++count;
if(count%3==0) res += tmp + ' ';
}
res = res.substr(0, res.length()-1);
std::cout<<res;
return 0;
}