Bài 32:
#include<stdio.h>
#include<string.h>
FILE*f1=fopen("BAI4.INP","r");
FILE*f2=fopen("BAI4.OUT","w");
struct cs{
char tu[100];
int sl;
};
int tim(int n, char s[], cs ds[]){
for(int i=0;i<n;i++){
if(strcmp(s,ds[i].tu)==0)
return i;
}
return -1;
}
main()
{
int n=0,i=0;
char s[100];
cs ds[100];
char ss[6];
while(!feof(f1)){
fscanf(f1,"%s",&s);
int k=tim(n,s,ds);
if(k==-1){
strcpy(ds[n].tu,s);
ds[n].sl=1;
n++;
}
else
ds[k].sl++;
}
int max=strlen(ds[0].tu);
for(int i=0;i<n;i++){
if(strlen(ds[i].tu)>max)
max=strlen(ds[i].tu);
}
for(int i=0;i<n;i++){
if(strlen(ds[i].tu)==max)
printf("%s %d\n",ds[i].tu,max);
}
}
Bài 33
#include<stdio.h>
#include<math.h>
#include<string.h>
FILE*f1=fopen("BAI4.INP","r");
FILE*f2=fopen("BAI4.OUT","w");
struct SV{
char ten[20];
char ngaysinh[20];
int maso;
float d1,d2,d3;
};
void timmax(int n, SV ds[]){
float max=ds[0].d1+ds[0].d2+ds[0].d3;
for(int i=0;i<n;i++){
if(ds[i].d1+ds[i].d2+ds[i].d3>max){
max=ds[i].d1+ds[i].d2+ds[i].d3;
}
}
for(int i=0;i<n;i++){
if(ds[i].d1+ds[i].d2+ds[i].d3==max){
fprintf(f2,"%d %s %s %.2f\n",ds[i].maso,ds[i].ten,ds[i].ngaysinh,ds[i].d1+ds[i].d2+ds[i].d3);
}
}
}
main()
{
int t;
int i=0,dem=0;
fscanf(f1,"%d",&t);
SV ds[100];
char ss[6];
fgets(ss,5,f1);
while(t--){
dem++;
ds[i].maso=dem;
fgets(ds[i].ten,20,f1);
ds[i].ten[strlen(ds[i].ten)-1]='\0';
fgets(ds[i].ngaysinh,20,f1);
ds[i].ngaysinh[strlen(ds[i].ngaysinh)-1]='\0';
fscanf(f1,"%f%f%f",&ds[i].d1,&ds[i].d2,&ds[i].d3);
fgets(ss,5,f1);
i++;
}
timmax(dem,ds);
}
Bài 34.
#include<stdio.h>
#include<math.h>
FILE*f1=fopen("BAI4.INP","r");
FILE*f2=fopen("BAI4.OUT","w");
struct cs{
int so;
int sl;
};
int nt(int n){
if(n<2)
return 0;
for(int i=2;i<=sqrt(n);i++){
if(n%i==0)
return 0;
}
return 1;
}
int tim(int n, int m, cs ds[]){
for(int i=0;i<n;i++){
if(ds[i].so==m)
return i;
}
return -1;
}
void sapxep(int n,cs ds[]){
for(int i=0;i<n-1;i++){
for(int j=i;j<n;j++){
if(ds[i].sl<ds[j].sl){
cs tam=ds[i];
ds[i]=ds[j];
ds[j]=tam;
}
}
}
}
main()
{
int n=0;
int s;
char ss[6];
cs ds[100];
fgets(ss,5,f1);
while(!feof(f1)){
fscanf(f1,"%d",&s);
if(nt(s)){
int k=tim(n,s,ds);
if(k==-1){
ds[n].so=s;
ds[n].sl=1;
n++;
}
else
ds[k].sl++;
}
}
sapxep(n,ds);
for(int i=0;i<n;i++){
fprintf(f2,"%d %d\n",ds[i].so,ds[i].sl);
}
}
Bài 35:
#include<stdio.h>
#include<math.h>
#include<string.h>
FILE*f1=fopen("BAI4.INP","r");
FILE*f2=fopen("BAI4.OUT","w");
struct cs{
int so;
int sl;
};
int tn(int n){
int s=0,m=n;
while(n){
s=10*s+n%10;
n=n/10;
}
if(s==m)
return 1;
return 0;
}
int tim(int n, int m, cs ds[]){
for(int i=0;i<n;i++){
if(m==ds[i].so)
return i;
}
return -1;
}
void sapxep(int n, cs ds[]){
for(int i=0;i<n-1;i++){
for(int j=i;j<n;j++){
if(ds[i].sl<ds[j].sl){
cs s=ds[i];
ds[i]=ds[j];
ds[j]=s;
}
}
}
}
main()
{
cs ds[100];
int n=0,s,t=0;
while(!feof(f1)){
fscanf(f1,"%d",&s);
t++;
if(tn(s)){
int k=tim(n,s,ds);
if(k==-1){
ds[n].so=s;
ds[n].sl=1;
n++;
}
else
ds[k].sl++;
}
}
sapxep(n,ds);
for(int i=0;i<n;i++){
fprintf(f2,"%d %d\n",ds[i].so,ds[i].sl);
}
int d=0;
for(int i=0;i<n;i++){
d=d+ds[i].sl;
}
fprintf(f2,"tong so %d/%d ",d,t);
}
Không có nhận xét nào:
Đăng nhận xét