问题21:
有一个3×4的矩阵,要求编写程序找出每一行中最大值并与第一列交换。(20分)
问题22:
用指针实现:把一个整数插入到由小到大排列的数列中,插入后仍然保持由小到大的顺序。(20分)
问题21实现代码:
/* author:宋金时 */ /* date:2011/11/04 */ #include "Stdio.h" #include "Conio.h" #define HANG 3 #define LIE 4 int main(void) { /* 此处添加你自己的代码 */ int arr_num[HANG][LIE],m_i,m_j,m_max; puts("please a 3*4 number:"); for(m_i=0;m_i<HANG;m_i++) { for(m_j=0;m_j<LIE;m_j++) { scanf("%d",&arr_num[m_i][m_j]); } } for(m_i=0;m_i<HANG;m_i++) { m_max = 0; for(m_j=0;m_j<LIE;m_j++) { if(arr_num[m_i][m_j]>arr_num[m_i][m_max]) { m_max = m_j; } } arr_num[m_i][0]^=arr_num[m_i][m_max]; arr_num[m_i][m_max]^=arr_num[m_i][0]; arr_num[m_i][0]^=arr_num[m_i][m_max]; for(m_j=0;m_j<LIE;m_j++) { printf("%d ",arr_num[m_i][m_j]); } printf("\n"); } getch(); return 0; }
问题22实现代码:
/* author:宋金时 */ /* date:2011/11/04 */ #include "Stdio.h" #include "Conio.h" #define N 20 #define INIT_COUNT 5 int g_count=INIT_COUNT; void insert(int arr_num[],int num); int main(void) { /* 此处添加你自己的代码 */ int m_arr[N]={1,2,3,4,5},m_input; puts("please input insert's number:"); scanf("%d",&m_input); insert(m_arr,m_input); getch(); return 0; } void insert(int arr_num[],int num) { int m_i,m_pos=-1; int* m_p = arr_num; for(m_i=0;m_i<g_count;m_i++) { if(num<=*(m_p+m_i)) { m_pos = m_i; break; } } if(m_pos!=-1) { for(m_i=g_count;m_i>m_pos;m_i--) { *(m_p+m_i) = *(m_p+m_i-1); } *(m_p+m_pos) = num; } else { *(m_p+g_count) = num; } g_count++; puts("inserted data:"); for(m_i=0;m_i<g_count;m_i++) { printf("%d ",*(m_p+m_i)); } printf("\n"); }