70,024
社区成员




#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
char str[8][10] = {"111", "222", "333", "444", "555", "666", "777", "888"};
int loc[8];
FILE *fi,*fo;
int i,j,k,n,t;
char ln[1000];
int main () {
srand(time(NULL));
fi=fopen("in.txt","r");
if (NULL==fi) {printf("Can not open file in.txt!\n");return 1;}
i=0;
while (1) {
if (NULL==fgets(ln,1000,fi)) break;
i++;
}
n=i;
printf("n=%d\n",n);
if (n==0) n=1;
for (i=0;i<8;i++) {
k=0;
while (1) {
loc[i]=rand()%n;
for (j=0;j<i;j++) {
if (loc[j]==loc[i]) break;
}
if (j>=i || k>100) break;
k++;
}
}
for (i=0;i<7;i++) {
for (j=i+1;j<8;j++) {
if (loc[i]>loc[j]) {
t=loc[i];loc[i]=loc[j];loc[j]=t;
}
}
}
for (i=0;i<8;i++) printf("%d ",loc[i]+1);
printf("\n");
fo=fopen("out.txt","w");
if (NULL==fo) {printf("Can not create file out.txt!\n");fclose(fi);return 2;}
rewind(fi);
k=0;
j=0;
i=0;
while (1) {
if (NULL==fgets(ln,1000,fi)) break;
if (i==loc[j]) {
ln[strlen(ln)-1]=0;//去掉末尾的'\n'
fprintf(fo,"%s%s",ln,str[k++]);
while (1) {
if (j<8 && loc[j+1]==loc[j]) {
fprintf(fo,"%s",str[k++]);
j++;
} else break;
}
fprintf(fo,"\n");
if (j<8) j++;
else break;
} else {
fprintf(fo,"%s",ln);
}
i++;
}
if (k<8) {
for (i=k;i<8;i++) fprintf(fo,"%s",str[i]);
fprintf(fo,"\n");
}
fclose(fo);
fclose(fi);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
char str[8][10] = {"111", "222", "333", "444", "555", "666", "777", "888"};
int loc[8];
FILE *fi,*fo;
int i,j,n,t;
char ln[1000];
int main () {
srand(time(NULL));
fi=fopen("in.txt","r");
if (NULL==fi) {printf("Can not open file in.txt!\n");return 1;}
i=0;
while (1) {
if (NULL==fgets(ln,1000,fi)) break;
i++;
}
n=i;
for (i=0;i<8;i++) {
k=0;
while (1) {
loc[i]=rand()%(n+1);
for (j=0;j<i;j++) {
if (loc[j]==loc[i]) break;
}
if (j>=i || k>100) break;
k++;
}
}
for (i=0;i<7;i++) {
for (j=i+1;j<8;j++) {
if (loc[i]>loc[j]) {
t=loc[i];loc[i]=loc[j];loc[j]=t;
}
}
}
for (i=0;i<8;i++) printf("%d ",loc[i]+1);
printf("\n");
fo=fopen("out.txt","w");
if (NULL==fo) {printf("Can not create file out.txt!\n");fclose(fi);return 2;}
rewind(fi);
j=0;
i=0;
while (1) {
if (NULL==fgets(ln,1000,fi)) break;
if (i==loc[j]) {
ln[strlen(ln)-1]=0;//去掉末尾的'\n'
fprintf(fo,"%s%s\n",ln,str[j]);
j++;
} else {
fprintf(fo,"%s",ln);
}
i++;
}
fclose(fo);
fclose(fi);
return 0;
}