查看: 2465| 回复: 2
跳转到指定楼层
上一主题 下一主题
收起左侧

[其他] 求助帖,求问stanford公开课Algorithms: Design and Analysis, Part I (Week 1)的问题

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
题目:. 1point 3acres
Question 1
Your 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;
  • }
-baidu 1point3acres
[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;
  • }. 1point 3 acres

[color=rgb(73, 123, 137) !important]复制代码

蟹蟹各位啦

. 1point3acres.com

上一篇:这里有没有在Washington, D.C. 工作的前辈
下一篇:学费的考虑
🔗
 楼主| yangze0930 2015-7-8 18:29:47 | 只看该作者
全局:
重新贴一下代码
vector数据结构写的:
  1. #include<iostream>
  2. #include<vector>
  3. #include<fstream>-baidu 1point3acres

  4. using namespace std;

  5. long long countInversion(vector<int> & vec). 1point 3 acres
  6. {
  7.         int n = vec.size();. Waral dи,
  8.         if (n != 1)
  9.         {
  10.                 vector<int> b, c;
  11.                 for (int i = 0; i < n / 2; i++)
  12.                 {
  13.                         b.push_back(vec[i]);
  14.                 }
  15.                 for (int i = 0; i < n - n / 2; i++). Waral dи,
  16.                 {
  17.                         c.push_back(vec[n / 2 + i]);
  18.                 }. Waral dи,
  19.                 long long num1 = countInversion(b);. .и
  20.                 long long num2 = countInversion(c);
  21.                 long long num3 = 0;
  22.                 vec.clear();. Χ
  23.                 int i = 0, j = 0;
  24.                 for (int k = 0; k < n; k++)
  25.                 {
  26.                         if (i < n / 2 && j<n - n / 2)
  27.                         {.
  28.                                 if (b[i] <= c[j])
  29.                                 {
  30.                                         vec.push_back(b[i]);. 1point3acres
  31.                                         i++;
  32.                                 }
    . .и
  33.                                 else if (b[i]>c[j])
  34.                                 {
    .--
  35.                                         vec.push_back(c[i]);-baidu 1point3acres
  36.                                         num3 = num3 + n / 2 - i;.google  и
  37.                                         j++;
  38.                                 }. check 1point3acres for more.
  39.                         }
  40.                         else if (i >= n / 2)
  41.                         {
  42.                                 vec.push_back(c[j]);
  43.                                 j++;
  44.                         }
  45.                         else if (j >= n - n / 2)
  46.                         {
  47.                                 vec.push_back(b[i]);
  48.                                 i++;
  49.                         }
  50.                 }. 1point3acres.com
  51.                 long long ans = num1 + num2 + num3;
  52.                 return ans;
  53.         }. 1point 3acres
  54.         else if (n == 1)
  55.                 return 0;. .и
  56. }


  57. int main(void). ----
  58. {. 1point3acres
  59.         vector<int> a;
  60.         int temp;
  61.         ifstream fin("D:\\IntegerArray.txt");
  62.         if (!fin)
  63.         {. 1point 3 acres
  64.                 cout << "can't open file";. 1point 3acres
  65.                 return 1;
  66.         }
  67.         for (int i = 0; i < 100000; i++)
  68.         {
  69.                 fin >> temp;
  70.                 a.push_back(temp);
  71.         }
  72.         long long ans = countInversion(a);
  73.         cout << a.size()<<'\t'<<ans;-baidu 1point3acres
  74.         getchar();
  75.         getchar();
  76.         return 0;
  77. }
复制代码
动态数组写的代码:
  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;

  4. long long inven(int a[],int n). .и
  5. {
  6.         if (n != 1)
  7.         {
  8.                 int *b=new int[n/2];
  9.                 int *c=new int[n-n/2];
  10.                 for (int i = 0; i < n / 2; i++)
  11.                 {
  12.                         b[i] = a[i];
  13.                 }
  14.                 for (int i = 0; i < n - n / 2; i++)
  15.                 {
  16.                         c[i] = a[n / 2 + i];
  17.                 }
  18.                 long long num1 = inven(b,n/2);. check 1point3acres for more.
  19.                 long long num2 = inven(c,n-n/2);
  20.                 long long num3 = 0;
  21.                 int i = 0, j = 0;
  22.                 for (int k = 0; k < n; k++)
  23.                 {
  24.                         if (i < n / 2 && j<n-n/2)
  25.                         {
  26.                                 if (b[i] <= c[j])
  27.                                 {
  28.                                         a[k] = b[i];
  29.                                         i++;
  30.                                 }
  31.                                 else if (b[i]>c[j])
  32.                                 {. 1point 3acres
  33.                                         a[k] = c[j];
  34.                                         num3 = num3 + n / 2 - i;
  35.                                         j++;
  36.                                 }
  37.                         }
  38.                         else if (i >= n / 2)
    . From 1point 3acres bbs
  39.                         {. 1point 3acres
  40.                                 a[k] = c[j];
  41.                                 j++;
  42.                         }
  43.                         else if (j >= n - n / 2)
  44.                         {. 1point 3acres
  45.                                 a[k] = b[i];. 1point3acres
  46.                                 i++;
  47.                         }
  48.                 }
  49.                 delete b;
  50.                 delete c;. check 1point3acres for more.
  51.                 long long ans = num1 + num2 + num3;
  52.                 return ans;
  53.         }
  54.         else if (n == 1)
  55.                 return 0;
  56. }

  57. int main(void). Χ
  58. {
  59.         ifstream fin("D:\\IntegerArray.txt");
  60.         if (!fin)
  61.         {
  62.                 cout << "can't open file";
  63.                 return 1;
  64.         }
  65.         int a[100000];
  66.         for (int i = 0; i < 100000; i++)
  67.         {
  68.                 fin >> a[i];
  69.         }
  70.         long long ans = inven(a,100000);
  71.         cout << ans;
  72.         getchar();
  73.         getchar();
  74.         return 0;
  75. }
复制代码
回复

使用道具 举报

🔗
 楼主| yangze0930 2015-7-8 18:30:16 | 只看该作者
全局:
蟹蟹大家啦!!!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表