鱼C论坛

 找回密码
 立即注册
查看: 2029|回复: 8

朋友们!帮帮忙吧!呜呜呜呜

[复制链接]
发表于 2020-11-17 17:42:17 From FishC Mobile | 显示全部楼层 |阅读模式

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

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

x
1,将一个数组中的重复元素保留一个其他清零   2,输入20个0~9之间的整数,统计每个数在输入数列中出现的次数
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-17 18:52:09 | 显示全部楼层
这是两个题目嘛?
1. 的意思是不是去除数组中的重复数字?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-17 19:02:37 From FishC Mobile | 显示全部楼层
shooan 发表于 2020-11-17 18:52
这是两个题目嘛?
1. 的意思是不是去除数组中的重复数字?

是两个题目
1的意思就是去除数组中的重复数字
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-17 19:22:35 | 显示全部楼层
毛大杀手 发表于 2020-11-17 19:02
是两个题目
1的意思就是去除数组中的重复数字

语言是C还是C++呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-17 19:28:06 From FishC Mobile | 显示全部楼层
shooan 发表于 2020-11-17 19:22
语言是C还是C++呢

C++
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-17 19:37:40 | 显示全部楼层
第一题

                               
登录/注册后可看大图


  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>

  4. using namespace std;

  5. vector<int> removeDuplicate(vector<int>& nums) {
  6.         vector<int> res;
  7.         for (int n : nums) {
  8.                 if (find(res.begin(), res.end(), n) == res.end())
  9.                 {
  10.                         res.push_back(n);
  11.                 }
  12.         }
  13.        
  14.         return res;
  15. }


  16. int main()
  17. {
  18.         vector<int> nums {1,2,3,5,6,4,1,2,3};
  19.         vector<int> res = removeDuplicate(nums);
  20.        
  21.        
  22.         for (auto& v : res) {
  23.                 cout << v << endl;
  24.         }
  25.        
  26.         return 0;
  27. }
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-17 19:41:48 From FishC Mobile | 显示全部楼层
shooan 发表于 2020-11-17 19:37
第一题

万分感谢!!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-17 19:42:59 | 显示全部楼层
第二题

                               
登录/注册后可看大图


  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>

  4. using namespace std;

  5. vector<int> count(vector<int>& nums) {
  6.         vector<int> count(10);        // 存储 0 到 9 是个数字出现的次数
  7.         for (int n : nums) {
  8.                 count[n]++;
  9.         }
  10.         return count;
  11. }


  12. int main()
  13. {
  14.         vector<int> nums;
  15.        
  16.         for (int i = 0; i < 20; i++)
  17.         {
  18.                 int v;
  19.                 cin >> v;
  20.                 nums.push_back(v);
  21.         }
  22.        
  23.         vector<int> res = count(nums);
  24.        
  25.         int k = 0;
  26.         for (auto& v : res) {
  27.                 if (v != 0)
  28.                 {
  29.                         cout << k  << ": " << v  << endl;
  30.                 }
  31.                 k++;
  32.         }
  33.        
  34.         return 0;
  35. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-17 20:38:12 | 显示全部楼层
本帖最后由 jackz007 于 2020-11-17 20:39 编辑

第1题
  1. #include <stdio.h>

  2. void show(int d[] , int n)
  3. {
  4.         int i                                                                 ;
  5.         printf("%d" , d[0])                                                   ;
  6.         for(i = 1 ; i < n ; i ++) printf("\t%d" , d[i])                       ;
  7.         printf("\n")                                                          ;
  8. }

  9. void dedup(int d[] , int m)
  10. {
  11.         int i , j , k , n                                                     ;
  12.         for(n = m , i = 0 ; i < n - 1 ; i ++) {
  13.                 for(j = n - 1 ; j > i ; j --) {
  14.                         if(d[j] == d[i]) {
  15.                                 for(k = j + 1 ; k < n ; k ++) d[k - 1] = d[k] ;
  16.                                 n --                                          ;
  17.                         }
  18.                 }
  19.         }
  20.         for(; n < m ;) d[n ++] = 0                                            ;
  21. }

  22. int main(void)
  23. {
  24.         int d[40] = {18 , 22 , 55 , 53 , 12 , 18 , 89 , 98 , 77 , 33 ,\
  25.                       52 , 73 , 77 , 26 , 13 , 88 , 17 , 19 , 53 , 18 ,\
  26.                       18 , 22 , 55 , 53 , 12 , 18 , 89 , 98 , 77 , 33 ,\
  27.                       52 , 73 , 77 , 26 , 13 , 88 , 17 , 19 , 53 , 18}  ;

  28.         show(d , 40)                                                    ;
  29.         dedup(d , 40)                                                   ;
  30.         printf("\n")                                                    ;
  31.         show(d , 40)                                                    ;
  32. }
复制代码

        编译、运行实况
  1. D:\00.Excise\C>g++ -o x x.c

  2. D:\00.Excise\C>x
  3. 18      22      55      53      12      18      89      98      77      33
  4. 52      73      77      26      13      88      17      19      53      18
  5. 18      22      55      53      12      18      89      98      77      33
  6. 52      73      77      26      13      88      17      19      53      18

  7. 18      22      55      53      12      89      98      77      33      52
  8. 73      26      13      88      17      19      0       0       0       0
  9. 0       0       0       0       0       0       0       0       0       0
  10. 0       0       0       0       0       0       0       0       0       0

  11. D:\00.Excise\C>
复制代码

        第2题
  1. #include <stdio.h>

  2. int main(void)
  3. {
  4.         int d[20] , e[10] = {0} , f , k , m                                      ;
  5.         printf("请输入20个值为 0 - 9 的整数 : ")                                 ;
  6.         for(k = 0 ; k < 20 ; k ++) scanf("%d" , & d[k])                          ;
  7.         for(f = 1 , k = 0 ; k < 20 && f ; k ++) {
  8.                 if(d[k] >= 0 && d[k] <= 9) {
  9.                         e[d[k]] ++                                               ;
  10.                 } else {
  11.                         printf("数值超限!")                                     ;
  12.                         f = 0                                                    ;
  13.                 }
  14.         }
  15.         if(f) for(k = 0 ; k < 10 ; k ++) if(e[k]) printf("%d : %d\n" , k , e[k]) ;
  16. }
复制代码

        编译、运行实况
  1. D:\00.Excise\C>g++ -o x1 x1.c

  2. D:\00.Excise\C>x1
  3. 请输入20个值为 0 - 9 的整数 : 1 5 7 8 9 2 3 4 5 4 0 2 5 6 1 3 2 4 8 9
  4. 0 : 1
  5. 1 : 2
  6. 2 : 3
  7. 3 : 2
  8. 4 : 3
  9. 5 : 3
  10. 6 : 1
  11. 7 : 1
  12. 8 : 2
  13. 9 : 2

  14. D:\00.Excise\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-5 21:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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