设计一个时间复杂度为O(n)的算法,实现将数组a[n]中所有元素调整为左右两部分,左边为奇数,右边为偶数。将下面代码补充完整。 public static void Adjust(int a[],int n){ int i=0,j=n-1,temp; while(i
举一反三
- 代码填空【快速排序的分区函数:以第1个元素为基准元素】 int swap(int a[], int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } int partition(int a[], int p, int q) { int x = a[/i][/i]; int i = p, j; for(j = p + 1;j <= q; j++) { if(a[j]<=x) { i++; ______(1)________; } } _________(2)___________; return i; }
- 下面 rotate 函数的功能是:将 n 行 n 列的矩阵a 转置#define N 4 void rotate(int a[][N]) { int i,j,t; for(i=0;i 答案: (1) j (2) a[i][j]=a[j][i] 答案解析: 难易: 中 知识点:[/i][/i]
- 针对如下三个实现矩阵求和的不同函数:int a[N][N];int sumA( int a[N][N] ){ int i, j; int sum = 0; for ( i = 0; i < N; i++ ) for ( j = 0; j < N; j++ ) sum += a[i][j]; return sum;}int sumB( int a[N][N] ){ int i, j; int sum = 0; for ( j = 0; j < N; j++ ) for ( i = 0; i < N; i++ ) sum += a[i][j]; return sum;}int sumC( int a[N][N] ){ int i, j; int sum = 0; for ( j = 0; j < N; j+=2 ) for ( i = 0; i < N; i+=2 ) sum += ( a[i][j] + a[i+1][j] + a[i][j+1] + a[i+1][j+1] ); return sum;}当N足够大的时候,三个函数的运行时间t1、t2、t3符合下列哪种情况?()[/i][/i][/i][/i] A: t1 > t2 > t3 B: t3 > t1 > t2 C: t2 > t3 > t1 D: t3 > t2 > t1
- 写出以下程序运行结果。 #include<iomanip.h> const int N=3; int main() { int a[N][N]={{7,-5,3},{2,8,-6},{1,-4,-2}}; int b[N][N]={{3,6,-9},{2,-8,3},{5,-2,-7}}; int i,j,c[N][N]; for(j=0;j<N;i++) //计算矩阵C for(j=0;j<N;j++) c[i][j]=a[i][j]+b[i][j]; for(j=0;j<N;i++) //输出矩阵C { for(j=0;j<N;j++) cout<<setw(5)<<c[i][j]; cout<<endl; } }[/i][/i][/i][/i]
- 写输出结果#include "stdio.h"int binary(int x, int a[], int n){ int low=0,high=n-l,mid; while(low<=high) { mid=(low+high)/2; if(x>a[mid]) high=mid-l; else if(x<a[mid]) low=mid+l; else return(mid); } void main(){ static int a[]={4,0,2,3,1}; int i,t,j; for(i=1;i<5;i++) t=a[i]; j=i-l; while(j>=0 && t>a[j]) { a[j+1]=a[j]; j--;} a[j+1]=t; } printf("%d\n",binary(3,a,5)); }