5 条题解

  • 3
    @ 2024-2-10 18:52:49

    咳咳咳(说事专用咳嗽),来发正经mapmap解法

    #include <bits/stdc++.h>
    using namespace std;
    map<int, int> mp;
    int n, x;
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        cin >> n;
        for (int i = 1; i <= n; i++)
        {
            cin >> x;
            mp[x]++;
        }
        for (auto b : mp)
            cout << b.first << " " << b.second << endl;
        return 0;
    }
    
    • 2
      @ 2023-10-18 20:05:31
      #include <bits/stdc++.h>
      using namespace std;
      int main()
      {
          cout<<6<<" "<<2;
          cout<<endl<<8<<" "<<2;
          cout<<endl<<9<<" "<<1;
          cout<<endl<<10<<" "<<1;
          cout<<endl<<10000<<" "<<2;
          cout<<endl<<20000<<" "<<1;
          cout<<endl<<999999999<<" "<<1;
          return 0;
      }
      
      • -1
        @ 2023-1-21 21:21:39
        #include<bits/stdc++.h>
        using namespace std;
        /*
        思路:将数组排序,统计连续相同的数有几个
        1、遇到每个数都C++
        2、C++之后判断连续相同的数是否结束
        结束标准:到了最后一个 || 当前数 != 下一个数 
        */ 
        int n, a[1010],c = 0;
        int main()
        {
        	cin >> n; 
        	for (int i = 0; i < n; i++){
        		cin >> a[i];
        	}
        	//排序
        	sort(a,a+n);
        	//统计连续相同的数出现的次数
        	for(int i = 0; i < n; i++){
        		c++;//统计连续相同的数出现的次数
        		//判断连续相同的数是否结束
        		if(i == n - 1 || a[i] != a[i+1]){
        			//输出
        			cout << a[i] << " " << c << endl; 
        			//计数器清零 
        			c = 0;
        		} 
        	} 
        	return 0;
        }
        
        
        
        • -3
          @ 2021-12-28 13:46:26
          
          这里采用STL的map,map会自动给数据排序。
          map内部自建一颗红黑树(一 种非严格意义上的平衡二叉树),
          这颗树具有对数据自动排序的功能,
          所以在map内部所有的数据都是有序的。
          map把值与键关联在一起,可以使用键来查找词。
          例如:键可以表示学号,而值 可以对应姓名。
          代码:
          #include<bits/stdc++.h>
          using namespace std;
          map<int,int> m;
          int n,x; 
          int main()
          {
              ios::sync_with_stdio(false);
              cin.tie(0);	
          	cin>>n;
          	for(int i=1;i<=n;i++)
          	{
          		cin>>x;
          		m[x]++;
          	}
          	map<int,int>::iterator iter;//遍历时需要定义一个迭代器
          	for(iter=m.begin();iter!=m.end();++iter)
          	{
          		cout<<iter->first<<" "<<iter->second<<endl;//iter->first 值的是键,iter->second指的是值。
          	}
          	return 0;
          }
          • -6
            @ 2022-4-19 22:49:06

            严禁抄题解,发现后取消成绩

            • 1

            【基础】统计每个数出现的次数

            信息

            ID
            736
            时间
            1000ms
            内存
            128MiB
            难度
            4
            标签
            递交数
            73
            已通过
            32
            上传者