所以我正在解决这个问题,我得到了这个输出
**但是我期望这样的输出**
我在这里犯了什么错误?我知道我的编码效率不高,我想知道如何使该代码更紧凑,因为我仍在学习这些内容,所以任何建议都将有所帮助。
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main()
{
int i,j,k,l,f=0,x=0,q=0,row,column,size;
int res[100];
printf("Enter the row: ");
scanf("%d",&row);
printf("Enter the column: ");
scanf("%d",&column);
size= row*column;
int **arr;
arr=(int**)malloc(row * sizeof(int));
for(i=0;i<row;i++)
{
arr[i] = (int*)malloc(column*sizeof(int));
}
for(i=0;i<row;i++)
{
for (j=0;j<column;j++)
{
printf("Enter the value at row %d and column %d : ",i,j);
scanf("%d",(*(arr+i)+j));
}
}
for(i=0;i<row;i++)
{
for (j=0;j<column;j++)
{
printf("\nThe value at row %d and column %d : %d",i,j,*(*(arr+i)+j));
}
}
printf("\nThe duplicate value(s) are:\n");
for(i=0; i<row; i++)
{
for(j=0; j<column; j++)
{
for(k=0; k<row; k++)
{
for(l=0; l<column; l++)
{
if(*(*(arr+i)+j)== *(*(arr+k)+l))
{
f=f+1;
}
if(f>1)
{
printf("%d in positions (%d,%d)",*(*(arr+i)+j),k,l);
}
}
}f=0;
}
}
free(arr);
}
int *
可用于对整数进行排序以标识重复项。
#include<stdio.h>
#include<stdlib.h>
int scanint ( int *value) {
int result = 0;
result = scanf ( "%d", value);//ampersand not needed. value is int *
if ( 0 == result) {//could not parse an int from input
while ( '\n' != ( result = getchar ( ))) {//read until newline
if ( EOF == result) {
fprintf ( stderr, "EOF\n");
exit ( EXIT_FAILURE);
}
}
return 0;
}
return 1;
}
int main() {
int i = 0;
int j = 0;
int row = 0;
int column = 0;
int size = 0;
int each = 0;
int check = 0;
int count = 0;
int match = 0;
int result = 0;
int line = 1;
int first = 1;
do {
printf ( "Enter the row: ");
fflush ( stdout);
result = scanint ( &row);
} while ( 0 == result);
do {
printf ( "Enter the column: ");
fflush ( stdout);
result = scanint ( &column);
} while ( 0 == result);
size = row * column;
int *sort = malloc ( sizeof *sort * row * column);
if ( NULL == sort) {
fprintf ( stderr, "problem malloc sort\n");
exit ( EXIT_FAILURE);
}
int (*arr)[column];//pointer to variable lenght array
arr = malloc ( sizeof *arr * row);
if ( NULL == arr) {
fprintf ( stderr, "problem malloc arr\n");
exit ( EXIT_FAILURE);
}
for ( i = 0; i < row; i++) {
for ( j = 0; j < column; j++) {
do {
printf ( "Enter the value at row %d and column %d : ", i, j);
fflush ( stdout);
result = scanint( &arr[i][j]);
} while ( 0 == result);
sort[i * column + j] = arr[i][j];//assign to sort
for ( each = i * column + j; each > 0; --each) {
if ( sort[each] < sort[each - 1]) {//sort the elements
int swap = sort[each];
sort[each] = sort[each - 1];
sort[each - 1] = swap;
}
else {
break;
}
}
}
}
for ( i = 0; i < row; i++) {
for ( j = 0; j < column; j++) {
printf ( "\nThe value at row %d and column %d : %d", i, j, arr[i][j]);
}
}
while ( check < size) {
match = 0;//assume no matches
count = 0;
while ( sort[check] == sort[check + 1]) {//duplicate
match++;//count matches
count++;//count matches
check++;//next element to check
}
if ( match) {//found matches
if ( first) {//first time through print statement
printf("\nThe duplicate value(s) are:\n");
first = 0;//prevent printing again
}
printf ( "%d. %d in positions ", line, sort[check]);//print start of line
++line;//next line
for ( i = 0; i < row; i++) {
for ( j = 0; j < column; j++) {
if ( sort[check] == arr[i][j]) {//found the match in arr
printf ( "(%d,%d)", i, j);//print indices
--count;
if ( ! count) {
printf ( " and ");
}
else {
if ( 1 < match && 0 < count) {
printf ( ", ");
}
}
}
}
}
printf ( "\n");
}
check++;
}
if ( first) {
printf("\nNo duplicates:\n");
}
free ( arr);
free ( sort);
}