c数组问题

李万鹏 2009-10-28 02:14:20
将一个5*5的矩阵中最大的元素放在中心,四个角分别放4个最小的元素(顺序从左到右,从上到下一次从小到大存放);
下面是我的程序,请问哪里出错了
#include <stdio.h>
int main(){
int a[5][5],i,j,t,m = 0,n = 0,b[25],k = 0,h;
for(i = 0;i < 5;i++)
for(j = 0;j < 5;j++)
scanf("%d",&a[i][j]);
for(i = 0;i < 5;i++)
for(j = 0;j < 5;j++)
if(a[i][j] > a[m][n]){
m = i;
n = j;
}
t = a[2][2];
a[2][2] = a[m][n];
a[m][n] = t;
for(i = 0;i < 5;i++)
for(j = 0;j < 5;j++)
b[k++] = a[i][j];
for(i = 0;i < 24;i++){
h = i;
for(j = i + 1;j < 25;j++)
if(b[h] > b[j])
h = j;
t = b[i];
b[i] = b[h];
b[h] = t;
}
for(i = 0;i < 4;i++)
printf("%d\001 ",b[i]);
k = 0,m = 0,n = 0,t = 0;
for(i = 0;i < 5;i++)
for(j = 0;j < 5;j++)
if(a[i][j] == b[0]){
printf("%d",a[i][j]);
t = a[i][j];
a[i][j] = a[m][n];
a[m][n] = t;
if(k - 1 == 1){
m = 0;
n = 4;
}
else
if(k - 1 == 2){
m = 4;
n = 0;
}
else
if(k - 1 == 3){
m = 4;
n = 4;
}
}
for(i = 0;i < 5;i++){
for(j = 0;j < 5;j++)
printf("%d ",a[i][j]);
printf("\n");
}
getch();
return 0;
}

...全文
149 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
李万鹏 2009-10-28
  • 打赏
  • 举报
回复
我的问题在最上面,程序在3 楼
likee003 2009-10-28
  • 打赏
  • 举报
回复
没有看懂楼主的意思 ,是要全部排列还是要怎么排 ?
李万鹏 2009-10-28
  • 打赏
  • 举报
回复
是改成这样也不行啊
#include <stdio.h>
int main(){
int a[5][5],i,j,t,m = 0,n = 0,b[25],k = 0,h;
for(i = 0;i < 5;i++)
for(j = 0;j < 5;j++)
scanf("%d",&a[i][j]);
for(i = 0;i < 5;i++)
for(j = 0;j < 5;j++)
if(a[i][j] > a[m][n]){
m = i;
n = j;
}
t = a[2][2];
a[2][2] = a[m][n];
a[m][n] = t;
for(i = 0;i < 5;i++)
for(j = 0;j < 5;j++)
b[k++] = a[i][j];
for(i = 0;i < 24;i++){
h = i;
for(j = i + 1;j < 25;j++)
if(b[h] > b[j])
h = j;
t = b[i];
b[i] = b[h];
b[h] = t;
}
for(i = 0;i < 4;i++)
printf("%d\001 ",b[i]);
k = 0,m = 0,n = 0,t = 0;
for(i = 0;i < 5;i++)
for(j = 0;j < 5;j++)
if(a[i][j] == b[k]){
printf("%d",a[i][j]);
k++;
t = a[i][j];
a[i][j] = a[m][n];
a[m][n] = t;
if(k - 1 == 1){
m = 0;
n = 4;
}
else
if(k - 1 == 2){
m = 4;
n = 0;
}
else
if(k - 1 == 3){
m = 4;
n = 4;
}
}
for(i = 0;i < 5;i++){
for(j = 0;j < 5;j++)
printf("%d ",a[i][j]);
printf("\n");
}
getch();
return 0;
}


李万鹏 2009-10-28
  • 打赏
  • 举报
回复
改成这样也不对呀
#include <stdio.h>
#include <string.h>
#include <conio.h>
struct student{
char name[20];
char address[20];
long number;
long tel;
struct student * next;
};
struct student * creat(){
struct student * p = NULL,* q = NULL,* head = NULL;
if(head != NULL){
q = head;
while(q != NULL)
q = q->next;
q->next = NULL;
}
p = (struct student *)malloc(sizeof(struct student));
memset(p, 0, sizeof(struct student));
printf("please enter name:\n");
scanf("%s",p->name);
printf("please enter address:\n");
scanf("%s",p->address);
printf("please enter number and tel:\n");
scanf("%ld,%ld",&p->number,&p->tel);
while(1){
if(head == NULL)
head = q = p;
else{
q->next = p;
q = p;
}
p = (struct student *)malloc(sizeof(struct student));
memset(p, 0, sizeof(struct student));
printf("please enter name:\n");
scanf("%s",p->name);
if(strcmp(p->name,"over") == 0){
getchar();
break;
}
printf("please enter address:\n");
scanf("%s",p->address);
printf("please enter number and tel:\n");
scanf("%ld,%ld",&p->number,&p->tel);
}
q->next = NULL;
return head;
}
void print(struct student * head){
struct student * p = head;
while(p != NULL){
printf("%s,%s,%ld,%ld",p->name,p->address,p->number,p->tel);
printf("\n");
p = p->next;
}
}
void check(struct student * head){
struct student * p = head;
char name[20];
printf("please enter who you want to check:\n");
scanf("%s",name);
while(p != NULL){
if(strcmp(name,p->name) == 0){
printf("%s,%s,%ld,%ld\n",p->name,p->address,p->number,p->tel);
break;
}
else
p = p->next;
}
if(p == NULL)
printf("the person is not present");
}
void save(struct student * head){
struct student * p = head;
FILE * fp;
fp = fopen("jilu.txt","a");
while(p != NULL){
fprintf(fp,"%s,%s,%ld,%ld\n",p->name,p->address,p->number,p->tel);
p = p->next;
}
fclose(fp);
printf("save successfully!");
}
struct student * import(){
FILE * fp;
struct student * head = NULL, * q = NULL, * p = NULL;
if((fp = fopen("jilu.txt","r")) == NULL)
return head = NULL;
else{
do{
p = (struct student *)malloc(sizeof(struct student));
memset(p, 0, sizeof(struct student));
fscanf(fp,"%s,", p->name);
fscanf(fp,"%s,", p->address);
fscanf(fp,"%ld,%ld", &p->number, &p->tel);
if(head == NULL)
head = q = p;
else{
q->next = p;
q = p;
}
}while(!feof(fp));
q->next = NULL;
}
fclose(fp);
return head;
}
int main(){
struct student * head = NULL;
char choice1,choice2,choice3;
head = import();
clrscr();
printf(" \002 Communication management system\002 \n");
printf(" \001 1.add information \001\n");
printf(" \001 2.check information \001\n");
printf(" \001 3.display information\001\n");
printf(" \001 4.quit \001\n");
printf("please enter your choice1:\n");
scanf("%c",&choice1);
do{
switch(choice1){
case '1':head = creat();break;
case '2':check(head);break;
case '3':print(head);break;
case '4':
printf("1.quit with save 2.quit without save\n");
printf("please enter your choice2:\n");
scanf("%c",&choice2);
switch(choice2){
case '1':save(head);exit(0);break;
case '2':exit(0);break;
}
}
printf("please enter your choice3:\n");
scanf("%c",&choice1);
getchar();
}while(1);
getch();
return 0;
}









basketballn 2009-10-28
  • 打赏
  • 举报
回复
if(a[i][j] == b[0]){
printf("%d",a[i][j]);
t = a[i][j];
a[i][j] = a[m][n];
a[m][n] = t;
if(k - 1 == 1){
m = 0;
n = 4;
}
else
if(k - 1 == 2){
m = 4;
n = 0;
}
else
if(k - 1 == 3){
m = 4;
n = 4;
}
修改存放了最小的数之后,后面的IF语句有什么用?期间K的值并没有变化啊,开始你给K=0,好像不对这里

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧