鱼C论坛

 找回密码
 立即注册
查看: 2021|回复: 2

[已解决]牛客[NOIP2006]明明的随机数

[复制链接]
发表于 2023-10-10 18:06:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int a[101],b[101];
  4. int main()
  5. {
  6.     int n,ans=0;
  7.     cin>>n;
  8.     for(int i=0;i<n;i++)
  9.     {
  10.         cin>>a[i];
  11.     }
  12.     for(int i=0;i<n;i++)
  13.     {
  14.         for(int j=i+1;j<n;j++)
  15.         {
  16.             if(a[i]!=a[j])
  17.             {
  18.                 b[i]=a[i];
  19.                 ans++;
  20.             }
  21.         }
  22.     }
  23.     sort(b,b+ans);
  24.     cout<<ans<<endl;
  25.     for(int i=0;i<n;i++)
  26.     {
  27.         cout<<b[i]<<" ";

  28.     }
  29.    
  30.    
  31.    
  32. }
复制代码

怎么修改代码,才能通过
最佳答案
2023-10-10 18:08:17
代码中存在一些问题,需要进行修改才能通过编译和逻辑上的正确性。下面是修改后的代码:
  1. ++
  2. #include <iostream>
  3. #include <algorithm>
  4. using namespace std;
  5. int main() {
  6.     int n;
  7.     cin>>n;
  8.     int a[101], b[101] = {0}; // 将b数组初始化为0
  9.     int ans = 0;
  10.     for(int i=0; i<n; i++) {
  11.         cin>>a[i];
  12.     }
  13.     for(int i=0; i<n; i++) {
  14.         bool flag = true; // 添加一个标记,表示是否已经存在相同的数
  15.         for(int j=0; j<i; j++) {
  16.             if(a[i] == a[j]) {
  17.                 flag = false;
  18.                 break;
  19.             }
  20.         }
  21.         if(flag) {
  22.             b[ans++] = a[i]; // 将不重复的数保存到b数组中
  23.         }
  24.     }
  25.     sort(b, b+ans);
  26.     cout<<ans<<endl;
  27.     for(int i=0; i<ans; i++) {
  28.         cout<<b[i]<<" ";
  29.     }
  30.     cout<<endl;
  31.     return 0;
  32. }
复制代码

修改内容如下:
1. 将 #include <bits/stdc++.h> 改为 #include <iostream>#include <algorithm>,只包含必要的头文件。
2. 将 using namespace std; 放在函数内部,避免全局使用。
3. 初始化 b 数组为0,以避免未初始化的元素带来的问题。
4. 添加一个标记变量 flag,用于判断是否存在重复的数。
5. 修改内层循环的起始条件为 j=0,遍历已经存储过的元素。
6. 将不重复的数保存到 b 数组中,同时计数 ans 自增。
7. 输出结果时循环的终止条件改为 i<ans,以避免输出无效的元素。

希望能对你有所帮助!如果还有其他问题,请随时提问。

球一个最佳答案谢谢啦!这对我非常重要!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-10-10 18:06:54 | 显示全部楼层
这道题目是要求统计一个数组中不同元素的个数,并输出这些不同元素,可以使用set来实现。以下是修改后的代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int a[101];
  4. set<int> s;
  5. int main()
  6. {
  7.     int n;
  8.     cin >> n;
  9.     for(int i = 0; i < n; i++)
  10.     {
  11.         cin >> a[ i];
  12.         s.insert(a[ i]);
  13.     }
  14.     cout << s.size() << endl;
  15.     for(auto it = s.begin(); it != s.end(); it++)
  16.     {
  17.         cout << *it << " ";
  18.     }
  19.     return 0;
  20. }
复制代码


首先将数组中的元素插入到set中,由于set中不允许重复元素,所以最后set的大小就是不同元素的个数。然后遍历set输出不同元素即可。

注意:头文件可以只包含 <set> ,不需要包含 <bits/stdc++.h> 。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-10 18:08:17 | 显示全部楼层    本楼为最佳答案   
代码中存在一些问题,需要进行修改才能通过编译和逻辑上的正确性。下面是修改后的代码:
  1. ++
  2. #include <iostream>
  3. #include <algorithm>
  4. using namespace std;
  5. int main() {
  6.     int n;
  7.     cin>>n;
  8.     int a[101], b[101] = {0}; // 将b数组初始化为0
  9.     int ans = 0;
  10.     for(int i=0; i<n; i++) {
  11.         cin>>a[i];
  12.     }
  13.     for(int i=0; i<n; i++) {
  14.         bool flag = true; // 添加一个标记,表示是否已经存在相同的数
  15.         for(int j=0; j<i; j++) {
  16.             if(a[i] == a[j]) {
  17.                 flag = false;
  18.                 break;
  19.             }
  20.         }
  21.         if(flag) {
  22.             b[ans++] = a[i]; // 将不重复的数保存到b数组中
  23.         }
  24.     }
  25.     sort(b, b+ans);
  26.     cout<<ans<<endl;
  27.     for(int i=0; i<ans; i++) {
  28.         cout<<b[i]<<" ";
  29.     }
  30.     cout<<endl;
  31.     return 0;
  32. }
复制代码

修改内容如下:
1. 将 #include <bits/stdc++.h> 改为 #include <iostream>#include <algorithm>,只包含必要的头文件。
2. 将 using namespace std; 放在函数内部,避免全局使用。
3. 初始化 b 数组为0,以避免未初始化的元素带来的问题。
4. 添加一个标记变量 flag,用于判断是否存在重复的数。
5. 修改内层循环的起始条件为 j=0,遍历已经存储过的元素。
6. 将不重复的数保存到 b 数组中,同时计数 ans 自增。
7. 输出结果时循环的终止条件改为 i<ans,以避免输出无效的元素。

希望能对你有所帮助!如果还有其他问题,请随时提问。

球一个最佳答案谢谢啦!这对我非常重要!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-9 17:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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