acm水题。错误是Segmentation fault:段错误,检查是否有数组越界,指针异常,访问到不应该访问的内存区域
DescriptionYouaregiventheages(inyears)ofallpeopleofacountrywithatleast1yearofage.Youk...
Description
You are given the ages (in years) of allpeople of a country with at least 1 year of age. You know that no individual in that country lives for 100 or more years. Now, you are given a very simple task of sorting all the ages in ascending order.
Input
There are multiple test cases in the input file. Each case starts with an integer n (0<n<=1000000), the total number of people. In the next line, there are n integers indicating the ages. Input is terminated with a case where n = 0. This case should not be processed.
Output
For each case, print a line with n space separated integers. These integers are the ages of that country sorted in ascending order.
Sample Input
5
3 4 2 1 5
5
2 3 2 3 1
0
Sample Output
1 2 3 4 5
1 2 2 3 3
我的代码。。。
#include <stdio.h>
int main() {
int a[1000000],n,j,i,temp;
while(scanf("%d",&n)!=EOF){
if (n==0)
break;
for (i=0; i<n; i++) {
scanf("%d",&a[i]);
}
for(j=0;j<n;j++){
for(i=0;i<n-1-j;i++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
printf("%d",a[0]);
for (i=1; i<n; i++) {
printf(" %d",a[i]);
}
printf("\n");
}
return 0;
}
错误是Segmentation fault:段错误,检查是否有数组越界,指针异常,访问到不应该访问的内存区域 展开
You are given the ages (in years) of allpeople of a country with at least 1 year of age. You know that no individual in that country lives for 100 or more years. Now, you are given a very simple task of sorting all the ages in ascending order.
Input
There are multiple test cases in the input file. Each case starts with an integer n (0<n<=1000000), the total number of people. In the next line, there are n integers indicating the ages. Input is terminated with a case where n = 0. This case should not be processed.
Output
For each case, print a line with n space separated integers. These integers are the ages of that country sorted in ascending order.
Sample Input
5
3 4 2 1 5
5
2 3 2 3 1
0
Sample Output
1 2 3 4 5
1 2 2 3 3
我的代码。。。
#include <stdio.h>
int main() {
int a[1000000],n,j,i,temp;
while(scanf("%d",&n)!=EOF){
if (n==0)
break;
for (i=0; i<n; i++) {
scanf("%d",&a[i]);
}
for(j=0;j<n;j++){
for(i=0;i<n-1-j;i++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
printf("%d",a[0]);
for (i=1; i<n; i++) {
printf(" %d",a[i]);
}
printf("\n");
}
return 0;
}
错误是Segmentation fault:段错误,检查是否有数组越界,指针异常,访问到不应该访问的内存区域 展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励30(财富值+成长值)
1个回答
2014-12-31
展开全部
哪个OJ上的哪一题,这样方便回答者测试修改后是否正确。
希望能够帮助到你。有什么问题欢迎追问。
希望能够帮助到你。有什么问题欢迎追问。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询