排序数字的数字 [英] Sorting digits of number
本文介绍了排序数字的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要对数字的4位数进行排序。
我的逻辑(一种冒泡排序)无法正常工作。
这里有什么问题?
我的尝试:
I need to sort the 4 digits of a number.
My logic is(a kind of bubble sort) which is not working.
What is the issue here?
What I have tried:
for(i=0;i<4;i++){
scanf("%d",&a[i]);
}
for(i=0;i<=4;i++){
for(j=0;j<=4-i-1;j++){
if(a[j]>a[j+1]){
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
printf("%d",a[j]);
}
}
}
推荐答案
你昨天在数字的位数 - C / C ++ / MFC讨论区 [ ^ ]。请不要重新发布;您可以编辑原始文件以添加更多信息。
You posted yhis same question yesterday at Sort digits of number - C / C++ / MFC Discussion Boards[^]. Please do not repost; you can edit your original to add more information.
排序必须包含a [i]
和一个[j]的
。尝试
The sort must involve botha[i]
anda[j]
. Try
#include <stdio.h>
int main()
{
int i,j;
char a[4];
for(i=0;i<4;++i)
{
scanf("%c",&a[i]);
}
for(i=0; i<3; ++i)
for(j=i+1; j<4; ++j)
{
if(a[i]>a[j])
{
char t =a[i];
a[i]=a[j];
a[j] = t;
}
}
for ( i=0; i<4; ++i)
printf("%c", a[i]);
printf("\n");
return 0;
}
你有超出范围的数组访问。打印出使用过的索引来查看它们:
You have out of bound array accesses. Print out the used indexes to see them:
printf("%d after swapping indexes %d and %d\n",a[j], j, j+1);
你必须将循环迭代限制为数组项的数量。
冒泡排序算法查找例如 [ ^ ]。
这篇关于排序数字的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文