我想输出 1 4 2 3 5 奇数升序和偶数降序
I want to output 1 4 2 3 5 odd number ascending order and even numbers in descending order
我想按升序输出奇数,按降序输出偶数我的代码在这里。
当我给输入n=5
数组5 2 4 3 1
时,我要输出1 4 2 3 5
,
但我得到了输出 4 2 1 3 5
。我不要数组的位置。
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; ++i)
{
cin >> arr[i];
}
// odd number ascending order
for (int i = 0; i <= n; ++i)
{
for (int j = i + 1; j < n; ++j)
{
if (arr[i] < arr[j])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for (int i = 0; i < n; ++i)
{
if (arr[i] % 2 == 0)
{
cout << arr[i]<< " ";
}
}
// numbers in descending order
for (int i = 0; i <= n - 1; i++)
{
for (int j = i + 1; j <= n; j++)
{
if (arr[j] < arr[i])
{
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
if (arr[i] % 2 == 1)
{
cout << arr[i]<<" ";
}
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n = 5;
int left = 1, right = n - (n % 2);
for(int i = 1; i <= n; i++)
{
if(i % 2 == 0)
{
cout << right << ' ';
right-= 2;
}
else if(i % 2 == 1)
{
cout << left << ' ';
left += 2;
}
}
return 0;
}
我们初学者应该互相帮助。:)
对于你我这样的初学者来说,这项任务并不容易。
我可以建议一种基于冒泡排序方法的方法。
给你。
#include <iostream>
#include <utility>
template <typename UnaryPredicate>
void conditional_bubble_sort( int a[], size_t n,
UnaryPredicate &&unary_predicate,
bool order = false )
{
while ( n && !unary_predicate( *a ) )
{
++a;
--n;
}
for ( size_t i = 0, last = 0; !( n < 2 ); n = last )
{
last = 0;
for ( size_t current = 0, next = 0; next < n; current = next )
{
while ( ++next < n && !unary_predicate( a[next] ) );
if ( next != n )
{
if ( !order /* ascending */ ? a[next] < a[current] : a[current] < a[next] )
{
std::swap( a[current], a[next] );
last = next;
}
}
}
}
}
int main()
{
int a[] = { 5, 2, 4, 3, 1 };
const size_t N = sizeof( a ) / sizeof( *a );
for ( const auto &item : a )
{
std::cout << item << ' ';
}
std::cout << '\n';
conditional_bubble_sort( a, N, []( const auto &item ) { return item % 2 != 0;} );
for ( const auto &item : a )
{
std::cout << item << ' ';
}
std::cout << '\n';
conditional_bubble_sort( a, N, []( const auto &item ) { return item % 2 == 0;}, true );
for ( const auto &item : a )
{
std::cout << item << ' ';
}
std::cout << '\n';
return 0;
}
程序输出为
5 2 4 3 1
1 2 4 3 5
1 4 2 3 5
我想按升序输出奇数,按降序输出偶数我的代码在这里。
当我给输入n=5
数组5 2 4 3 1
时,我要输出1 4 2 3 5
,
但我得到了输出 4 2 1 3 5
。我不要数组的位置。
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; ++i)
{
cin >> arr[i];
}
// odd number ascending order
for (int i = 0; i <= n; ++i)
{
for (int j = i + 1; j < n; ++j)
{
if (arr[i] < arr[j])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for (int i = 0; i < n; ++i)
{
if (arr[i] % 2 == 0)
{
cout << arr[i]<< " ";
}
}
// numbers in descending order
for (int i = 0; i <= n - 1; i++)
{
for (int j = i + 1; j <= n; j++)
{
if (arr[j] < arr[i])
{
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
if (arr[i] % 2 == 1)
{
cout << arr[i]<<" ";
}
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n = 5;
int left = 1, right = n - (n % 2);
for(int i = 1; i <= n; i++)
{
if(i % 2 == 0)
{
cout << right << ' ';
right-= 2;
}
else if(i % 2 == 1)
{
cout << left << ' ';
left += 2;
}
}
return 0;
}
我们初学者应该互相帮助。:)
对于你我这样的初学者来说,这项任务并不容易。
我可以建议一种基于冒泡排序方法的方法。
给你。
#include <iostream>
#include <utility>
template <typename UnaryPredicate>
void conditional_bubble_sort( int a[], size_t n,
UnaryPredicate &&unary_predicate,
bool order = false )
{
while ( n && !unary_predicate( *a ) )
{
++a;
--n;
}
for ( size_t i = 0, last = 0; !( n < 2 ); n = last )
{
last = 0;
for ( size_t current = 0, next = 0; next < n; current = next )
{
while ( ++next < n && !unary_predicate( a[next] ) );
if ( next != n )
{
if ( !order /* ascending */ ? a[next] < a[current] : a[current] < a[next] )
{
std::swap( a[current], a[next] );
last = next;
}
}
}
}
}
int main()
{
int a[] = { 5, 2, 4, 3, 1 };
const size_t N = sizeof( a ) / sizeof( *a );
for ( const auto &item : a )
{
std::cout << item << ' ';
}
std::cout << '\n';
conditional_bubble_sort( a, N, []( const auto &item ) { return item % 2 != 0;} );
for ( const auto &item : a )
{
std::cout << item << ' ';
}
std::cout << '\n';
conditional_bubble_sort( a, N, []( const auto &item ) { return item % 2 == 0;}, true );
for ( const auto &item : a )
{
std::cout << item << ' ';
}
std::cout << '\n';
return 0;
}
程序输出为
5 2 4 3 1
1 2 4 3 5
1 4 2 3 5