更多"【说明】
以下程序实现数据的排序,将n个整数分别按照升序和降序进行"的相关试题:
[多项选择]【说明】
以下程序实现数据的排序,将n个整数分别按照升序和降序进行排序,类SortInt_1实现升序排序,类SortInt_2实现降序排序。
【Java代码】
class SortInt_1
int i,i,k,temp;
void SortInt(int a1,int a2[])//升序排序
for(i=0;i<a1-1;i++)
k=i;
for(j=i+1;j<a1;j++)
if( (1) ) k=j;
if(k !=i)
temp=a2[i];a2[i]=a2[k];a2[k]=temp;
class SortInt_2 (2)
int i,j,k,temp;
void SortInt(int a1, int a2[])//降序排序
for(i=0; i<a1-1;i++)
k=i;
for(j=i+1;j<a1;j++)
if( (3) )k=j;
if(k !=i)
temp=a2[i];a2[i]=a2[k];a2[k]=temp;
public class test
public static void main(String args[])
int a[]=10,55,100,35,87,90,100,16;
SortInt_1 NewInt= (4) ;
NewInt.SortInt(a.lenvh,a);//调用SortInt_1类的方法
System.out.prin
[简答题]【说明】
以下程序的功能是设计一个栈类stack<T>,并建立一个整数栈。
【程序】
#include < iostream. h >
#include < stdlib. h >
const int Max =20; //栈大小
template < class T >
class stack{ //栈元素数组
T s[Max]; //栈顶下标
int top;
public:
stack( )
{
top =-1; //栈顶初始化为-1
}
void push( const T &item); //item入栈
T pop( ); //出栈
int stackempty( ) const; //判断栈是否为
};
template < class T >
void stack <T >::push(const T &item)
{
if(top== (1) )
{
cout <<"栈满溢出" <<endl;
exit(1);
}
top ++
s[top] = item;
}
template < class T >
T stack<T> ::pop( )
{
T temp;
if(top== (2) )
{
cout <<"栈为空,不能出栈操作" < < endl;
exit(1);
}
temp =s[top];
top --;
return temp;
}
template < class T >
int stack < T >:: stackempty( ) const
{ return top == -1;
{
void main( )
{
stack <int> st;
int a[] ={1,2,3,4,5};
cout <<"整数栈" <<e
[简答题]【说明】
以下程序的功能是设计一个栈类stack<T>,并建立一个整数栈。
【程序】
#include < iostream. h >
#include < stdlib. h >
const int Max =20; //栈大小
template < class T >
class stack //栈元素数组
T s[Max]; //栈顶下标
int top;
public:
stack( )
top =-1; //栈顶初始化为-1
void push( const T &item); //item入栈
T pop( ); //出栈
int stackempty( ) const; //判断栈是否为
;
template < class T >
void stack <T >::push(const T &item)
if(top== (1) )
cout <<"栈满溢出" <<endl;
exit(1);
top ++
s[top] = item;
template < class T >
T stack<T> ::pop( )
T temp;
if(top== (2) )
cout <<"栈为空,不能出栈操作" < < endl;
exit(1);
temp =s[top];
top --;
return temp;
template < class T >
int stack < T >:: stackempty( )
[简答题]【说明】数据排序。将给定的n个整数分别按照升序和降序进行排列。
class SortInt_1
{
int i, j, k, temp;
void SortInt(int a1, a2[]){//升序排序
for(i=0; i<a1-1; i++){
k=i;
for(j=i+1 ;j<a1 ;j++)
if ( (1) ) k=j;
if(k!=i){
temp=a2[i];a2[i]=a2[k];a2[k]=temp;
}
}
}
}
class Sortlnt_2 (2)
{
int i, j, k, temp;
void Sortlnt(int a1,a2[]){//降序排序
for(i=0;i<a1-1 ;i++) {
k=i;
for(j=i+1 ;j<a1 ;j++)
if ( (3) ) k=j;
if(k!=i){
temp=a2[i];a2[i]=a2[k];a2[k]=temp;
}
}
}
}
Class TestOverLoad {
Public static void main(String args[])
{
int a[]={10,55,100,35,87,90,100,16};
Sortlnt_1 newlnt1= (4) ;
Newlnt1. SortInt(a. length, a);//调用SortInt_1类的方法
System. out. println("升序排列的数据");
For(int i=0;i<8;i++)
System. out. print(a[i]+" ");
system. out. println( );
SortInt_2 newInt2=new sortint_2( ); //创
[简答题]【说明】本程序ExceptionTester实现功能:读入两个整数,第1个数除以第2个数,之后输出。若第2个数为0,则自动进行异常处理。
程序如下:
(1) ;
public class ExceptionTester
public static void main(String args[])
int result;
int number[]=new int[2];
boolean valid; for(int i=0;i<2;i++)
valid= (2) ;
while(!valid)
try
System.out.println("Enter number"+(i+1));
number[i]=Integer.valueOf(Keyboard.getString( )).intValue( );
valid=true;
catch(NumberFormatExceptione)
System.out.println("Invalid integer entered.Please try again.");
by
result=number[0]/number[1];
System.out.print(number[0]+"/"+number[1]+"="+result);
catch( (3) )
System.out.println("Second number is 0,cannot do division!");
其中,Keyboard类的声明为:
impon java.io.*;
public class Keyboard
static BufferedReader inputStream=new (4)
(new Inp
[简答题]【说明】本程序实现功能:读入两个整数,第1个数除以第2个数,声明当除数为零时抛出异常类DivideByZeroException。
public class DivideByZeroException (1) {
public DivideByZeroException ( ) {
super("Attcmpted to divide by zero");
}
}
import java.io. *;
public class Example {
private static int quotient(int numerator, in)/”}t denominator) throws
DivideByZeroException {
if (denominator==0)
throw (2) ;
return(numerator / denominator);
}
public static void main(String args[]) {
int number1=0, number2=0, result0;
try{
System.out.print1n("Enter the first number:");
number1 = Integer. valueOf(Keyboard.getString( )).intValue( );
System.out.print1n("Enter the second number:");
number2 = Integer. Va1ueOf(Keyboard.getString( )).intValue( );
result = quotient(number1,number2);
}
catch (NumberFormatException e) {
System.out.print1n("Invalid integer entered!");
System. exit(-1);
}
catch ( (3) ) {
Syste
[填空题]下列程序为比较排序法排序程序,请补充完该程序(该程序对10个数值数据进行排序处理)。
Dim a(1 To 10)AS Integer
Private Sub Form _Click( )
Randomize
p=“”
For i = 1 To 10
Do
x = Int (Rnd * 90) + 10
yes = 0
For j = 1 To 【9】
If x = a(j) Then yes = 1: Exit For
Next
Loop While yes = 1
a(i) = x
Next i
For i = 1 To 9
For 【10】 To 10
If a(i) > a(j) Then
t = a(i)
【11】
a(j) = t
End If
Next j
Next i
p = Str(a(1))
For i = 2 To 10
p = p & “,” & Str(a(i))
Print p
Next i
End Sub
[简答题]【说明】
进行两个整数之间的比较,由考生通过输入窗口分别输入两个整数,程序比较出结果。例如:先后输入的两个数分别为25和36。
比较结果显示:
25!=36
25<36
25<=36
【Java代码】
import javax.swing.JOptionPane;
public class Java3
public static void main(String args[])
String (1) // 用户输入第1个字符串
secondNumber, // 用户输入第2个字符串
result; // 包含输出
int number1, // 比较的第1个数
number2; // 比较的第2个数
// 用户输入的第1个字符串
firstNumber =
JOptionPane. (2) ("Enter first integer:");
//读用户输入的第2个字符串
secondNumber =
JOptionPane.showlnputDialog("Enter second integer:");
将字符串类型转换成整数类型
number1= Integer. (3) (firstNumber);
number2= Integer.parselnt(secondNumber);
result= "":
if ( (4) )
result=number1+"=="+number2;
if (number1 != number2)
result=number1+"!="+number2;
if (number1<number2)
[单项选择]以下程序中函数f的功能是将n个字符串按由大到小的顺序进行排序。
#include <string.h>
void f(char p[][10], int n)
{
char t[20]; int i,j;
for(i=0;i<n-1;i++)
for(j=i+l;j<n;j++)
if(strcmp (p[i],p[j])<0)
{ strcpy(t,p[i]);strcpy(p[i],p[j]);strcpy(p[j],t);}
}
main( )
{
char p[][lO]-{"abc","aabdfg","abbd","dcdbe","cd"};int i;
f(p,5); printf("%dkn",strlen(p[0]));
}
程序运{亍后的输出结果是
A. 6
B. 4
C. 5
D. 3
[简答题]有一个程序:输入3个整数ABC,把这3个数看成是一个三角形的3条边。这个程序要输出信息,来说明这个三角形的几种情况:是三边不等的、是等腰的、还是等边的。请根据给出的说明,用等价类划分法设计该问题的测试用例(要求写出设计过程)。
[单项选择]以下程序中函数f的功能是将n个字符串按由大到小的顺序进行排序。
#include <string. h>
void f(char p[][10],int n) chart[20];int i,j;
for(i=0;i<n-1;i++)
for(j=i+1;j<n; j++)
if(strcmp(p[i],p[j]<0)
strcpy(t, p[i]); strcpy(p[i],p[j]);strcpy(p[j],t);main( ) char p[][10]="abc", "aabdfg", "abbd", "dcdbe", "cd"; int i;
f(p,5); printf("%d/n", strlen(p[0]));
程序运行后的输出结果是( )。
A. 6
B. 4
C. 5
D. 3
[单项选择]以下程序中函数f( )的功能是将n个字符串按由大到小的顺序进行排序。
#include<string.h>
void f(char p[ ][10],int n)
{ char t[20]; int i,j;
for(i =O;i <n-1 ;i ++ )
for(j =i + 1 ;j < n;j ++ )
if(strcmp(p [i] ,p[j] ) <0)
{ strcpy(t,p[i]);strcpy(p[i] ,p[j] );strcpy(p[j] ,t); } }
main ( )
{ char p [ ] [ 10 ] = { "abc","aabdfg","abbd","dcdbe", "cd" }; int i;
f(p,5); printf("%d/n",strlen(p[O] ) );
}
程序运行后的输出结果是( )。
A. 6
B. 4
C. 3
D. 5