HDU 1040 As Easy As A+B

这是一篇关于解决简单排序问题的博客。作者通过一个简单的A+B问题引入,描述了一个给定整数序列进行升序排序的任务。文章提供样本输入和输出,并分享了使用冒泡排序的思路。在编程过程中遇到了Runtime Error,并给出了可能的解决方案,如检查数组越界、除0错误等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目
Problem Description
These days, I am thinking about a question, how can I get a problem as easy as A+B? It is fairly difficulty to do such a thing. Of course, I got it after many waking nights.
Give you some integers, your task is to sort these number ascending (升序).
You should know how easy the problem is now!
Good luck!
Input
Input contains multiple test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow. Each test case contains an integer N (1<=N<=1000 the number of integers to be sorted) and then N integers follow in the same line.
It is guarantied that all integers are in the range of 32-int.
Output
For each case, print the sorting result, and one line one case.
Sample Input
2
3 2 1 3
9 1 4 7 2 5 8 3 6 9
Sample Output
1 2 3
1 2 3 4 5 6 7 8 9

思路
输入:
第一行 n 代表输入n个例子
下一行 x 代表会有多少个数字出现 a b c(出现x个乱序的数字
最后一行 y 代表会有多少个数字出现 d e f g h(出现y个乱序的数字
输出:
第一行 从小到大升序排序的x个数字
最后一行 从小到大升序排序的y个数字
用的是冒泡排序来写,其实冒泡忘得差不多了,呜呜
第一次写Runtime Error(ACCESS_VIOLATION)了
具体解决办法:
  检查一下数组、指针是否越界;
  是否除0;
  检查一下小数组是否符合题意,可以把数组开的大一些;
  检查一下局部数组变量是否过大。
C++可以用longlongint,因为数字很大但还是在32int的范围内…一开始学习acm的时候写的一道题,现在看看真的当时有好多弱智问题搞不明白,太傻了

代码

#include <stdio.h>

int main()
{
	int n;
	scanf("%d",&n);
	while(n--)//一开始写的还是scanf("%d",&n)!=EOF
	{
		_int64 a[1005];//本题如果使用整型int来保存结果,会导致结果错误。
							  //用使用printf输出结果时,则你的格式字符串应该写成%I64d以输出long long类型的整数。
						      //VisualC++6.0 环境运行下,将long long 用 _int64 进行替换
							  //用long long会报错:error C2632: 'long' followed by 'long' is illegal
							  //因为 VC6中所使用的编译器是C90标准的,而 long long 型是在C99中新加入的
							  //(longlong int双长整型是C 99扩充的数据类型
		                      //同时扩充的还有float_complex,double_complex,longlong_complex,bool等),故无法实现编译
		int i,j,t,m,x=0;
		scanf("%d",&m);//差点忘了这个控制有多少个数字的输入
		for(i=0;i<m;i++)
		{
			scanf("%I64d",&a[i]);//这里的取地址符忘了,I64d也没注意改,太粗心了
		}
		for(i=0;i<m;i++)
		{
			for(j=i;j<m;j++)//这里一开始写成(j=0;j<m-i;j++)了
			{
				if(a[i]>a[j])
				{
					t=a[i];
					a[i]=a[j];
					a[j]=t;
				}
			}
		}

		for(i=0;i<m;i++)
		{
			if(x==0)
			{
				printf("%I64d",a[i]);
				x++;
			}
			else
			printf(" %I64d",a[i]);
		}
		putchar('\n');//是单引号啊!!!
	}
	return 0;//这个不影响就AC
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值