Форум » Базар » for leha... » Ответить

for leha...

Nobody RST: #include <stdio.h> #include <stdlib.h> #include <string.h> int count=0,index=0; bool zeul(int a, int b) { count++; return a==b; } void lastprocess(int s[]) { bool blc1,blc2; blc1=zeul(s[0],s[1]); if(blc1) { blc2=zeul(s[0],s[2]); if(blc2) index+=4; else if(!blc2) index+=3; } else if(!blc1) { blc2=zeul(s[0],s[2]); if(blc2) index+=2; else if(!blc2) index+=1; } printf("The index %d coin is falth\ncount = %d\n",index,count); exit(0); } void divide(int n,int s[]) { int u[512],v[512]; bool blc; int a=0,b=0; int q,h;//q1 is 1st quatter q2 is second quatter h is half h=n/2; for(int i=0;i<h;i++) u=s; for(int j=0;j<h;j++) v[j]=s[h+j]; q=h/2; // half the halfed array so it is one quatter for(i=0;i<q;i++) a+=u; for(j=q;j<h;j++) b+=u[j]; blc=zeul(a,b); if(!blc && h>=4) //1st half is 0 and half>=4 { if(h==4) lastprocess(u); // half become n u become s else divide(h,u); } else if(blc && h>=4) { index=index+h; if(h==4) { lastprocess(v); } else divide(h,v); } } int process3(int s[]) { bool blc,blc1; blc=zeul(s[0],s[1]); if(blc) index+=3; else if(!blc) { blc1=zeul(s[0],s[2]); if(blc1) index+=2; else if(!blc) index+=1; } return 0; } int gisudvd(int n,int s[]) { static int camzo=2; while(n>camzo) camzo*=2; int u[512],v[512]; bool blc,blc1; int a=0,b=0; int q,h;//q is a quatter h is half h=camzo/2; for(int i=0;i<h;i++) u=s; for(int j=0;j<(n-h);j++) v[j]=s[h+j]; q=h/2; // half the halfed array so it is one quatter for(i=0;i<q;i++) a+=u; for(j=q;j<h;j++) b+=u[j]; blc=zeul(a,b); if(!blc) divide(h,u); else if(blc) { index+=h; if(n-h==2) { blc1=zeul(s[0],s[index]); if(!blc1) { index+=1; printf("The index %d coin is falth\ncount = %d\n",index,count); exit(0); } else if(blc) { index+=2; printf("The index %d coin is falth\ncount = %d\n",index,count); exit(0); } } else if(n-h==1) { index+=1; printf("The index %d coin is falth\ncount = %d\n",index,count); } else if(n-h==3) { process3(v); printf("The index %d coin is falth\ncount = %d\n",index,count); exit(0); } else gisudvd(camzo-h,v); } return 0; } void main() { int num,s[1024]; int n=0; FILE *fp; fp=fopen("coins.txt","r"); if(!fp) { printf("Erro in opening open coin file\a\n"); exit(1); } printf("/////////////////////////////////////////////////////////////////\n"); while((fscanf(fp,"%d",&num))==1) s[n++]=num; printf("/////////////////////////////There are %d number of coins\n",n); if(n%4==0 && n>=8) divide(n,s); else if(n==4) lastprocess(s); else if(n==2 || n==1) { printf("I can't find which is falth because there are not enough coins\n\n"); exit(1); } else if(n%4!=0 && n!=3) gisudvd(n,s); else if(n=3) process3(s); }

Ответов - 2

Nobody RST: /* PROGRAM TO SEARCH AN ELEMENT USING BINARY SEARCH */ #include<stdio.h> #include<conio.h> void main() { int list[20],n,no,i,found=0,lb=0,ub,mid; clrscr(); printf("\n\t\t PROGRAM BINARY SEARCH \n"); printf("\n Enter the number of elements in the list : "); scanf("%d",&n); printf("\n Enter the list \n"); for(i=0;i<n;i++) scanf("%d",&list); printf("\n Enter the number to be searched : "); scanf("%d",&no); ub=n-1; while((lb<=ub)&&(found==0)) { mid=(lb+ub)/2; if(list[mid]==no) found=1; if(list[mid]<no) lb=mid+1; else ub=mid-1; } if(found==1) printf("\n The number is present at the position %d",mid+1); else printf("\n %d is not present in the list",no); getch(); }

Nobody RST: http://msdn.microsoft.com/vstudio/express/



полная версия страницы