java 题目:有三个学生,每个学生有五门课程的成绩,从键盘输入以下数据:学生号,五门课程成绩。 有五个学生,每个学生有三门课程的成绩,从键盘输入学生数据(包...
\u7f16n\u4e2a\u5b66\u751f\uff0cm\u95e8\u8bfe\u7a0b\u7684\u6210\u7ee9\u5904\u7406\u987a\u5e8f\uff0c1.\u5b9e\u9645\u5b66\u751f\u4eba\u6570\uff0c\u8bfe\u7a0b\u7531\u952e\u76d8\u8f93\u5165\uff0c2:n\u4e2a\u5b66\u751f\uff0cm\u95e8\u8bfe\u7a0b\u6210\u7ee9\u7528// Ch08sort.cpp : Defines the entry point for the console application.
//\u9ed8\u8ba4\u662f\u4ece\u5c0f\u5230\u5927\u6392\u5e8f
#include
#include
#include
using namespace std;
//\u8981\u6392\u5e8f\u7684\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u4ee5\u53ca\u53d6\u503c\u7684\u8303\u56f4
#define SIZE 10
#define MAX 10000
//---------------------------------\u63d2\u5165\u6392\u5e8f----------------------------------------
//\u76f4\u63a5\u63d2\u5165\u6392\u5e8f080201
//\u539f\u7406\uff1a\u6bcf\u6b21\u5c06\u5f85\u6392\u5e8f\u7684\u8bb0\u5f55\uff0c\u6309\u5176\u5173\u952e\u5b57\u5927\u5c0f\u63d2\u5165\u5230\u524d\u8fb9\u5df2\u7ecf\u6392\u597d\u5e8f\u7684\u5b50\u6587\u4ef6\u4e2d\u7684\u9002\u5f53\u4f4d\u7f6e
int InsertSort(int arr[],int len){
int temp,j;
for(int i=1;i<len;i++){
if(arr[i]<arr[i-1]){
temp=arr[i];
j=i;
do{
arr[j]=arr[j-1];
j--;
}while(j>0&&arr[j-1]>temp);
arr[j] = temp;
}
}
return 0;
}
//SHELL\u6392\u5e8f \u5e0c\u5c14\u6392\u5e8f 8.2.2
/*
\u5148\u53d6\u4e00\u4e2a\u5c0f\u4e8en\u7684\u6574\u6570d1\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u589e\u91cf\uff0c\u628a\u6587\u4ef6\u7684\u5168\u90e8\u8bb0\u5f55\u5206\u6210d1\u4e2a\u7ec4\u3002
\u6240\u6709\u8ddd\u79bb\u4e3adl\u7684\u500d\u6570\u7684\u8bb0\u5f55\u653e\u5728\u540c\u4e00\u4e2a\u7ec4\u4e2d\u3002\u5148\u5728\u5404\u7ec4\u5185\u8fdb\u884c\u76f4\u63a5\u63d2\u4eba\u6392\u5e8f;
\u7136\u540e\uff0c\u53d6\u7b2c\u4e8c\u4e2a\u589e\u91cfd2<d1\u91cd\u590d\u4e0a\u8ff0\u7684\u5206\u7ec4\u548c\u6392\u5e8f\uff0c\u76f4\u81f3\u6240\u53d6\u7684\u589e\u91cfdt=1(dt<dt-l<\u2026<d2<d1)\uff0c
\u5373\u6240\u6709\u8bb0\u5f55\u653e\u5728\u540c\u4e00\u7ec4\u4e2d\u8fdb\u884c\u76f4\u63a5\u63d2\u5165\u6392\u5e8f\u4e3a\u6b62\u3002
*/
int ModifiedInsertSort(int arr[],int n,int delta){
int temp;
for(int i=delta;i<n;i+=delta){
for(int j=i;j>=delta;j-=delta){
if(arr[j]<arr[j-delta]){
temp = arr[j];
arr[j] = arr[j-delta];
arr[j-delta] = temp;
}
}
}
return 0;
}
int ShellSort(int arr[],int len){
//
for(int delta=len/2;delta>0;delta/=2){
//\u5206\u522b\u5bf9delta\u4e2a\u5b50\u5e8f\u5217\u8fdb\u884c\u6392\u5e8f//
for(int j=0;j<delta;j++){
ModifiedInsertSort(&arr[j],len-j,delta);
}
}
return 0;
}
//1\uff0e \u4e0d\u8bbe\u76d1\u89c6\u54e8\u7684\u7b97\u6cd5\u63cf\u8ff0
////\u6ce8\u610f\uff1a \u5f53\u589e\u91cfd=1\u65f6\uff0cShellPass\u548cInsertSort\u57fa\u672c\u4e00\u81f4\uff0c\u53ea\u662f\u7531\u4e8e\u6ca1\u6709\u54e8\u5175\u800c\u5728\u5185\u5faa\u73af\u4e2d\u589e\u52a0\u4e86\u4e00\u4e2a\u5faa\u73af\u5224\u5b9a\u6761\u4ef6"j>0"\uff0c\u4ee5\u9632\u4e0b\u6807\u8d8a\u754c\u3002
void ShellPass(int *R,int n,int d)
{//\u5e0c\u5c14\u6392\u5e8f\u4e2d\u7684\u4e00\u8d9f\u6392\u5e8f\uff0cd\u4e3a\u5f53\u524d\u589e\u91cf
int i,j;
for(i=d+1;i<=n;i++) //\u5c06R[d+1\uff0e\uff0en]\u5206\u522b\u63d2\u5165\u5404\u7ec4\u5f53\u524d\u7684\u6709\u5e8f\u533a
if(R[i] <R[i-d] ){
R[0]=R[i];j=i-d; //R[0]\u53ea\u662f\u6682\u5b58\u5355\u5143\uff0c\u4e0d\u662f\u54e8\u5175
do {//\u67e5\u627eR[i]\u7684\u63d2\u5165\u4f4d\u7f6e
R[j+d]=R[j]; //\u540e\u79fb\u8bb0\u5f55
j=j-d; //\u67e5\u627e\u524d\u4e00\u8bb0\u5f55
}while(j>0&&R[0] <R[j] );
R[j+d]=R[0]; //\u63d2\u5165R[i]\u5230\u6b63\u786e\u7684\u4f4d\u7f6e\u4e0a
} //endif
} //ShellPass
void ShellSort11(int* R,int n)
{
int increment=n; //\u589e\u91cf\u521d\u503c\uff0c\u4e0d\u59a8\u8bben>0
do {
increment=increment/3+1; //\u6c42\u4e0b\u4e00\u589e\u91cf
ShellPass(R,n,increment); //\u4e00\u8d9f\u589e\u91cf\u4e3aincrement\u7684Shell\u63d2\u5165\u6392\u5e8f
}while(increment>1);
} //ShellSort
/*
2\uff0e\u8bbe\u76d1\u89c6\u54e8\u7684shell\u6392\u5e8f\u7b97\u6cd5
\u7b97\u6cd5\u5206\u6790
1\uff0e\u589e\u91cf\u5e8f\u5217\u7684\u9009\u62e9
Shell\u6392\u5e8f\u7684\u6267\u884c\u65f6\u95f4\u4f9d\u8d56\u4e8e\u589e\u91cf\u5e8f\u5217\u3002
\u597d\u7684\u589e\u91cf\u5e8f\u5217\u7684\u5171\u540c\u7279\u5f81\uff1a
\u2460 \u6700\u540e\u4e00\u4e2a\u589e\u91cf\u5fc5\u987b\u4e3a1\uff1b
\u2461 \u5e94\u8be5\u5c3d\u91cf\u907f\u514d\u5e8f\u5217\u4e2d\u7684\u503c(\u5c24\u5176\u662f\u76f8\u90bb\u7684\u503c)\u4e92\u4e3a\u500d\u6570\u7684\u60c5\u51b5\u3002
\u6709\u4eba\u901a\u8fc7\u5927\u91cf\u7684\u5b9e\u9a8c\uff0c\u7ed9\u51fa\u4e86\u76ee\u524d\u8f83\u597d\u7684\u7ed3\u679c\uff1a\u5f53n\u8f83\u5927\u65f6\uff0c\u6bd4\u8f83\u548c\u79fb\u52a8\u7684\u6b21\u6570\u7ea6\u5728nl.25\u52301.6n1.25\u4e4b\u95f4\u3002
2\uff0eShell\u6392\u5e8f\u7684\u65f6\u95f4\u6027\u80fd\u4f18\u4e8e\u76f4\u63a5\u63d2\u5165\u6392\u5e8f
\u5e0c\u5c14\u6392\u5e8f\u7684\u65f6\u95f4\u6027\u80fd\u4f18\u4e8e\u76f4\u63a5\u63d2\u5165\u6392\u5e8f\u7684\u539f\u56e0\uff1a
\u2460\u5f53\u6587\u4ef6\u521d\u6001\u57fa\u672c\u6709\u5e8f\u65f6\u76f4\u63a5\u63d2\u5165\u6392\u5e8f\u6240\u9700\u7684\u6bd4\u8f83\u548c\u79fb\u52a8\u6b21\u6570\u5747\u8f83\u5c11\u3002
\u2461\u5f53n\u503c\u8f83\u5c0f\u65f6\uff0cn\u548cn2\u7684\u5dee\u522b\u4e5f\u8f83\u5c0f\uff0c\u5373\u76f4\u63a5\u63d2\u5165\u6392\u5e8f\u7684\u6700\u597d\u65f6\u95f4\u590d\u6742\u5ea6O(n)\u548c\u6700\u574f\u65f6\u95f4\u590d\u6742\u5ea60(n2)\u5dee\u522b\u4e0d\u5927\u3002
\u2462\u5728\u5e0c\u5c14\u6392\u5e8f\u5f00\u59cb\u65f6\u589e\u91cf\u8f83\u5927\uff0c\u5206\u7ec4\u8f83\u591a\uff0c\u6bcf\u7ec4\u7684\u8bb0\u5f55\u6570\u76ee\u5c11\uff0c\u6545\u5404\u7ec4\u5185\u76f4\u63a5\u63d2\u5165\u8f83\u5feb\uff0c\u540e\u6765\u589e\u91cfdi\u9010\u6e10\u7f29\u5c0f\uff0c\u5206\u7ec4\u6570\u9010\u6e10\u51cf\u5c11\uff0c\u800c\u5404\u7ec4\u7684\u8bb0\u5f55\u6570\u76ee\u9010\u6e10\u589e\u591a\uff0c\u4f46\u7531\u4e8e\u5df2\u7ecf\u6309di-1\u4f5c\u4e3a\u8ddd\u79bb\u6392\u8fc7\u5e8f\uff0c\u4f7f\u6587\u4ef6\u8f83\u63a5\u8fd1\u4e8e\u6709\u5e8f\u72b6\u6001\uff0c\u6240\u4ee5\u65b0\u7684\u4e00\u8d9f\u6392\u5e8f\u8fc7\u7a0b\u4e5f\u8f83\u5feb\u3002
\u56e0\u6b64\uff0c\u5e0c\u5c14\u6392\u5e8f\u5728\u6548\u7387\u4e0a\u8f83\u76f4\u63a5\u63d2\u4eba\u6392\u5e8f\u6709\u8f83\u5927\u7684\u6539\u8fdb\u3002
3\uff0e\u7a33\u5b9a\u6027
\u5e0c\u5c14\u6392\u5e8f\u662f\u4e0d\u7a33\u5b9a\u7684\u3002\u53c2\u89c1\u4e0a\u8ff0\u5b9e\u4f8b\uff0c\u8be5\u4f8b\u4e2d\u4e24\u4e2a\u76f8\u540c\u5173\u952e\u5b5749\u5728\u6392\u5e8f\u524d\u540e\u7684\u76f8\u5bf9\u6b21\u5e8f\u53d1\u751f\u4e86\u53d8\u5316\u3002
*/
//---------------------------------\u4ea4\u6362\u6392\u5e8f----------------------------------------
//\u4ea4\u6362\u6392\u5e8f\uff0d\uff0d\uff0d\u5192\u6ce1\u6392\u5e8f\uff0c\u5feb\u901f\u6392\u5e8f
//\u539f\u7406\uff1a\u4e24\u4e24\u6bd4\u8f83\u5f85\u6392\u5e8f\u8bb0\u5f55\u7684\u5173\u952e\u5b57\uff0c\u53d1\u73b0\u4e24\u4e2a\u8bb0\u5f55\u7684\u6b21\u5e8f\u76f8\u53cd\u65f6\u5373\u8fdb\u884c\u4ea4\u6362\uff0c\u76f4\u5230\u6ca1\u6709\u53cd\u5e8f\u7684\u8bb0\u5f55\u4e3a\u6b62\u3002
//\u5192\u6ce1\u6392\u5e8f080301
int BubbleSort(int arr[],int len){
int k,temp ;
for(int i=0;i<len-1;i++){
k = i;
for(int j=i+1;j<len;j++){
if(arr[j]<arr[k])
k = j;
}
if(k!=i){
temp = arr[i];
arr[i] = arr[k];
arr[k] = temp;
}
}
return 0;
}
//\u4f18\u5316\u7684\u5192\u6ce1\u6392\u5e8f
int ImprovedBubbleSort(int arr[],int len){
bool noswap;
int temp;
for(int i=0;i<len;i++){
noswap = true;
for(int j=len-1;j>i;j--){
if(arr[j]<arr[j-1]){
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
noswap = false;
}
}
if(noswap){
break;
}
}
return 0;
}
//\u5feb\u901f\u6392\u5e8f080302
int Partition(int arr[],int len,int i,int j){
int pivot = arr[i];
while(i<j){
while(i=pivot){
j--;
}
if(i<j){
arr[i++] = arr[j];
}
while(i<j&&arr[i]<=pivot){
i++;
}
if(i<j){
arr[j--] = arr[i];
}
}
arr[i] = pivot;
return i;
}
int QuickSort(int arr[],int len,int low=0,int high=SIZE){
int pivotpos;
if(low<high){
pivotpos = Partition(arr,len,low,high);
QuickSort(arr,len,low,pivotpos-1);
QuickSort(arr,len,pivotpos+1,high);
}
return 0;
}
//---------------------------------\u9009\u62e9\u6392\u5e8f----------------------------------------
//\u76f4\u63a5\u9009\u62e9\u6392\u5e8f080401
//\u539f\u7406\uff1a\u6bcf\u4e00\u8d9f\u4ece\u5f85\u6392\u5e8f\u7684\u8bb0\u5f55\u4e2d\u9009\u51fa\u5173\u952e\u5b57\u6700\u5c0f\u7684\u8bb0\u5f55\uff0c\u987a\u5e8f\u653e\u5230\u5df2\u6392\u597d\u5e8f\u7684\u5b50\u6587\u4ef6\u7684\u6700\u540e
int StraightSelect(int arr[],int len){
int temp=0;
int k;
for(int i=0;i<len;i++){
k=i;
for(int j=k+1;j<len;j++){
if(arr[j]<arr[k]){
k = j;
}
}
if(k!=i){
temp = arr[k];
arr[k] = arr[i];
arr[i] = temp;
}
}
return 0;
}
//---------------------------------\u5f52\u5e76\u6392\u5e8f----------------------------------------
//\u5f52\u5e76\u6392\u5e8f\uff1a
int Merge(int arr[],int len){
return 0;
}
//---------------------------------\u5206\u914d\u6392\u5e8f----------------------------------------
//\u6876\u6392\u5e8f,\u7bb1\u6392\u5e8f8.6
//\u4e8b\u5148\u77e5\u9053\u5e8f\u5217\u4e2d\u7684\u8bb0\u5f55\u90fd\u4f4d\u4e8e\u67d0\u4e2a\u5c0f\u533a\u95f4\u6bb5[0\uff0cm)\u5185
// \u5c06\u5177\u6709\u76f8\u540c\u503c\u7684\u8bb0\u5f55\u90fd\u5206\u914d\u5230\u540c\u4e00\u4e2a\u6876\u4e2d\uff0c\u7136\u540e\u4f9d\u6b21\u6309\u7167\u7f16\u53f7\u4ece\u6876\u4e2d\u53d6\u51fa\u8bb0\u5f55\uff0c\u7ec4\u6210\u4e00\u4e2a\u6709\u5e8f\u5e8f\u5217
int BucketSort(int arr[],int len){
int max = MAX;//\u6700\u5927\u503c
int *count = new int[max];
int i;
for(i=0;i<max;i++){
count[i] = 0;
}
for(i=0;i<len;i++){
count[arr[i]]++;
}
for(i=0;i<len;){
for(int j=0;j<max;j++){
while(count[j]>0){
arr[i++] = j;
count[j]--;
}
}
}
return 0;
}
//\u6253\u5370\u6570\u7ec4
int printarr(int arr[],int len){
for(int i=0;i<len;i++){
if(i%10==0)
cout<<endl;
cout<<setw(4)<<arr[i]<<" ";
}
cout<<endl;
return 0;
}
int main()
{
int arr[SIZE];
int len = sizeof(arr)/sizeof(arr[0]);//SIZE
srand( (unsigned)time( NULL ) );
for(int i=0;i<len;i++){
arr[i] =rand()%MAX;
}
cout<<"\u751f\u6210\u6570\u7ec4\uff1a"<<endl;
printarr(arr,len);
// StraightSelect(arr,len);
// InsertSort(arr,len);
// BubbleSort(arr,len);
// QuickSort(arr,len);
// ImprovedBubbleSort(arr,len);
// ShellSort(arr,len);
BucketSort(arr,len);
cout<<endl<<"\u6392\u5e8f\u540e\u5f97\u5230\u7684\u6570\u7ec4\uff1a"<<endl;
printarr(arr,len);
return 0;
}
\u7ed9\u6211\u5c31\u79c1
public static void main(String[] args) {Scanner input = new Scanner(System.in);
String [][] arrs = new String[3][6];
for (int i = 0; i <arrs.length; i++) {
System.out.println("请输入第"+(i+1)+"学生号!");
String stuID = input.next();
arrs[i][0] = stuID;
for (int j = 1; j <arrs[i].length; j++) {
System.out.println("请输入第"+j+"门课成绩!");
int grade = input.nextInt();
if(grade<0){
System.out.println("输入有误,请重新输入第"+j+"门课成绩!");
}
arrs[i][j] = grade+"";
}
}
for (int i = 0; i < arrs.length; i++) {
for (int j = 0; j < arrs[i].length; j++) {
System.out.print(arrs[i][j]+" ");
}
System.out.println();
}
System.out.println("--------------------------------");
for (int i = 1; i < arrs[0].length; i++) {
int [] index = {i};
sort(arrs,index);
System.out.println("第"+i+"门的成绩最好的学号为:"+arrs[2][0]);
}
}
public static void sort(String[][] ob, final int[] order) {
Arrays.sort(ob, new Comparator<Object>() {
public int compare(Object o1, Object o2) {
String[] one = (String[]) o1;
String[] two = (String[]) o2;
for (int i = 0; i < order.length; i++) {
int k = order[i];
if (Integer.parseInt(one[k]) > Integer.parseInt(two[k])) {
return 1;
} else if (Integer.parseInt(one[k]) < Integer.parseInt(two[k])) {
return -1;
} else {
continue; //如果按一条件比较结果相等,就使用第二个条件进行比较。
}
}
return 0;
}
});
}
我用的是二维数组录入成绩的方便用比较最大值!你可以换个容器试试,希望能帮到你!望采纳给赞!谢谢
扩展阅读:java题库网站免费 ... java-jar ... java入门网站 ... jquery防止xss攻击 ... java.gamedog.cn ... java web期末大作业 ... java poi ... jquery 漏洞利用 ... java 导入 ...