编写程序, 编写程序,随机生成10个两位整数, 用冒泡法将它们从小到大进行排序

作者&投稿:采芳 (若有异议请与网页底部的电邮联系)
编写JAVA程序,用冒泡法对十个随机数由小到大顺序排序,输出排序后的结果~

public class Sequence02 {
public static void main(String[] args) {
int[] number = new int[10];
/**
* 产生10个[0-100]的随机数、注意区间 在j2se中区间一般都是[)
*/
for (int i = 0; i < number.length; i++) {
number[i] = (int) (Math.random() * 101);
}
/**
* 打印随机数
*/
System.out.println("随机数为:");
for (int random : number) {
System.out.print(random + " ");
}
/**
* 冒泡排序
* j的范围:排出9个也就都出来了
* k的范围:前一个与后一个比较选出大的,因为最大就是number.length
*/
int b;
for (int j = 0; j < number.length - 1; j++) {
// for (int k = 0; k < number.length - 1; k++) { 使得排序运行81次
for (int k = 0; k < number.length - (j + 1); k++) { // 排序运行45次,推荐使用
if (number[k] > number[k + 1]) {
b = number[k];
number[k] = number[k + 1];
number[k + 1] = b;
}
}
}
/**
* 打印排序结果
*/
System.out.println("排序结果为:");
for (int random : number) {
System.out.print(random + " ");
}
}
}

main()
{int a[10],i,j,temp;
printf("请输入10个整数:
");
for(i=0;i<10;i++)
scanf("%d",____&a[i]____);
for(i=1;i<10;i++)
for(j=0;___j<n-i______;j++)
if(___a[j]>a[j+1]_________)
{temp=a[j];______a[j]=a[j+1]________;a[j+1]=temp;}
printf("
排好序的10个整数为:
");
for(i=0;i<10;i++)
printf("%5d",____a[i]____);
}

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
using namespace std;
int main()
{
int i,j,t;
int a[10];
srand(time(NULL));
cout<<"随机生成10个两位整数:";
for(i=0;i<10;i++)
{
a[i]=10+rand()%90;
cout<<setw(3)<<a[i];
}
cout<<endl;
for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(a[j]<a[i])
{
t=a[j];
a[j]=a[i];
a[i]=t;
}
}
}
cout<<"用冒泡法将它们从小到大进行排序:";
for(i=0;i<10;i++)
{
cout<<setw(3)<<a[i];
}
return 0;
} 运行结果截图:

import java.util.Random;

public class Sort
{
int[] array = new int[10];
public static void main(String[] args)
{
new Sort().sort();
}

void rand()
{
Random random = new Random();
for(int i = 0; i < 10; i++)
{
array[i] = random.nextInt(90) + 10;
}
}

void sort()
{
rand();
int i,j,temp;
for(i=0; i < 10; i++)
{
for (j = 0; j < 9 - i; j++)
if (array[j] > array[j+1])
{
temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
}
}
for(i = 0; i < 10; i++)
{
System.out.println(array[i]);
}
}
}

那种语言的代码?


宜章县17392794189: 编写程序,随机生成10个两位整数,使用数组处理,求解并输出其中的最大者、最小值以及平均值.!! -
鄂竹希舒: 先在form中定义一个 int[] a = new int[10];生成随机数按钮中int i = 0; Random rd = new Random(); for(i = 0; i < 10; i++) {a[i] = rd.next(9, 100);}最大值按钮中 int i= 0, max = 0;for(i = 0 ; i < 10; i++) {if(max < a[i]) max = a[i];}lblShow.text = ...

宜章县17392794189: 编一个程序能随机生成十个两位整数的程序 -
鄂竹希舒: Java里这么写: for(int i=0;i<10;i++){ System.out.println(90*Math.random()+10); }原理:Math.random()产生一个0-1之间的随机数 其他语言不详

宜章县17392794189: 编写一个程序,用来随机产生10个两位数并找出其中的最大数及最大数的位置 -
鄂竹希舒: CLS RANDOMIZE TIMER DIM a(10) m = 0: n = 1 FOR i = 1 TO 10a(i) = INT(RND * 90 + 10)PRINT "a("; LTRIM$(STR$(i)); ")="; a(i) NEXT i FOR i = 1 TO 10IF m < a(i) THEN m = a(i): n = i NEXT i PRINT SELECT CASE nCASE 1n$ = "st...

宜章县17392794189: 编写程序,生成十个二位随机数,然后冒泡排序 -
鄂竹希舒: #include "stdio.h" #include "math.h" void main() { int td[20],i,j,min; printf("\n产生20个随机数据放入数组中:"); for(i=0;itd[j]) { min=td[j]; td[j]=td[i]; td[i]=min; } } printf("%d ",min); } printf("\n最终数组中的数据输出为:"); for(i=0;i

宜章县17392794189: java编程题写程序,随机生成10个0到9之间的任意整数,并将这些整数用数组保存 -
鄂竹希舒: import java.util.ArrayList; import java.util.List; import java.util.Random;public class Rand {public static void main(String[] args) {List li = new ArrayList();for(int i=0;i<10;i++){int r = new Random().nextInt(10);li.add(r);} for (Integer i: li) {System.out.print(i + "\t");}} }将随机数保存到li中(List类型)

宜章县17392794189: 用JAVA编写一程序,随机产生10个整数,放到一数组中,打印输出平均值. -
鄂竹希舒: import java.util.Random;//说明: 编写一程序,随机产生10个整数,放到一数组中,打印输出平均值.public class shiyan14 { public static void main(String []arrgs) { Random random=new Random(); int[] a=new int[10]; int sum=0; for (int i = 0; i < a....

宜章县17392794189: 用VB编写程序,随机生成10个10~99互不相同的整数,将它们按由小到大的顺序排列起来 -
鄂竹希舒: Private Sub Command1_Click()ClsDim a(1 To 10) As IntegerFor i = 1 To 10Randomizea(i) = Int(Rnd * 90 + 10)For j = 1 To i - 1If a(i) = a(j) Theni = i - 1Exit ForEnd IfNext jNext iPrint "产生的随机数为:"For i = 1 To UBound(a)...

宜章县17392794189: 求一道C语言程序的编程:随机产生10个二位整数,要求将这10个数顺序输出
鄂竹希舒:#include<stdio.h> #include<stdlib.h> #include<time.h> main() { int a[10],i,j,b; srand(time(0)); for(i=0;i<10;i++)a[i]=rand()%90+10; for(i=0;i<9;i++) for(j=0;j<9;j++) { if(a[j]>a[j+1]) { b=a[j]; a[j]=a[j+1]; a[j+1]=b; } } for(i=0;i<10;i++)printf("%4d",a[i]); system("pause"); }

宜章县17392794189: 编写程序,随机生成10个三位正整数,将它们按由小到大的升序排序,并按每行4个数输出. -
鄂竹希舒: VB?public class Generation {public static string str; private static char[] constant ={'0','1','2','3','4','5','6','7','8','9',// 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'//'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U',...

宜章县17392794189: Vb编写程序,随机生成 10 个[1,100]的整数,输出其平均值及大于平均值的数. -
鄂竹希舒: Private Sub Command1_Click() '生成10-100间10个数字求平均值及大于平均值的数字 Me.Cls Dim a() As Integer, i As Integer, he As Integer Dim pj As Single ProduceShu 10, 100, 10, a() Print "生成的10个数字分别为:" & Space(2); For i = ...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网