xlx1314 2014-12-20 15:55 采纳率: 0%
浏览 5147

求解答!c++中自动随机产生100个在0~1000之间的整数并排序为什么会出现只产生50个数???

#include
#include
#include
using namespace std;

void bubble_up(int list[],int count)
{
for(int i=0;i for(int j=count-1;j>1;j=j-1)
if(list[j-1]>list[j])
{
int tmp=list[j-1];
list[j-1]=list[j];
list[j]=tmp;
}
}
int main()
{
int i;
int array[100]={0};
srand((unsigned)time(NULL));
cout<<"总共有以下100个数字:"<<endl;
for(i=0;i<101;i++)
{
int numtemp=rand()%1000;
array[i]=numtemp;
cout<<array[i]<<"\t";
i++;
}
cout<<endl;
bubble_up(array,100);
cout<<"对原100个数字排序后的结果是:"<<endl;
for(i=0;i<101;i++)
cout<<array[i]<<"\t";
cout<<endl;
return 0;
}

  • 写回答

2条回答 默认 最新

  • chomol 2014-12-21 02:59
    关注

    恩,首先,循环次数有问题
    其次,for中多用了个i++;
    还有,排序算法有问题。
    最后,好好学习吧加油

    #include
    #include

    using namespace std;
    void bubble_up(int list[], int count)
    {
    for (int i = 0; i for (int j = 0; j if (list[j]>list[i])
    {
    int tmp = list[j];
    list[j] = list[i];
    list[i] = tmp;
    }
    }
    int main()
    {
    int i;
    int array[100] = { 0 };
    srand((unsigned)time(NULL));
    cout << "总共有以下100个数字:" << endl;
    for (i = 0; i<100; i++)
    {
    int numtemp = rand() % 1000;
    array[i] = numtemp;
    cout << array[i] << "\t";
    }
    cout << endl;
    bubble_up(array, 100);
    cout << "对原100个数字排序后的结果是:" << endl;
    for (i = 0; i<100; i++)
    cout << array[i] << "\t";
    cout << endl;
    return 0;
    }

    评论

报告相同问题?

问题事件

  • 请提交代码 1月3日

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站