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/
相关文章
- 4种Golang并发操作中常见的死锁情形
- Linux如何进行GPIO读写操作的?
- [Linux] 编写Dockerfile文件自动构建镜像
- [Linux] nginx的try_files指令实现隐藏index.php的重写
- [linux] shell脚本编程-统计日志文件中的设备号发通知邮件
- [Linux]F5负载均衡器
- [Go] golang设置运行的cpu数
- [Go] golang的MPG调度模型
- [Linux] 进程间通信
- [linux] 多进程和多线程
- [linux] 进程五状态模型
- Linux系统日志介绍
- Linux系统账户后门及排查
- [Go] golang的用途和windows搭建环境
- Linux操作系统root账号密码获取防范技术研究
- [Linux] Linux系统(进程管理)
- [Linux] ssh秘钥对免密码登陆
- [Linux] Linux系统(用户管理)
- [Linux] Linux系统(文件操作)
- [Linux] Linux系统(登陆、退出、修密码)