10 条题解

  • 2
    @ 2023-1-10 10:56:55

    从小到大排序(用sort),倒序输出就行了。 AC代码来喽!

    #include <iostream>
    #include <algorithm>
    using namespace std;
    int n = 10;
    int a[11];
    int main()
    {
        for (int i = 1; i <= 10; i++)
        {
            cin >> a[i];
        }
        sort (a + 1, a + n + 1);
        for (int i = 10; i >= 1; i--)
        {
            cout << a[i] << " ";
        }
        return 0;
    }
    
    • 1
      @ 2024-6-2 13:34:45

      史上最长AC代码来袭!!!

      #include <bits/stdc++.h>
      using std::cin;
      using std::cout;
      using std::endl;
      using std::sort;
      using std::vector;
      using std::priority_queue;
      using std::greater;
      using std::less;
      using std::set;
      using std::deque;
      using std::mt19937_64;
      using std::uniform_int_distribution;
      mt19937_64 rdll(time(0)+114514);
      long long Randomll(long long l, long long r)
      {
          return uniform_int_distribution<long long>(l, r)(rdll);
      }
      int a[15];
      namespace sub1
      {
          bool cmp(int a, int b)
          {
              return a > b;
          }
          void main()
          {
              sort (a + 1, a + 11, cmp);
              for (int i = 1; i <= 10; i++)
                  cout << a[i] << " ";
              cout << endl;
          }
      }
      namespace sub2
      {
          vector<int> vec;
          void main()
          {
              for (int i = 1; i <= 10; i++)
                  vec.push_back(a[i]);
              sort (vec.rbegin(), vec.rend());
              for (auto i : vec)
                  cout << i << " ";
              cout << endl;
          }
      }
      namespace sub3
      {
          priority_queue<int, vector<int>, less<int> > pq;
          void main()
          {
              for (int i = 1; i <= 10; i++)
                  pq.push(a[i]);
              while (!pq.empty())
              {
                  cout << pq.top() << " ";
                  pq.pop();
              }
              cout << endl;
          }
      }
      namespace sub4
      {
          set<int> s;
          void main()
          {
              for (int i = 1; i <= 10; i++)
                  s.insert(a[i]);
              for (set<int>::iterator it = --s.end(); it != --s.begin(); it--)
                  cout << *it << " ";
              cout << endl;
          }
      }
      namespace sub5
      {
          deque<int> dq;
          void main()
          {
              for (int i = 1; i <= 10; i++)
                  dq.push_front(a[i]);
              sort (dq.begin(), dq.end());
              reverse(dq.begin(), dq.end());
              for (auto i : dq)
                  cout << i << " ";
              cout << endl;
          }
      }
      namespace sub6
      {
          vector<int> vec;
          void main()
          {
              for (int i = 1; i <= 10; i++)
                  vec.push_back(a[i]);
              sort (vec.begin(), vec.end());
              reverse(vec.begin(), vec.end());
              for (auto i : vec)
                  cout << i << " ";
              cout << endl;
          }
      }
      namespace sub7
      {
          deque<int> dq;
          void main()
          {
              for (int i = 1; i <= 10; i++)
                  dq.push_front(a[i]);
              sort (dq.rbegin(), dq.rend());
              for (auto i : dq)
                  cout << i << " ";
              cout << endl;
          }
      }
      int main()
      {
          for (int i = 1; i <= 10; i++)
              cin >> a[i];
          long long x = Randomll(1, 10000000000) % 7;
          if (x == 0)
              sub1::main();
          else if (x == 1)
              sub2::main();
          else if (x == 2)
              sub3::main();
          else if (x == 3)
              sub4::main();
          else if (x == 4)
              sub5::main();
          else if (x == 5)
              sub6::main();
          else
              sub7::main();
          return 0;
      }
      
    • -1
      @ 2024-6-2 15:59:49

      一般:

      #include <iostream>
      #include <algorithm>//没有这个,sort函数就运行不了了(准确的说是编辑错误)
      using namespace std;
      int main()//头文件
      {
          int a[11];
          for (int i=1;i<=10;i++)
          {
              cin >> a[i];
          }
          sort(a+2,a+10+1);
          for (int i=10;i>=1;i--)//sort是从小到大排列,所以需要倒序输出
          {
              cout << a[i] << ' ';
          }
          return 0;//结尾
      }
      

      老六:

      #include <iostream>
      using namespace std;
      int main()//头文件
      {
          cout << 11 << ' ' << 10 << ' ' << 9 << ' ' << 8 << ' ' << 7 << ' ' << 6 << ' ' << 5 << ' ' << 4 << ' ' << 3 << ' ' << 2;//作者一步一步试出来的
          return 0;//结尾
      }
      

      点个赞吧!

      • -1
        @ 2022-10-16 12:04:18

        这道题目吗,有三种解法,可以任选

        (在此感谢@hetao815606指出题解中的错误,现已更正👍 )

        1.冒泡排序

        上代码!

        #include <iostream>
        using namespace std;
        int a[11];
        int main()
        {
            for(int i = 1;i <= 10;i++)
            {
                cin >> a[i];
            }
            for(int i = 1;i <= 10 * 9 / 2;i++)
            {
                for(int i = 1;i <= 9;i++)
                {
                    if(a[i] < a[i + 1])
                    {
                        int t = a[i];
                        a[i] = a[i + 1];
                        a[i + 1] = t;
                    }
                } 
            }
          
            for(int i = 1;i <= 10;i++)
            {
                cout << a[i] << " ";
            }
            return 0;
        }
        
        

        2.选择排序

        上代码!

        #include <iostream>
        using namespace std;
        int a[11];
        int main()
        {
            for(int i = 1;i <= 10;i++)
            {
                cin >> a[i];
            }
            for(int i = 1;i <= 10 - 1;i++)
            {
                for(int j = i + 1;j <= 10;j++)
                {
                    if(a[i] < a[j])
                    {
                        int t = a[i];
                        a[i] = a[j];
                        a[j] = t;
                    }
                }
            }
            for(int i = 1;i <= 10;i++)
            {
                cout << a[i] << " ";
            }
            return 0;
        }
        
        

        3.计数排序

        上代码!

        #include <iostream>
        using namespace std;
        int a[11];
        int main()
        {
            int x = 0;
            for(int i = 1;i <= 10;i++)
            {
                cin >> x;
                a[x]++;
            }
            for(int i = 100;i >= 1;i--)
            {
                for(int j = 1;j <= a[i];j++)
                {
                    cout << i << " ";
                }   
            }
            return 0;
        }
        
        

        三种任选吧😄

        hetao3097453

        • -2
          @ 2024-4-6 12:17:44

          日常sort排序

          先用sort按照从小到大的顺序排好,再把他们倒序输出,出来的就是从大到小的啦

          已AC,请放心享用美食
          #include <bits/stdc++.h>
          using namespace std;
          int main()
          {
              int a[15];
              for (int i = 1; i <= 10; i++)
              {
                  cin >> a[i];
              }
              sort(a + 1, a + 11);
              for (int i = 10; i >= 1; i--)
              {
                  cout << a[i] << " ";
              }
              return 0;
          }
          

          本人会sort,但连全排列都不会,特别丢人

          即使如此,还是给个赞吧,鞑郝人(古代蒙古国一支游牧民族,和鞑靼人习性相近),呸!是大好人

          • -2
            @ 2024-2-16 14:14:30

            这道题可以使用sort函数实现,sort函数的格式:

            1. sort(first,last);[first,last)区间从小到大排序。

            2. sort(flrst,last,cmp);[first,last)区间按照cmp函数的方法排序,注意:cmp函数必须为有两个参数并且返回值为bool类型的函数

              AC代码:

            #include <bits/stdc++.h>
            using namespace std;
            int a[11];
            bool cmp(int a,int b)
            {
                return a > b;
            }
            int main()
            {
                for (int i = 1;i <= 10;i++)
                {
                    cin >> a[i];
                }
                sort(a + 1,a + 11,cmp);
                for (int i = 1;i <= 10;i++)
                {
                    cout << a[i] << " ";
                }
                return 0;
            }
            
            • -2
              @ 2023-4-14 22:31:56

              理论上来说,应该是可以直接用max和min函数硬算输出的,但是数组不香么

              #include<bits/stdc++.h>
              using namespace std;
              int main()
              {
                  int a[11];
                  for (int i=1;i<=10;i++)
                      cin>>a[i];
                  sort(a+1,a+11);
                  for (int i=10;i>=1;i--)
                      cout<<a[i]<<" ";
                  return 0;
              }
              • @ 2024-3-13 22:13:49

                数组可以在外面定义,不用初始化

            • -2
              @ 2023-3-26 13:18:13
              #include<bits/stdc++.h>
              using namespace std;
              int main()
              {
                  int a[10];
                  for(int i=0;i<10;i++)
                  {
                      cin>>a[i];
                  }
                  sort(a,a+10);
                  for(int i=9;i>=0;i--)
                  {
                      cout<<a[i]<<" ";
                  }
                  return 0;
              }
              
              • -3
                @ 2023-8-18 21:20:48

                排序就找sort函数👍

                #include <bits/stdc++.h>
                using namespace std;
                int main()
                {
                    int a[10];
                    for (int i = 0; i < 10; i++) cin >> a[i];
                    sort(a , a + 10);
                    for (int i = 9; i >= 0; i--) cout << a[i] << " ";
                    return 0;
                }
                
                • -3
                  @ 2023-2-6 15:40:07

                  写题解请注意

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

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

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

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

                  ```cpp

                  你的代码

                  ```

                  </span>

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

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

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

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

                  题解被删除的可能

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

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

                  • 1

                  【入门】编程输入10个正整数,然后自动按从大到小的顺序输出

                  信息

                  ID
                  169
                  时间
                  1000ms
                  内存
                  16MiB
                  难度
                  3
                  标签
                  递交数
                  167
                  已通过
                  89
                  上传者