It can be solved in a more efficient manner by using a struct to store several pairs of ip and corresponding name of the machine.However i have simply used a 2d array for ip and name.
I would encourage people to use struct.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include<stdio.h>
#include<string.h>
void main()
{
int i,j,t;
scanf("%d",&t);
char name[t][100];
char ip[t][100];
for(i=0;i<t;i++)
{
scanf("%s",ip[i]);
scanf("%s",name[i]);
}/*
for(i=0;i<t;i++)
{
printf("%s\n",ip[i]);
printf("%s",name[i]);
}*/
for(i=0;i<t;i++)
{
int found=0;
int c=0;
char search[10];
int k=0;
while(c<2)
{
char temp[100];
strcpy(temp,ip[i]);
if(temp[k]=='.')
{c++;}
search[k]=temp[k];
k++;
}
for(j=i+1;j<t;j++)
{
char search2[k];
strncpy(search2,ip[j],k);
if(strcmp(search,search2)==0)
{
printf("Machines %s and %s are on the same local network\n",name[i],name[j]);
}
}
}
}
please explain the second half of the solution
ReplyDeleteI take all the other elements in the ip array (one-by-one counter=j),extract the first k char(strncpy) and compare it.
DeleteUnderstood?
no please write the pseudocode
DeleteFor each element in ip[](counter=i)
DeleteExtract first two segments of the ip( stored in search,length k)
For each element in ip[](i+1 onwards,counter j)
Extract k(length of search) characters of ip(search2) and compare with search
Print RESULT
END FOR
END FOR
Hope this is better!
For each element in ip[](counter=i)
DeleteExtract first two segments of the ip( stored in search,length k)
For each element in ip[](i+1 onwards,counter j)
Extract k(length of search) characters of ip(search2) and compare with search
Print RESULT
END FOR
END FOR
Hope this is better!
This comment has been removed by the author.
ReplyDeleteYes, but you assume that the first two segments take 5 characters which may not satisfy all test cases. Consider 0.0.1.2 and 0.0.5.4, ur code will not show that they are local machines but they actually are.
ReplyDelete#include
ReplyDelete#include
#include
main()
{
int n,i,j,l,c;
char ip[50][50],name[50][50],a[50][50],b[50][50];
scanf("%d",&n);
for(i=0;i<n;i++)
{
fflush(stdin);
scanf("%s",ip[i]);
scanf("%s",name[i]);
}
for(i=0;i<n;i++)
{
l=strlen(ip[i]);
for(j=0;j<l;j++)
{
if(ip[i][j]!='.')
{
a[i][j]=ip[i][j];
}
else
{
c=j;
break;
}
}
for(j=(c+1);j<l;j++)
{
if(ip[i][j]!='.')
b[i][j-c-1]=ip[i][j];
else
break;
}
}
for(i=0;i<(n-1);i++)
{
for(j=(i+1);j<n;j++)
{
if(strcmp(a[i],a[j])==0 && strcmp(b[i],b[j])==0)
printf("Machines %s and %s are on the same local network",name[i],name[j]);
else
continue;
}
}
}
this is the code made by me... on skillrack its showing private (hidden) cases failed.. please rectify the problem.. thank you
i tried this code too..its still showing private hidden cases failed.. please help.. thank you
Delete#include
#include
main()
{
int n,i,j,l,c;
char ip[50][50],name[50][50],a[50][50],b[50][50];
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s\n",ip[i]);
gets(name[i]);
}
for(i=0;i<n;i++)
{
l=strlen(ip[i]);
for(j=0;j<l;j++)
{
if(ip[i][j]!='.')
{
a[i][j]=ip[i][j];
}
else
{
c=j;
break;
}
}
for(j=(c+1);j<l;j++)
{
if(ip[i][j]!='.')
b[i][j-c-1]=ip[i][j];
else
break;
}
}
for(i=0;i<(n-1);i++)
{
for(j=(i+1);j<n;j++)
{
if(strcmp(a[i],a[j])==0 && strcmp(b[i],b[j])==0)
printf("Machines %s and %s are on the same local network\n",name[i],name[j]);
else
continue;
}
}
}
gets will help in reading names having spaces in between them.. it wll read string with space
DeleteThere is some error i guess in that regard. Skillrack is not accepting any solution,it is always displaying "HIDDEN CASE".
DeleteYour code is correct according to me.
Deleteyes...its working nw..
DeleteThis will run ....try it !!!
ReplyDelete#include
int cmp(char x[20],char y[20])
{
int i=0,j=0,m1,m2,n1,n2;
m1=m2=n1=n2=0;
for(i=0;x[i]!='.';i++)
m1=m1*10+((int)x[i]-48);
for(++i;x[i]!='.';i++)
m2=m2*10+((int)x[i]-48);
for(j=0;y[j]!='.';j++)
n1=n1*10+((int)y[j]-48);
for(++j;y[j]!='.';j++)
n2=n2*10+((int)y[j]-48);
if(m1==n1 && m2==n2)
return(1);
else
return(0);
}
void main()
{
char ip[5][20],nm[5][20];
int i,n,j;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s",ip[i]);
scanf("%s",nm[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(cmp(ip[i],ip[j])==1)
printf("Machines %s and %s are on the same local network",nm[i],nm[j]);
}
}
}
Yes, even the code i posted is now working.
DeleteSkillrack has made a few changes.
Some test cases have been removed.