64,282
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
char ou[6] = {'0','2','4','6','8','\0'};
char ji[6] = {'1','3','5','7','9','\0'};
ofstream out("全偶.txt");
int cnt = 0;
for (int a=0; a<5; a++)
{
for (int b=0; b<5; b++)
{
for (int c=0; c<5; c++)
{
for (int d=0; d<5; d++)
{
for (int e=0; e<5; e++)
{
char temp[6] = {ou[a], ou[b], ou[c], ou[d], ou[e], '\0'};
cout << temp << endl;
out << temp << ends;
cnt++;
}
}
}
}
}
out.close();
cout << "总数:" << cnt << endl;
out.open("全奇.txt");
cnt = 0;
for (int a=0; a<5; a++)
{
for (int b=0; b<5; b++)
{
for (int c=0; c<5; c++)
{
for (int d=0; d<5; d++)
{
for (int e=0; e<5; e++)
{
char temp[6] = {ji[a], ji[b], ji[c], ji[d], ji[e], '\0'};
cout << temp << endl;
out << temp << ends;
cnt++;
}
}
}
}
}
out.close();
cout << "总数:" << cnt << endl;
return 0;
}
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
int i;
int L;
void main(int argc,char **argv) {
if (argc<2) {
printf("%s 要排列的字符串\n",argv[0]);
return;
}
L=strlen(argv[1]);
vector<char> P(L);
vector<char>::iterator b,e,it;
b=P.begin();
e=P.end();
for (i=0;i<L;i++) P[i]=argv[1][i];
sort(b,e);
i=0;
do {
printf("%10d: ",++i);
for(it=b;it!=e;it++) printf("%c",*it);
printf("\n");
} while (next_permutation(b,e));
printf("The total: %d",i);
}
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
#define N 10
#define M 5
int number[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int temp[M];
int total = 0;
// 会在E盘根目录下生成32个文件,比如_00000.txt表示全偶数排列;_00001表示前4个数为偶数,最后一个数为奇数的排列,以此类推
void write_file()
{
++total;
string filename = "E:/_";
for(int i = 0; i < M; ++i)
{
if(temp[i] % 2 == 0)
{
filename += "0";
}
else
{
filename += "1";
}
}
filename += ".txt";
ofstream fos(filename, ios::app);
if(!fos)
{
cout << "can not open file..." << endl;
exit(1);
}
for(int i = 0; i < M; ++i)
{
fos << temp[i] << " ";
}
fos << endl;
fos.close();
}
void move(int offset, int i) /* 交换两个数 */
{
int another;
another = number[offset];
number[offset] = number[i];
number[i] = another;
}
void permutation(int offset, int count) /* 排列 */
{
int i;
if(count == 0)
{
write_file();
return;
}
else
{
for(i = offset; i < N; i++)
{
temp[offset] = number[i];
move(offset, i);
permutation(offset+1, count-1);
move(offset, i);
}
}
}
int main()
{
int start = 0;
int count = M;
permutation(start, M); // 从10个数中,取M个数的排列
cout << total << endl;
}
#include <stdlib.h>
#include <stdio.h>
using namespace std;
typedef struct tag_MODE
{
bool bFir;
bool bSec;
bool bThi;
bool bFou;
bool bFiv;
}MODE;
int nNum[5] = {0};
void getKey()
{
for ( int i = 0 ; i < 5 ; ++i )
{
nNum[i] = rand()%10;
for ( int j = 0 ; j < i ; ++j )
{
if ( nNum[j] == nNum[i] )
{
i--;
goto MID;
}
}
MID:
;
}
}
int main()
{
getKey();
//进行全排列,并对每种排列归类进MODE……
return 0;
}
#include<stdio.h>
#include <fstream>
using namespace std;
int main()
{
ofstream out[32];
int cnt[32] ={0};
int i =0;
for (i =0;i<32;i++)
{
char szBuffer[32]={'0'};
int nMask =0x01;
for (int j=4;j>=0;j--)
{
if (i&nMask)
szBuffer[j]='1';
//else//这个可以去掉
//szBuffer[j]='0';
nMask<<=1;
}
strcpy(szBuffer+5,".txt");
out[i].open(szBuffer);
}
for (i =0;i<3125*32;i++)//i的范围为0到3125*32。
{
int j =0;
char szBuffer[32];
sprintf(szBuffer,"%05d",i);
int type =0;
for (j =0;j<5;j++)
{
if (szBuffer[j]%2!=0)
type|=1<<j;
}
out[type] <<szBuffer;
out[type] <<"\n";
cnt[type]++;
}
for (i =0;i<32;i++)
{
out[i] <<cnt[i];
}
return 0;
}
void fun()
{
for (int i=0; i<32; i++)
{
for (int j=0; j<5; j++)
{
printf("%s", (i >> j) & 1 ? "奇" : "偶");
}
printf("\n");
}
}