在1至50中,取出7个数怎么算出奇数的个数及其值?现有以下问题。求1 2 3 4 5 6 7这7个数能够组成的奇数个数以及其值
#include <iostream>
#include <algorithm>
using namespace std;
bool is_odd(int x) {
return x % 2 == 1;
}
int main() {
int nums[] = {1, 2, 3, 4, 5, 6, 7};
int cnt = 0;
do {
if (is_odd(nums[0])) {
cnt++;
for (int i = 0; i < 7; i++)
cout << nums[i];
cout << endl;
}
} while (next_permutation(nums, nums + 7)); // 枚举所有排列
cout << "奇数的个数为:" << cnt << endl;
return 0;
}
不知道你这个问题是否已经解决, 如果还没有解决的话:给定N个(长整型范围内的)整数,要求输出从小到大排序后的结果。
本题旨在测试各种不同的排序算法在各种数据情况下的表现。各组测试数据特点如下:
数据1:只有1个元素;
数据2:11个不相同的整数,测试基本正确性;
数据3:103个随机整数;
数据4:104个随机整数;
数据5:105个随机整数;
数据6:105个顺序整数;
数据7:105个逆序整数;
数据8:105个基本有序的整数;
数据9:105个随机正整数,每个数字不超过1000。
加粗样式输入格式:
输入第一行给出正整数N(≤10^5),随后一行给出N个(长整型范围内的)整数,其间以空格分隔。
输出格式:
在一行中输出从小到大排序后的结果,数字间以1个空格分隔,行末不得有多余空格。
输入样例:
11
4 981 10 -17 0 -20 29 50 8 43 -5
输出样例:
-20 -17 -5 0 4 8 10 29 43 50 981
//heapsort
#include<stdio.h>
#include<stdlib.h>
typedef int KeyType;
typedef struct {
KeyType *elem;/*elem[0]一般作哨兵或缓冲区*/
int Length;
}SqList;
void CreatSqList(SqList &H);/*待排序列建立*/
void HeapAdjust(SqList &H,int head ,int tail);
void HeapSort(SqList &H);
int main()
{
SqList H;
int i;
CreatSqList(H);
HeapSort(H);
printf("%d",H.elem[1]);
for(i=2;i<=H.Length;i++)
{
printf(" %d",H.elem[i]);
}
printf("\n");
return 0;
}
void CreatSqList(SqList &H)
{
int i,n;
scanf("%d",&n);
if(n<=0) return;
else
{
H.elem=(KeyType*)malloc(sizeof(KeyType)*n);
H.Length=0;
for(i=1;i<=n;i++) //0号作为哨兵
{
scanf("%d",&H.elem[i]);
H.Length++;
}
}
}
void HeapAdjust(SqList &H,int head ,int tail)
{
//H内存中是顺序表,逻辑上是二叉树、堆
int rc=H.elem[head];
int lchild=2*head;
int rchild=2*head+1;
int gchild;//大孩子
while(lchild<=tail)//只要左孩子存在
{
if(rchild<=tail&&H.elem[lchild]<H.elem[rchild])
gchild=rchild;
else gchild=lchild;
if(rc<H.elem[gchild])
{
H.elem[head]=H.elem[gchild];
head=gchild;
lchild=2*head;
rchild=2*head+1;
}
else break;
}
H.elem[head]=rc;
}
void HeapSort(SqList &H)
{
int i;
for(i=H.Length/2;i>0;--i)
{
HeapAdjust(H,i,H.Length);
}
for(i=1;i<=H.Length-1;i++)
{
int t=H.elem[1];
H.elem[1]=H.elem[H.Length-i+1];
H.elem[H.Length-i+1] = t;
HeapAdjust(H,1,H.Length-i);
}
}
//shellsort
//#include<stdio.h>
//#include<stdlib.h>
//typedef int KeyType;
//typedef struct {
// KeyType *elem; /*elem[0]一般作哨兵或缓冲区*/
// int Length;
//}SqList;
//void CreatSqList(SqList *L);/*待排序列建立*/
//void ShellInsert(SqList L,int dk);
//void ShellSort(SqList L);
//
//int main()
//{
// SqList L;
// int i;
// CreatSqList(&L);
// ShellSort(L);
// printf("%d",L.elem[1]);
// for(i=2;i<=L.Length;i++)
// {
// printf(" %d",L.elem[i]);
// }
// printf("\n");
//
// return 0;
//}
//void CreatSqList(SqList *L)
//{
// int i,n;
// scanf("%d",&n);
// if(n<=0) return;
// else
// {
// L->elem=(KeyType*)malloc(sizeof(KeyType)*n);
// L->Length=0;
// for(i=1;i<=n;i++)//0号作为哨兵
// {
// scanf("%d",&L->elem[i]);
// L->Length++;
// }
// }
//}
//void ShellSort(SqList L)
//{
// /*按增量序列dlta[0…t-1]对顺序表L作Shell排序,假设规定增量序列为5,3,1*/
// int k;
// int dlta[3]={5,3,1};
// int t=3;
// for(k=0;k<t;++k)
// ShellInsert(L,dlta[k]);
//}
//void ShellInsert(SqList L,int dk)
//{
// int i,j;
// for(i=dk+1;i<=L.Length;i++)
// {
// if(*(L.elem+i)<*(L.elem+i-dk))
// {
// *(L.elem)=*(L.elem+i);//备份无序子序列首元素
// for(j=i-dk;j>0&&*(L.elem+j)>*(L.elem);j-=dk)
// {
// *(L.elem+j+dk)=*(L.elem+j);
// }
// *(L.elem+j+dk)=*(L.elem);
// }
// }
//}
//sort
//#include<stdio.h>
//#include<stdlib.h>
//#include<iostream>
//#include <algorithm>
//using namespace std;
//
//int main()
//{
// int i,n;
// scanf("%d",&n);
// int a[100000];
// for(i=1;i<=n;i++)
// {
// scanf("%d",&a[i]);
// }
// sort(a+1,a+n+1);
// printf("%d",a[1]);
// for(i=2;i<=n;i++)
// {
// printf(" %d",a[i]);
// }
// printf("\n");
// return 0;
//}
//quicksort
#include<stdio.h>
#include<stdlib.h>
typedef int KeyType;
typedef struct {
KeyType *elem;/*elem[0]一般作哨兵或缓冲区*/
int Length;
}SqList;
void CreatSqList(SqList *L);/*待排序列建立*/
int Partition(SqList L,int low,int high);
void QSort(SqList L,int low,int high);
int main()
{
SqList L;
int i;
CreatSqList(&L);
QSort(L,1,L.Length);
printf("%d",L.elem[1]);
for(i=2;i<=L.Length;i++)
{
printf(" %d",L.elem[i]);
}
printf("\n");
return 0;
}
void CreatSqList(SqList *L)
{
int i,n;
scanf("%d",&n);
if(n<=0) return;
else
{
L->elem=(KeyType*)malloc(sizeof(KeyType)*n);
L->Length=0;
for(i=1;i<=n;i++) //0号作为哨兵
{
scanf("%d",&L->elem[i]);
L->Length++;
}
}
}
int Partition(SqList L,int low,int high)
{
L.elem[0]=L.elem[low];
while(low<high)
{
while(high>low&&L.elem[high]>=L.elem[0])
--high;//high左移
L.elem[low]=L.elem[high];
while(low<high&&L.elem[low]<=L.elem[0])
++low;
L.elem[high]=L.elem[low];
}
L.elem[low]=L.elem[0];
return low;
}
void QSort(SqList L,int low,int high)
{
if(!(low<high)) return;
else
{
int pivotloc=Partition(L,low,high);
QSort(L,low,pivotloc-1);
QSort(L,pivotloc+1,high);
}
}