C语言编程题目(二)
编写程序实现:
(1)产生300个随机的四位数,然后将这些4位数保存到Rand.txt中;
(2)统计Rand.txt 中个位数与千位之和等于十位与百位之和的数的个数;
(3)对满足条件2的数将其逆序排列将其输出到out.txt中。
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 5 #define MAXNUM 9999 6 #define MINNUM 1000 7 #define MAXLINE 300 8 #define DIR "D:\\Rand.txt" 9 10 void getRand(); 11 void writeDat(); 12 void readDat(); 13 void findDat(); 14 15 main() 16 { 17 printf("Hellow fjk\n"); 18 getRand(); 19 findDat(); 20 } 21 22 void getRand() 23 { 24 int a[MAXLINE]; 25 int i; 26 srand((unsigned int)time(NULL)); 27 28 for(i=0;i<MAXLINE;i++) 29 a[i]=rand()%(MAXNUM-MINNUM+1)+MINNUM; 30 writeDat(MAXLINE,a); 31 } 32 33 void writeDat(int maxline,int Rand[]) 34 { 35 FILE *fp; 36 int i; 37 38 fp=fopen(DIR,"w+"); 39 for(i=0;i<maxline;i++) 40 fprintf(fp,"%d\n",Rand[i]); 41 fclose(fp); 42 } 43 44 void readDat(int maxline,int temp[]) 45 { 46 FILE *fp; 47 int i; 48 fp=fopen(DIR,"r"); 49 for(i=0;i<maxline;i++) 50 { 51 fscanf(fp,"%d",&temp[i]); 52 printf("%d\n",temp[i]); 53 } 54 fclose(fp); 55 } 56 57 void findDat() 58 { 59 int temp[MAXLINE],b[MAXLINE],i,j,t,cnt=0; 60 int ge_wei,shi_wei,bai_wei,qian_wei; 61 FILE *fp; 62 63 readDat(MAXLINE,temp); 64 for(i=0;i<MAXLINE;i++) 65 { 66 ge_wei=temp[i]%10; 67 shi_wei=temp[i]%10/10; 68 bai_wei=temp[i]%1000/100; 69 qian_wei=temp[i]/1000; 70 71 if((ge_wei+qian_wei)>(bai_wei+shi_wei)) 72 { 73 b[cnt]=temp[i]; 74 cnt++; 75 } 76 } 77 printf("%d\n",cnt); 78 for(i=0;i<cnt-1;i++) 79 for(j=i+1;j<cnt;j++) 80 if(b[i]>b[j]) 81 { 82 t=b[i]; 83 b[i]=b[j]; 84 b[j]=t; 85 } 86 87 fp=fopen("D:\\out.txt","w+"); 88 for(i=0;i<cnt;i++) 89 fprintf(fp,"%d\n",b[i]); 90 fclose(fp); 91 }