zl程序教程

您现在的位置是:首页 >  其他

当前栏目

c 按照位数读取一行-C++习题 倒置排序 OpenJudge

2023-02-18 16:46:37 时间

  一道习题的解法,可供参考。

  一、题目

  描述

  将一些整数按倒置值排序后输出.所谓倒置,是指把整数各位倒过来构成一个新数,例如:13倒置成了31.

  输入

  第一行的整数N表示后面列出的组数。每组数的第一个整数n表示后面将有n个整数。(每组数据量不超80)

  输出

  将每组数按倒置值进行排序输出.其每组数的结果占一行.

  二、代码 1.头文件

  代码如下:

  #include

  2.主代码

  代码如下:

#include
using namespace std;
int main()
{
    int n, m;
    int arr1[100], arr2[100];        //设计数组,arr1用于输出,arr2用于倒置排序
    cin >> n;
    for (int i = 1; i 
    {
        cin >> n;
        for (int j = 0; j < n; j++)            //输入值
        {
            cin >> m;
            arr1[j] = m;
            arr2[j] = m;
        }
        for (int j = 0; j 
            int p, q ,sum;
            sum = 0;
            p = arr2[j];
                while (p != 0)
                {
                    q = p % 10;
                    sum += q;
                    sum = sum  * 10;
                    p = p / 10;
                }

                sum = sum / 10;
                arr2[j] = sum;
        }
        for (int i = 0; i < n; i++)                    //将数值arr2中数值由小到大排序,arr1按arr2的顺序排列
            for (int j = 0; j < n - 1; j++)            //可以常规交换也可以用swap函数
                if (arr2[j] > arr2[j + 1])
                {
                    int temp;
                    temp = arr2[j];
                    arr2[j] = arr2[j + 1];

                    arr2[j + 1] = temp;
                    swap(arr1[j], arr1[j + 1]);
                }
        for (int i = 0; i < n; i++)                    //输出arr1的结果即可
        {
            cout 
[1]: https://xuan.ddwoo.top/index.php/archives/429/
[2]: https://xuan.ddwoo.top/index.php/archives/422/
[3]: https://xuan.ddwoo.top/index.php/archives/422/
[4]: https://xuan.ddwoo.top/index.php/archives/421/
[5]: https://xuan.ddwoo.top/index.php/archives/421/