注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
题目:
Question 1Your task is to compute the number of inversions in the file given, where the ith row of the file indicates the ith entry of an array.
Because of the large size of this array, you should implement the fast divide-and-conquer algorithm covered in the video lectures. The numeric answer for the given input file should be typed in the space below.
求问第一周编程题,第一次使用vector写代码。为什么算出来的结果和用动态数组写的代码算出来的不一样 使用vector写的程序代码,算出来是2584786980.- #include<iostream>
- #include<vector>
- #include<fstream>
- using namespace std;
- long long countInversion(vector<int> & vec)
- {
- int n = vec.size();
- if (n != 1)
- {
- vector<int> b, c;
- for (int i = 0; i < n / 2; i++)
- {
- b.push_back(vec[i]);
- }
- for (int i = 0; i < n - n / 2; i++)
- {
- c.push_back(vec[n / 2 + i]);
- }
- long long num1 = countInversion(b);
- long long num2 = countInversion(c);
- long long num3 = 0;
- vec.clear();
- int i = 0, j = 0;
- for (int k = 0; k < n; k++)
- {
- if (i < n / 2 && j<n - n / 2)
- {
- if (b[i] <= c[j])
- {
- vec.push_back(b[i]);
- i++;
- }
- else if (b[i]>c[j])
- {
- vec.push_back(c[i]);
- num3 = num3 + n / 2 - i;
- j++;
- }
- }
- else if (i >= n / 2)
- {
- vec.push_back(c[j]);
- j++;
- }
- else if (j >= n - n / 2)
- {
- vec.push_back(b[i]);
- i++;
- }
- }
- long long ans = num1 + num2 + num3;
- return ans;
- }
- else if (n == 1)
- return 0;
- }
- int main(void)
- {
- vector<int> a;
- int temp;
- ifstream fin("D:\\IntegerArray.txt");
- if (!fin)
- {
- cout << "can't open file";
- return 1;
- }
- for (int i = 0; i < 100000; i++)
- {
- fin >> temp;
- a.push_back(temp);
- }
- long long ans = countInversion(a);
- cout << a.size()<<'\t'<<ans;
- getchar();
- getchar();
- return 0;
- }. ----
[color=rgb(73, 123, 137) !important]复制代码
使用动态数组算出来的结果是2407905288- #include<iostream>
- #include<fstream>
- using namespace std;
- long long inven(int a[],int n)
- {
- if (n != 1)
- {
- int *b=new int[n/2];
- int *c=new int[n-n/2];
- for (int i = 0; i < n / 2; i++)
- {
- b[i] = a[i];
- }
- for (int i = 0; i < n - n / 2; i++)
- {
- c[i] = a[n / 2 + i];
- }
- long long num1 = inven(b,n/2);
- long long num2 = inven(c,n-n/2);
- long long num3 = 0;
- int i = 0, j = 0;
- for (int k = 0; k < n; k++)
- {
- if (i < n / 2 && j<n-n/2)
- {
- if (b[i] <= c[j])
- {
- a[k] = b[i];
- i++;
- }
- else if (b[i]>c[j])
- {
- a[k] = c[j];
- num3 = num3 + n / 2 - i;
- j++;
- }
- }
- else if (i >= n / 2)
- {
- a[k] = c[j];
- j++;
- }
- else if (j >= n - n / 2)
- {
- a[k] = b[i];
- i++;
- }
- }
- delete b;
- delete c;
- long long ans = num1 + num2 + num3;
- return ans;
- }
- else if (n == 1)
- return 0;
- }
- int main(void)
- {
- ifstream fin("D:\\IntegerArray.txt");
- if (!fin)
- {
- cout << "can't open file";
- return 1;
- }
- int a[100000];
- for (int i = 0; i < 100000; i++)
- {
- fin >> a[i];
- }
- long long ans = inven(a,100000);
- cout << ans;
- getchar();
- getchar();
- return 0;
- }.google и
[color=rgb(73, 123, 137) !important]复制代码
蟹蟹各位啦
-baidu 1point3acres
|