關于如何從用戶輸入中隱藏偶數并僅按升序列印奇數的任何建議?就像這個輸出描述的一樣:
5
3
2
8
7
OUTPUT:
3
5
7
Press any key to continue . . .
我一直試圖在幾個小時內弄清楚,但無法找到解決方案:(。
#include <stdio.h>
#include <time.h>
void sort(int number[], int count)
{
int temp, i, j, k;
for (j = 0; j < count; j)
{
for (k = j 1; k < count; k)
{
if (number[j] > number[k])
{
temp = number[j];
number[j] = number[k];
number[k] = temp;
}
}
}
printf("OUTPUT:\n");
for (i = 0; i < count; i)
printf("%d\n", number[i]);
}
void main()
{
int i, number[1000];
int count = 5;
printf("\nType your number:");
for (i = 0; i < count; i)
scanf("%d", &number[i]);
sort(number, count);
}
uj5u.com熱心網友回復:
只需在您的程式中添加 'if(number[i]%2==0)'。
#include <stdio.h>
#include <time.h>
void sort(int number[], int count)
{
int temp, i, j, k;
for (j = 0; j < count; j)
{
for (k = j 1; k < count; k)
{
if (number[j] > number[k])
{
temp = number[j];
number[j] = number[k];
number[k] = temp;
}
}
}
printf("OUTPUT:\n");
for (i = 0; i < count; i)
if(number[i]%2!=0)
printf("%d\n", number[i]);
}
void main()
{
int i, number[1000];
int count = 5;
printf("\nType your number:");
for (i = 0; i < count; i)
scanf("%d", &number[i]);
sort(number, count);
}
uj5u.com熱心網友回復:
嘗試使用以下代碼將解決您的問題:
#include <stdio.h>
#include <time.h>
void sort(int number[], int count)
{
int temp, i, j, k;
int numbs[100];
int counter = 0;
for(int h=0; h<count; h){
if(number[h] % 2 != 0){
numbs[counter] = number[h];
counter ;
}
}
for (j = 0; j < counter; j)
{
for (k = j 1; k < count; k)
{
if (numbs[j] > numbs[k])
{
temp = numbs[j];
numbs[j] = numbs[k];
numbs[k] = temp;
}
}
}
printf("OUTPUT:\n");
for (i = 0; i < counter; i)
printf("%d\n", numbs[I]);
}
void main()
{
int i, number[1000];
int count = 5;
printf("\nType your number:");
for (i = 0; i < count; i)
scanf("%d", &number[i]);
sort(number, count);
}
uj5u.com熱心網友回復:
有很多方法可以完成任務,以下可能是矯枉過正。
考慮按升序qsort對陣列進行排序的介面和用法。int
#include <stdlib.h>
// It needs a function that compares the values
int cmp_int(void const *lhs, void const *rhs)
{
int const a = *(int *)lhs;
int const b = *(int *)rhs;
return (b < a) - (a < b);
}
int main(void)
{
int a[] = {42, 17, -3, 0, 8, -2, 33};
size_t const a_size = (sizeof a) / (sizeof a[0]);
qsort(a, a_size, // The source array and the number of its elements.
sizeof(a[0]), // The size of each element.
cmp_int); // The pointer to the comparator function.
// ...
}
您可以調整相同的概念并撰寫一個僅列印奇數元素的函式。
#include <stdbool.h>
#include <stdio.h>
bool is_odd(int x)
{
return (x % 2) != 0;
}
void print_if(char const *fmt,
size_t n, int const *a,
bool (*predicate)(int))
{
for (size_t i = 0; i < n; i)
{
if ( predicate(a[i]) )
printf(fmt, a[i]);
}
putchar('\n');
}
int main(void)
{
int a[] = //...
size_t const a_size = //...
// ...
// Sort 'a', hopefully using qsort.
print_if("%d ", a_size, a, is_odd);
// ...
}
作為替代方案,您可以僅將奇數元素復制到另一個(大小合適的)陣列中,對其進行排序并全部列印出來。
size_t copy_if(size_t n, int const *src,
int *dst,
bool (*predicate)(int))
{
size_t j = 0;
for ( size_t i = 0; i < n; i )
{
if ( predicate( src[i] ) )
{
dst[j] = src[i];
j;
}
}
return j; // Note that the number of elements copied is returned. Use it!
}
實時示例 @Compiler Explorer。
uj5u.com熱心網友回復:
對于根據 C 標準的初學者,不帶引數的函式 main 應宣告為
int main( void )
由于宣告的陣列有 1000 個元素,因此這意味著用戶可以輸入不大于 1000 的任意數量的值。否則,宣告一個具有 1000 個元素的陣列只輸入5數字是沒有意義的。那就是你應該問用戶他要輸入多少個整數。
您的排序功能不區分奇數和偶數。它嘗試對陣列的所有元素進行排序,盡管您似乎只需要對奇數值的元素進行排序。
要僅對陣列奇數值的元素進行排序,您可以使用冒泡排序演算法。
此外,您應該在兩個單獨的函式中拆分排序和輸出具有奇數值的元素。
這是一個演示程式,展示了如何執行該任務。
#include <stdio.h>
void sort_odds( int a[], size_t n )
{
size_t i = 0;
while ( i != n && a[i] % 2 == 0 ) i ;
if ( i != n )
{
a = i;
n -= i;
for ( size_t last = 0; !( n < 2 ); n = last )
{
last = 0;
size_t previous = 0;
for ( size_t j = 0; j < n; j )
{
if ( a[j] % 2 == 1 )
{
if ( a[j] < a[previous] )
{
int tmp = a[j];
a[j] = a[previous];
a[previous] = tmp;
last = j;
}
previous = j;
}
}
}
}
}
void display_odds( const int a[], size_t n )
{
for ( size_t i = 0; i < n; i )
{
if ( a[i] % 2 == 1 ) printf( "%d ", a[i] );
}
putchar( '\n' );
}
int main(void)
{
enum { N = 1000 };
int number[N];
size_t count = 0;
printf( "Enter the number of integers you want to input (no more than %d): ", N );
scanf( "%zu", &count );
if ( count != 0 )
{
if ( N < count ) count = N;
printf( "Enter your integers: " );
int num;
size_t i = 0;
for ( ; scanf( "%d", &num ) == 1 && i < count; i )
{
number[i] = num;
}
sort_odds( number, i );
display_odds( number, i );
}
}
程式輸出可能看起來像
Enter the number of integers you want to input (no more than 1000): 10
Enter your integers: 9 8 7 6 5 4 3 2 1 0
1 3 5 7 9
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/430958.html
下一篇:按日期映射和排序資料
