Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- // https://en.wikipedia.org/wiki/Rostov_Oblast
- const char* text = R"(Residents identified
- themselves as belonging to 157 different
- ethnic groups, including 27 of more than 2,000
- persons each. The largest ethnicities are[14]
- the 3,795,607 Russians (90.3%);
- the 110,727 Armenians (2.6%) and the 77,802
- Ukrainians (1.9%). Other important groups
- are the 35,902 Turks (0.9%); 16,493 Belarusians (0.4%);
- 13,948 Tatars (0.3%); 17,961 Azerbaijanis (0.4%); 11,449
- Chechens (0.3%); 16,657 Romani (0.4%); 11,597 Koreans (0.3%);
- 8,296 Georgians (0.2%), and 2,040 Assyrians (.05%).
- There were also 76,498 people (1.8%) belonging to
- other ethno-cultural groupings. 76,735 people were
- registered from administrative databases, and could
- not declare an ethnicity. It is estimated that the proportion
- of ethnicities in this group is the same as that of the declared
- group.[21])";
- void my_copy(char* des, int n, const char* src) {
- for (int k = 0; k < n; k++)
- des[k] = src[k];
- }
- int parse(const char* text, double*& out) {
- int num = 0;
- char* buff = new char[10];
- int len = strlen(text);
- cout << "len: " << len << endl;
- int bg = 0;
- int ed = 0;
- for (int k = 0; k < len; k++) {
- if (text[k] == '(')
- bg = k+1;
- if (text[k] == ')') {
- ed = k-1;
- my_copy(buff, ed-bg, &(text[bg]));
- buff[ed - bg] = '\0';
- out[num++] = atof(buff);
- }
- }
- delete[] buff;
- return num;
- }
- void task1() {
- char buff[10];
- auto out = new double[100];
- int num = parse(text, out);
- for (int k = 0; k < num; k++)
- cout << out[k] << ", ";
- cout << endl;
- delete[] out;
- /*
- for (int k = 0, n = 10; ((n > 0) && (k < 10)); k++, n--)
- cout << k << " " << n << endl;
- int data[5][5]{};
- for (int r = 0; r < 5; r++) {
- data[2][r] = 15 + r;
- data[4][r] = 15 - r;
- }
- for (int r = 0; r < 5; r++) {
- cout << data[2][r] - data[4][r];
- }
- */
- }
- int binary_search_recursion(int* arr, int left, int right, int target) {
- if (left <= right) {
- int midle = left + (right - left) / 2;
- if (arr[midle] == target) {
- return midle;
- }
- else if (arr[midle] > target) {
- binary_search_recursion(arr,left, midle-1, target);
- }
- else {
- binary_search_recursion(arr,midle+1, right, target);
- }
- } else
- return -1;
- }
- int binary_search(int* arr, int left, int right, int target) {
- while (left <= right) {
- int midle = left + (right - left) / 2;
- if (arr[midle] == target) {
- return midle;
- }
- else if (arr[midle] > target) {
- right = midle - 1;
- }
- else {
- left = midle + 1;
- }
- }
- return -1;
- }
- void test_binary_search() {
- int arr[]{1,2,3,4,5,6,7,8,9};
- cout << "7 at " << binary_search(arr,0,size(arr),7) << endl;
- }
- int main() {
- test_binary_search();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment