6 条题解

  • 4
    @ 2022-8-8 21:27:31

    这是我唯一一个自信到自测都不用自测就递交并且AC的程序QwQ

    #include<bits/stdc++.h>
    using namespace std;
    short x,n,cnt[101],maxn,ans;
    int main(){
        cin>>n;
        for(short i=1;i<=n;i++){
            cin>>x;
            cnt[x]++;
            if(cnt[x]>maxn){
                maxn=cnt[x];
                ans=x;
            }
        }
        cout<<ans;
        return 0;
    }
    
    • 2
      @ 2022-3-10 0:11:39

      引言

      一道平平无奇的题目,(我连算法标签都不知道该写什么),但如果老老实实不懂得用函数去写还是挺麻烦的(我FOI比赛的时候有一道近似题,当时我还没学会count()函数,然后在那改了老半天,最后写了个暴力,tle了一个点,挺纳闷的)


      分析

      简单看下这道题,输入两行,第一行n,第二行n个数,求出现次数最多的数。

      这道题要用vector做。 我们C++ Level10 是不是有一课叫《vector的相关函数》?是不是讲了两个vector的函数resize和assign?但我们今天要用到函数是另一个没教过的函数:count。

      count适用于vector和string,计数对象中某个东西出现的次数,在vector里这么用:

      ...
      vector<int> v;
      
      int main()
      {
          ... //输入部分过掉
          int x = count(v.begin(), v.end(), 1); //寻找v中数字1出现的次数
          cout << x << endl;
      

      我们还要用到另一个函数:unique。去重,适用于数组。一般和sort搭配使用。

      ...
      int main()
      {
          int a[5] = {1, 2, 2, 3, 1};
          sort(a, a + 5);
          int x = unique(a, a + 5) - a; //获得最后一个不重复数的下标,因为unique只是把重复的数字放到数组末尾,并没有删除
          for (int i = 0; i < x; i++)
          {
              cout << a[i] << " ";
          }
      
          return 0;
      

      这道题的核心在于:定义一个数组a和一个int型vector,v。a用来储存不重复的数,不用太大,因为本题给定的数据范围很小,n最多100。去重之后,遍历不重复的部分,计数出目前不重元素在向量v中的个数,与maxn比较,结果用temp记录。如果有变化,将答案ans更改为a[i]。maxn也改为目前较大值。最后输出ans即可。 代码如下(AC):

      #include <bits/stdc++.h>
      
      using namespace std;
      
      vector<int> v;
      
      int n, maxn = -1;
      int a[105];
      int ans, temp = -1;
      
      int main()
      {
          cin >> n;
      
          for (int i = 0; i < n; i++)
          {
              cin >> a[i];
              v.push_back(a[i]);
          }
          
          sort(a, a + n);
          int x = unique(a, a + n) - a;
      
          for (int i = 0; i < x; i++)
          {
              int b = count(v.begin(), v.end(), a[i]);
      
              temp = max(maxn, b);
              
              if (temp != maxn)
              {
                  ans = a[i];
              }
      
              maxn = max(maxn, b);
          }
      
          cout << ans << endl;    
      
          return 0;
      }
      

      后记:这道题是我想复杂了,完全可以直接遍历找到答案。这种做法仅限这种数据范围很小的题目,代码如下(AC):

      #include <bits/stdc++.h>
      
      using namespace std;
      
      int n, a[105], x, maxn = -1, temp = -1, ans;
      
      int main()
      {
          cin >> n;
      
          for (int i = 1; i <= n; i++)
          {
              cin >> x;
              a[x]++;
          }
      
          for (int i = 1; i <= n; i++)
          {
              temp = max(maxn, a[i]);
      
              if (temp != maxn)
              {
                  ans = i;
              }
      
              maxn = max(maxn, a[i]);
          }
      
          cout << ans << endl;
      
          return 0;
      }
      

      补充

      emmm,如果把这道题包装一下,就可以写成这样子(FOI2021 第二题 镇长选举):

      题目描述

      S小镇选举镇长,有n个人投票,没有弃权。当地法律规定,只要某个竞选人支持票数达到一半以上,即当选镇长。请编程得到最终胜选人编号。注意竞选人编号不一定是连续的。数据保证存在答案。

      输入格式

      输入两行,第一行一个正整数n,代表有n个人投票,第二行n个正整数,代表竞选人编号m。 数据范围:n ≤ 500000, m ≤ 10 ^ 9

      输出格式

      输出一行,最终胜选人编号。

      输入输出样例

      输入#1

      10
      1 2 5 5 5 6 8 5 5 5
      

      输出#1

      5
      

      这道补充题是不是和这道题很相似?但是这道FOI原题它的数据范围更大,留给各位思考~

      • 1
        @ 2023-9-24 10:25:50
        #include <iostream>
        using namespace std;
        int n, x, a[1005], maxn = -1, pos;
        int main()
        {
            ios::sync_with_stdio(false);
            cin.tie(0);
            cout.tie(0);
            cin >> n;
            for (int i = 1; i <= n; i++)
            {
                cin >> x;
                a[x]++;
            }
            for (int i = 1; i <= n; i++)
            {
                if (a[i] > maxn)
                {
                    maxn = a[i];
                    pos = i;
                }
            }
            cout << pos;
            return 0;
        }
        
        • 0
          @ 2022-8-23 18:26:42
          //计数排序
          #include <bits/stdc++.h>
          using namespace std;
          int n,a[105],maxx,x,num;
          int main()
          {
              ios::sync_with_stdio(false);
              cin.tie(0);
              cout.tie(0);//输入输出加速(建议记住)
              cin >> n;
              for (int i = 1;i <= n;i++)
              {
                  cin >> x;
                  a[x]++;
              }
              for (int i = 1;i <= 100;i++)
              {
                  if (a[i] > maxx)
                  {
                      maxx = a[i];
                      num = i;
                  }
              }
              cout << num;
              return 0;
          }
          

          第一次写题解😄 ,希望对大家有用

          • 0
            @ 2022-5-3 13:55:10
            #include <bits/stdc++.h>//排序算法
            using namespace std;
            int n , a[10000] , maxn = -1 , ans;
            int main()
            {
            	cin >> n;
            	for(int i = 1;i <= n;i++)
            	{
            		int x;
            		cin >> x;
            		a[x]++;
            	}
            	for(int i = 1;i <= n;i++)
            	{
            		if(a[i] > maxn)
            		{
            			maxn = a[i];
            			ans = i;
            		}
            	}
            	cout << ans;
            	return 0;
             } 
            
            • 0
              @ 2022-4-24 16:43:42

              写题解请注意

              鼓励大家写题解,但注意题解格式。

              题解一定要有思路解析或代码注释,能否让别人理解你的思路

              也是你的能力的检验,不要只放无意义的代码给大家复制,那就失去了做题的初心。

              给代码两端加上这个会舒服一些

              ```cpp

              你的代码

              ```

              </span>

              这个点在键盘的左上角tab上面那个键,注意切换输入法

              #include<iostream>
              using namespace std;
              int main()
              {
                  int n;
                  cin>>n;//这是一个注释
                  return 0;
              } 
              

              请注意严禁抄袭题解,写题解不要只放代码,需加上你的思路或代码注释。

              抄袭题解一经发现直接取消成绩。

              题解被删除的可能

              1. 代码不符合格式规范
              2. 没有思路讲解或者没有注释,
              3. 无意义的题解

              大家携手共同维护一个良好的编程环境,如果一经发现,多次作乱。可能会被管理员拉黑,请注意,一旦拉黑即失去登陆资格。

              • 1

              信息

              ID
              1255
              时间
              1000ms
              内存
              256MiB
              难度
              7
              标签
              递交数
              620
              已通过
              133
              上传者