69,379
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void fun(int *array,const unsigned num,const unsigned size)
{/*从size大小的array中随机选出num个数组,存放在array的前num个单元中*/
unsigned i, p;
int temp;
srand((unsigned)time(NULL));
for(i = 0; i!= num; ++i)
{
p = i + rand() % (size - i);/*交换的位置*/
if(p == i)
continue;
temp = array[i];
array[i] = array[p];
array[p] = temp;
}
}
int main()/*测试*/
{
unsigned i;
int array[35];
for(i = 0; i != 35; ++i)
array[i]= 1+i;/*在arry中存储35个要随机选出的数字*/
fun(array,8,35);
for(i = 0; i != 8; ++i)
printf("%i ", array[i]);
return 0;
}
#include <sys\stat.h>
#include <io.h>
#include <fcntl.h>
#include <share.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define MAX_CLU_BYTES 65536
FILE *fo;
int fh;
__int64 offs,offs1;
__int64 rvi64;
int rv,wrv;
unsigned char buf[MAX_CLU_BYTES];
char ofn[_MAX_PATH];
char offstr[80];
void strcpybutcomma(char *t,char *s) {
char c;
while (1) {
c = *s++;
if (','!=c) *t++ = c;
if (0==c) break;
}
}
void main(int argc,char **argv) {
if (argc<3) {
printf("Copy File Tail.\n");
printf("Usage:\n");
printf(" cft filename.ext offset_begin[-offset_end]\n");
printf("Copy filename.ext offset_begin[-offset_end] to offset_begin[-offset_end]-filename.ext\n");
printf("Note: Byte at offset_end is NOT included.\n");
printf("Example:\n");
printf(" cft abc.rar 12345\n");
printf("Copy abc.rar offset 12345-end to 12345-abc.rar\n");
printf(" cft abc.rar 123-12345\n");
printf("Copy abc.rar offset 123-12345 to 123-12345-abc.rar\n");
printf(" cft abc.rar 0xAB-0xCD\n");
printf("Copy abc.rar offset 0xAB-0xCD to 0xAB-0xCD-abc.rar\n");
return;
}
strcpybutcomma(offstr,argv[2]);
rv=sscanf(offstr,"%I64i-%I64i",&offs,&offs1);
if (rv==0) {
printf("offset %s is not number\n",argv[2]);
return;
}
fh=_sopen(argv[1],_O_BINARY|_O_RDONLY|_O_RANDOM,_SH_DENYWR);
if (fh==-1) {
printf("_sopen %s errno=%d\n",argv[1],errno);
return;
}
if (rv==1) {
offs1=_filelengthi64(fh);
if (offs1==-1i64) {
printf("%I64=_filelengthi64 errno=%d\n",offs1,errno);
_close(fh);
return;
}
} else {//rv==2
if (offs1<offs) {
printf("%s offset_begin>offset_end error\n",argv[2]);
_close(fh);
return;
}
}
rvi64=_lseeki64(fh,offs,SEEK_SET);
if (rvi64!=offs) {
printf("%I64u=_lseeki64 %I64u errno=%d\n",rvi64,offs,errno);
_close(fh);
return;
}
sprintf(ofn,"%s-",offstr);
strcat(ofn,argv[1]);
fo=fopen(ofn,"wb");
if (fo==NULL) {
_close(fh);
printf("fopen %s error\n",ofn);
return;
}
cprintf("\n%I64u\r",offs);
while (1) {
rv=_read(fh,buf,(unsigned int)__min(offs1-offs,MAX_CLU_BYTES));
if (rv==0) break;//
if (rv<0) {
fclose(fo);
_close(fh);
printf("_read %s offset %I64u error\n",argv[1],offs);
return;
}
wrv=fwrite(buf,1,rv,fo);
if (wrv!=rv) {
fclose(fo);
_close(fh);
printf("fwrite %s error\n",ofn);
return;
} else {
offs+=rv;
cprintf("%I64u\r",offs);
if (offs>=offs1) break;//
}
}
fclose(fo);
_close(fh);
printf("Copy %s offset %s to %s OK.\n",argv[1],argv[2],ofn);
}
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void foo(int a[], int n, int m)
{
srand(time(0));
for (int i = 0; i != n; ++i)
{
if (rand() % (n - i) < m)
{
printf("%d ", a[i]);
--m;
}
}
printf("\n");
}
int main()
{
int a[35];
for (int i = 0; i != 35; ++i)
{
a[i] = i;
}
foo(a, 35, 8);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#define count 8 //宏定义
int final[count]; //一条运行语句
void Print_data()
{
int i=0;
for(i=0;i<count;i++)
printf("%d ",final[i]);
printf("\n");
}
void dfs(int n,int posi)
{
int i;
if(n==count)
Print_data();
else
for(i=posi;i<=35;i++)
{
final[n]=i;
if(n<count)
dfs(n+1,i+1);
}
}
//组合数
int main()
{
dfs(0,1);
return 0;
}
#include <stdlib.h>
#include <stdio.h>
void swap(int *a,int *b)
{
int t;
t=*a;*a=*b;*b=t;
}
void arr(int a[],int index)
{
int i;
if (index==8){
for(i=0;i<8;++i) printf("%d ",a[i]);
printf("\n");
}
else
for (i=index;i<35;++i)
{
swap(&a[index],&a[i]);
arr(a,index+1);
swap(&a[index],&a[i]);
}
}
int main()
{
int a[35] ={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35};
arr(a,0);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void randomInit(int *arr,int size)
{
for(int i=1;i<=size;++i)
{
arr[i-1]=i;
}
srand((unsigned int)time(NULL));
}
void randomShift(int *arr,int size)
{
int leftLen=size;
for(int cur=0;cur<size-1;++cur)
{
int randPos=rand()%leftLen--;
int temp=arr[cur];
arr[cur]=arr[cur+randPos];
arr[cur+randPos]=temp;
}
}
int main()
{
int arr[35];
randomInit(arr,35);
randomShift(arr,35);
for(int i=0;i<35;++i)
{
printf("%d ",arr[i]);
}
return 0;
}
int buf[35] = {0};
int flag[35] = {0};
int count = 0;
int idx = 0;
int i = 0;
for (i=0; i<35; i++)
{
buf[i] = i;
}
srand((int)time(0));
while (count < 8)
{
idx = rand()%35;
while(flag[idx]!=0)
idx = rand() % 35;
count++;
flag[idx]++;
}
for (int i=0; i<35; i++)
{
if (flag[i] != 0)
printf("%d\n", buf[ i ]);
}