#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;
}
const print = s => console.log(s);
let s = `<a href="https://google.ru/link/123abc/><img src="google.png"><br>Гугл</a>`;
let s2 = `<a href="https://ya.ru/link><img src="ya.png"><br>Яндекс</a>`;
let s3 = `<a href="https://rambler.ru/link/address/><img src="rambler.png"><br>Рамблер</a>`;
let a = s.match(/(?<=<br>).+(?=<)/g)[0];
let b = s2.match(/(?<=<br>).+(?=<)/g)[0];
let c = s3.match(/(?<=<br>).+(?=<)/g)[0];
let arr = [[a, s], [b, s2], [c, s3]];
arr.sort((a, b) => a[0].charCodeAt(0) - b[0].charCodeAt(0));
print(arr);
function display_with_spaces(num) {
let str = String(num);
let left = str.length % 3;
let res = '';
let ind = 0;
if(left > 0) {
res = str.substr(ind, left) + ' ';
ind += left;
while(ind < str.length) {
res += str.substr(ind, 3);
if(str.length - ind > 3) res = res + ' ';
ind += 3;
}
}
else {
while(ind < str.length) {
res += str.substr(ind, 3);
if(str.length - ind > 3) res = res + ' ';
ind += 3;
}
}
return res;
}
let print = s => console.log(s);
let stud = [
{ name: "One", marks: [8, 10, 7, 5, 4] },
{ name: "Two", marks: [5, 2, 8, 5, 3] },
{ name: "Three", marks: [6, 9, 3, 6, 7] },
{ name: "Fore", marks: [1, 7, 8, 4, 10] },
{ name: "Five", marks: [5, 8, 6, 9, 3] },
];
let st_min = {...stud[0]}, st_max = {...stud[0]};
let av_min = av_max = st_min.marks.reduce((a, c) => a + c, 0) / st_min.marks.length;
for(let ob of stud) {
let av = ob.marks.reduce((a, c) => a + c, 0) / ob.marks.length;
if(av_min > av) {
st_min = {...ob}; av_min = av;
}
if(av_max < av) {
st_max = {...ob}; av_max = av;
}
}
print(st_min);
print(st_max);
for(let ob of arr) {
for(let el of arr2) {
if(ob.id === el) ob.win = true;
}
}
console.log(arr);
#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;
}
let print = s => console.log(s);
let arr = [1,'3', 2, 3, {}, NaN, [2, 4, 5, []]];
arr = arr.flat(Infinity);
let sum = 0;
for(let el of arr) {
if(Number.isFinite(el)) sum += el;
}
print(sum);
let print = s => console.log(s);
let arr = [1, '3', 2, 3, {}, NaN, [2, 4, 5, [] ]];
let sum = arr.flat(Infinity).filter(el => Number.isFinite(el)).reduce((acc,el) => acc + el, 0);
print(sum);
#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;
}
let arr = [2,1,3,5,2,7,1,4];
let min = arr[0], max = arr[0];
for(let i of arr) {
if(min > i) min = i;
if(max < i) max = i;
}
console.log('Result is: ' + (max-min));
#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;
}
let print = s => console.log(s);
function remove_vowels(s) {
let res = '';
for(let i of s) {
if(i != 'a' && i != 'e' && i != 'i' && i != 'o' && i != 'u') res += i;
}
return res;
}
let str = 'Twinkle, twinkle, little star';
print(remove_vowels(str));