c语言程序设计,数组编号,求思路
从键盘输入10个整数存放在数组a中,然后对数组a中的10个整数按从小到大连续编号,要求不能改变数组a中元素的顺序,且相同的整数要具有相同的编号。最后输出数组a及其元素的编号。例如:
输入:
5 3 4 7 3 5 6 9 21 100
则输出为:
5 3 4 7 3 5 6 9 21 100
3 1 2 5 1 3 4 6 7 8
程序代码:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define COUNT 10
#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))
typedef struct
{
int value;
int original_order;
int sorted_order;
} value_order_t;
void display_array(int * array, size_t size)
{
printf("[ ");
for(int i = 0; i < size; i++)
{
printf("%d ", array[i]);
}
printf("]\n");
}
int main(void)
{
const int array_count = COUNT;
int input_array[array_count];
value_order_t value_order_array[array_count];
int order_array[array_count];
printf("Please input integer array: ");
if(scanf("%d %d %d %d %d %d %d %d %d %d", &input_array[0], &input_array[1], &input_array[2], &input_array[3], &input_array[4], &input_array[5], &input_array[6], &input_array[7], &input_array[8], &input_array[9])!= 10)
{
fprintf(stderr, "Size of integer array isn't %d", array_count);
return EXIT_FAILURE;
}
printf("\n");
printf("Original integer array: ");
display_array(input_array, SIZE_OF_ARRAY(input_array));
for(int i = 0; i < array_count; i++)
{
value_order_array[i].value = input_array[i];
value_order_array[i].original_order = i;
value_order_array[i].sorted_order = -1;
}
for(int i = 0; i < array_count - 1; i++)
{
for(int j = i; j < array_count; j++)
{
if(value_order_array[i].value > value_order_array[j].value)
{
value_order_t temp = value_order_array[i];
value_order_array[i] = value_order_array[j];
value_order_array[j] = temp;
}
}
}
value_order_array[0].sorted_order = 0;
for(int i = 1; i < array_count; i++)
{
if(value_order_array[i].value == value_order_array[i - 1].value)
{
value_order_array[i].sorted_order = value_order_array[i - 1].sorted_order;
}
else
{
value_order_array[i].sorted_order = value_order_array[i - 1].sorted_order + 1;
}
}
for(int i = 0; i < array_count; i++)
{
for(int j = 0; j < array_count; j++)
{
if(value_order_array[j].original_order == i)
{
order_array[i] = value_order_array[j].sorted_order + 1;
}
}
}
printf("Sorted order: ");
display_array(order_array, SIZE_OF_ARRAY(order_array));
return EXIT_SUCCESS;
}[此贴子已经被作者于2019-12-22 16:39编辑过]
程序代码:#include <cstdio>
#include <utility>
#include <algorithm>
using namespace std;
void foo( const int (&a)[10] )
{
pair<int,int> b[] = { {a[0],1}, {a[1],2}, {a[2],3}, {a[3],4}, {a[4],5}, {a[5],6}, {a[6],7}, {a[7],8}, {a[8],9}, {a[9],10} };
sort( begin(b), end(b), [](auto& lhs,auto& rhs){return lhs.first<rhs.first;} );
int index=1, pre=b[0].first;
for( auto& value : b )
{
if( value.first != pre )
{
pre = value.first;
++index;
}
value.first = index;
}
sort( begin(b), end(b), [](auto& lhs,auto& rhs){return lhs.second<rhs.second;} );
for( size_t i=0; i!=size(a); ++i )
printf( "%d%c", a[i], " \n"[i+1==size(a)] );
for( size_t i=0; i!=size(b); ++i )
printf( "%d%c", b[i].first, " \n"[i+1==size(b)] );
}
int main( void )
{
int a[] = { 5, 3, 4, 7, 3, 5, 6, 9, 21, 100 };
foo( a );
}
程序代码:#include <stdio.h>
#include <stdlib.h>
int cmp_b_first( const void* pl, const void* pr )
{
if( *(int*)pl < *(int*)pr )
return -1;
if( *(int*)pl > *(int*)pr )
return +1;
return 0;
}
void foo( const int a[static 10] )
{
int b[][2] = { {a[0],1}, {a[1],2}, {a[2],3}, {a[3],4}, {a[4],5}, {a[5],6}, {a[6],7}, {a[7],8}, {a[8],9}, {a[9],10} };
qsort( b, 10, sizeof(*b), &cmp_b_first );
int index=1, pre=b[0][0];
for( size_t i=0; i!=10; ++i )
{
if( b[i][0] != pre )
{
pre = b[i][0];
++index;
}
b[i][0] = index;
}
int c[10];
for( size_t i=0; i!=10; ++i )
c[b[i][1]-1] = b[i][0];
for( size_t i=0; i!=10; ++i )
printf( "%d%c", a[i], " \n"[i+1==10] );
for( size_t i=0; i!=10; ++i )
printf( "%d%c", c[i], " \n"[i+1==10] );
}
int main( void )
{
int a[] = { 5, 3, 4, 7, 3, 5, 6, 9, 21, 100 };
foo( a );
}