139 - Telephone Tangles
Moderator: Board moderators
139 help
[c]/* @JUDGE_ID: 17563KJ 139 C */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
main()
{
struct sample
{
char head[20];
char name[30];
float cost;
char T;
}sample[3000];
int s_n;
int i,j;
char newstr[300];
char *p;
int loc;
char num[20];
int done;
int num_s;
int len;
int k;
int find;
char call[30];
int min;
char sub[30];
float y,z;
char temp[300];
char *q;
int result;
#ifndef ONLINE_JUDGE
close (0); open ("in", O_RDONLY);
#endif
s_n = -1;
while(gets(newstr) && (newstr[0]!='0' ||newstr[1]!= '0'
||newstr[2]!= '0' ||newstr[3]!= '0' || newstr[4] != '0' || newstr[5]!=
'0')){
s_n++;
sscanf(newstr,"%s",&sample[s_n].head);
for(i = strlen(sample[s_n].head)+1 ;i<=strlen(newstr);i++){
temp[i-strlen(sample[s_n].head)-1] = newstr;
}
/* printf("%s\n",temp);
*/ p = strstr(temp,"$");
result = p -temp;
/* printf("result %d\n",result);
*/ for(i = 0 ; i<25;i++){
num = '\0';
}
for(i = 0 ; i< result;i++){
sample[s_n].name = temp;
}
sample[s_n].name ='\0';
for(i = result+1 ; i<strlen(temp);i++){
num[i-result-1] = temp;
}
sscanf(num,"%f",&sample[s_n].cost);
sample[s_n].cost = sample[s_n].cost/100;
if((sample[s_n].head[0] == '0')&& (sample[s_n].head[1]=='0')){
sample[s_n].T = 'I';
}
if((sample[s_n].head[0] == '0')&& (sample[s_n].head[1]!='0')){
sample[s_n].T = 'S';
}
/* printf("%s\n",num);
printf("%s=%s\n",sample[s_n].head,sample[s_n].name);
printf("cost %.2f\n",sample[s_n].cost);
*/ }
while(scanf("%s %d",&call,&min) && call[0]!= '#'){
/* printf("%s\n",call);
*/ if(call[0] != '0'){
y = 0.00;
z = 0.00;
printf("%-16s%-20s%15s%5d%6.2f%7.2f\n",call,"Local",call,min,0.00,0.00);
}else{
/*----------------------------------------------------*/
find = 0;
for(i = 0 ; i<=s_n ;i++){
if(strstr(call,sample.head)!= NULL){
for(j = strlen(sample.head);j<=strlen(call) ;j++){
sub[j-strlen(sample.head)] = call[j];
}
if(sample.T == 'I'){
if(strlen(sub) >= 4 && strlen(sub)<=10){
find =1;
break;
}
}else if(sample[i].T =='S'){
if(strlen(sub) >= 4 && strlen(sub) <=7){
find =1;
break;
}
}
}
}
if(find == 1){
y = sample[i].cost;
z = y*min;
printf("%-16s%-25s%10s%5d%6.2f%7.2f\n",call,sample[i].name,sub,min,y,z);
}else if(find == 0){
printf("%-16s%-35s%5d%13.2f\nn",call,"Unknown",min,-1.00);
}
/*----------------------------------------------------*/
}
}
}
This is my code.......088925 Broad-wood$ 81
I got correct result for ..
03 Arrow.town$ 38
0061 Australia$ 140
0911 N
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
main()
{
struct sample
{
char head[20];
char name[30];
float cost;
char T;
}sample[3000];
int s_n;
int i,j;
char newstr[300];
char *p;
int loc;
char num[20];
int done;
int num_s;
int len;
int k;
int find;
char call[30];
int min;
char sub[30];
float y,z;
char temp[300];
char *q;
int result;
#ifndef ONLINE_JUDGE
close (0); open ("in", O_RDONLY);
#endif
s_n = -1;
while(gets(newstr) && (newstr[0]!='0' ||newstr[1]!= '0'
||newstr[2]!= '0' ||newstr[3]!= '0' || newstr[4] != '0' || newstr[5]!=
'0')){
s_n++;
sscanf(newstr,"%s",&sample[s_n].head);
for(i = strlen(sample[s_n].head)+1 ;i<=strlen(newstr);i++){
temp[i-strlen(sample[s_n].head)-1] = newstr;
}
/* printf("%s\n",temp);
*/ p = strstr(temp,"$");
result = p -temp;
/* printf("result %d\n",result);
*/ for(i = 0 ; i<25;i++){
num = '\0';
}
for(i = 0 ; i< result;i++){
sample[s_n].name = temp;
}
sample[s_n].name ='\0';
for(i = result+1 ; i<strlen(temp);i++){
num[i-result-1] = temp;
}
sscanf(num,"%f",&sample[s_n].cost);
sample[s_n].cost = sample[s_n].cost/100;
if((sample[s_n].head[0] == '0')&& (sample[s_n].head[1]=='0')){
sample[s_n].T = 'I';
}
if((sample[s_n].head[0] == '0')&& (sample[s_n].head[1]!='0')){
sample[s_n].T = 'S';
}
/* printf("%s\n",num);
printf("%s=%s\n",sample[s_n].head,sample[s_n].name);
printf("cost %.2f\n",sample[s_n].cost);
*/ }
while(scanf("%s %d",&call,&min) && call[0]!= '#'){
/* printf("%s\n",call);
*/ if(call[0] != '0'){
y = 0.00;
z = 0.00;
printf("%-16s%-20s%15s%5d%6.2f%7.2f\n",call,"Local",call,min,0.00,0.00);
}else{
/*----------------------------------------------------*/
find = 0;
for(i = 0 ; i<=s_n ;i++){
if(strstr(call,sample.head)!= NULL){
for(j = strlen(sample.head);j<=strlen(call) ;j++){
sub[j-strlen(sample.head)] = call[j];
}
if(sample.T == 'I'){
if(strlen(sub) >= 4 && strlen(sub)<=10){
find =1;
break;
}
}else if(sample[i].T =='S'){
if(strlen(sub) >= 4 && strlen(sub) <=7){
find =1;
break;
}
}
}
}
if(find == 1){
y = sample[i].cost;
z = y*min;
printf("%-16s%-25s%10s%5d%6.2f%7.2f\n",call,sample[i].name,sub,min,y,z);
}else if(find == 0){
printf("%-16s%-35s%5d%13.2f\nn",call,"Unknown",min,-1.00);
}
/*----------------------------------------------------*/
}
}
}
This is my code.......088925 Broad-wood$ 81
I got correct result for ..
03 Arrow.town$ 38
0061 Australia$ 140
0911 N
-
- A great helper
- Posts: 284
- Joined: Thu Feb 28, 2002 2:00 am
- Location: Germany
- Contact:
-
- A great helper
- Posts: 284
- Joined: Thu Feb 28, 2002 2:00 am
- Location: Germany
- Contact:
My program read your data and replies
[quote]
031526 Arrow.town 1526 22 0.38 8.36
0061853279 Australia 853279 3 1.40 4.20
0889256287213 Broad-wood 6287213 122 0.81 98.82
779760 Local 779760 1 0.00 0.00
002832769 Unknown 5 -1.00
0356563 Arrow.town 56563 199 0.38 75.62
088925123 Unknown 6 -1.00
0061123456789012Unknown 10 -1.00
0911443655 N
[quote]
031526 Arrow.town 1526 22 0.38 8.36
0061853279 Australia 853279 3 1.40 4.20
0889256287213 Broad-wood 6287213 122 0.81 98.82
779760 Local 779760 1 0.00 0.00
002832769 Unknown 5 -1.00
0356563 Arrow.town 56563 199 0.38 75.62
088925123 Unknown 6 -1.00
0061123456789012Unknown 10 -1.00
0911443655 N
I think problem 139 include some requesets that are not clear..
1. the city name can has blank ... "Log Angles"
2. you need to check the subscriber IDD(4-10) STD(4-7)
*3. you should confirm that the call number has the head number on the
"begining" of call number..
I spent three hours in *3...><...
Moreover,the problem 139 says
"Telephone numbers will not be ambiguous."
So there is no call number that you can't deside IDD or STD...
1. the city name can has blank ... "Log Angles"
2. you need to check the subscriber IDD(4-10) STD(4-7)
*3. you should confirm that the call number has the head number on the
"begining" of call number..
I spent three hours in *3...><...
Moreover,the problem 139 says
"Telephone numbers will not be ambiguous."
So there is no call number that you can't deside IDD or STD...
This problem really makes me crazy!..><..I've struggled for almost 2 days. And it still get WA.. I followed your suggestions. You can read my code and you'll know what I mean.
[c]
#include<stdio.h>
#include<string.h>
void main(void)
{
int n,x,time,len,y,z,point,len2;
double t;
char buffer[50],c;
struct telecom
{
char num[7],name[26];
double fee;
}data[2000];
for(n=0;n<2000;)
{
scanf("%s",buffer);
if(strcmp(buffer,"000000")==0)
break;
strcpy(data[n].num,buffer);
c=getchar();
gets(buffer);
for(x=0;buffer[x]!='$';x++)
data[n].name[x]=buffer[x];
data[n].name[x]='\0';
for(t=0,point=-1;buffer[x]!='\0';x++)
if(buffer[x]>='0' && buffer[x]<='9')
{
t=t*10+buffer[x]-'0';
if(point>=0)
point++;
}
else if(buffer[x]=='.')
point++;
for(x=0;x<point;x++)
t/=10;
data[n].fee=t;
n++;
}
while(1)
{
scanf("%s",buffer);
if(strcmp(buffer,"#")==0)
break;
scanf("%d",&time);
if(buffer[0]!='0')
printf("%-16sLocal%30s%7d%24s%7s\n",buffer,buffer,time,"0.00","0.00");
else
{
for(x=0;x<n;x++)
{
len=strlen(data[x].num);
len2=strlen(buffer);
if(len>=len2 || len>15)
continue;
if(len==6 && (len2-len<4 || len2-len>7))
continue;
for(y=0;y<len;y++)
if(data[x].num[y]!=buffer[y])
break;
if(y==len)
if(len2-len>=8 && len2-len <=10 && buffer[1]!='0')
continue;
else if(len2-len<4 || len2-len>7)
continue;
else
break;
}
if(y==len)
{
printf("%-16s",buffer);
printf("%s",data[x].name);
for(z=0;z<35-strlen(data[x].name)-(len2-len);z++)
printf(" ");
for(z=len;buffer[z]!='\0';z++)
printf("%c",buffer[z]);
t=time;
printf("%7d%24.2lf%7.2lf\n",time,data[x].fee/100,t*data[x].fee/100);
}
if(x==n)
{
printf("%-16s",buffer);
printf("%-35s","Unknown");
printf("%7d",time);
printf("%31s\n","-1.00");
}
}
}
}
[/c]
[c]
#include<stdio.h>
#include<string.h>
void main(void)
{
int n,x,time,len,y,z,point,len2;
double t;
char buffer[50],c;
struct telecom
{
char num[7],name[26];
double fee;
}data[2000];
for(n=0;n<2000;)
{
scanf("%s",buffer);
if(strcmp(buffer,"000000")==0)
break;
strcpy(data[n].num,buffer);
c=getchar();
gets(buffer);
for(x=0;buffer[x]!='$';x++)
data[n].name[x]=buffer[x];
data[n].name[x]='\0';
for(t=0,point=-1;buffer[x]!='\0';x++)
if(buffer[x]>='0' && buffer[x]<='9')
{
t=t*10+buffer[x]-'0';
if(point>=0)
point++;
}
else if(buffer[x]=='.')
point++;
for(x=0;x<point;x++)
t/=10;
data[n].fee=t;
n++;
}
while(1)
{
scanf("%s",buffer);
if(strcmp(buffer,"#")==0)
break;
scanf("%d",&time);
if(buffer[0]!='0')
printf("%-16sLocal%30s%7d%24s%7s\n",buffer,buffer,time,"0.00","0.00");
else
{
for(x=0;x<n;x++)
{
len=strlen(data[x].num);
len2=strlen(buffer);
if(len>=len2 || len>15)
continue;
if(len==6 && (len2-len<4 || len2-len>7))
continue;
for(y=0;y<len;y++)
if(data[x].num[y]!=buffer[y])
break;
if(y==len)
if(len2-len>=8 && len2-len <=10 && buffer[1]!='0')
continue;
else if(len2-len<4 || len2-len>7)
continue;
else
break;
}
if(y==len)
{
printf("%-16s",buffer);
printf("%s",data[x].name);
for(z=0;z<35-strlen(data[x].name)-(len2-len);z++)
printf(" ");
for(z=len;buffer[z]!='\0';z++)
printf("%c",buffer[z]);
t=time;
printf("%7d%24.2lf%7.2lf\n",time,data[x].fee/100,t*data[x].fee/100);
}
if(x==n)
{
printf("%-16s",buffer);
printf("%-35s","Unknown");
printf("%7d",time);
printf("%31s\n","-1.00");
}
}
}
}
[/c]
prob 139: Why invalid memory assinment?
I dont' know why i get runtime error for this problem. I think my array size is ok. Can anybody help me in this case? Thanks in advance.
Here is my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
long int a, b, c, d, i, j, k, l, m, len, hi, min, ln[500], cnt[500];
float s, e, ch[500];
char code[30],tph[30],area[50],tel[500][20],state[500][30];
char *st;
main ()
{
i = 1;
while (1)
{
gets (area);
if ( (strcmp (area,"000000") )==0) break;
st = strtok (area, " ");
strcpy (tel[i], st);
ln[i] = strlen (tel[i]);
st = strtok (NULL, "$");
strcpy (state[i], st);
st = strtok (NULL, " ");
s = atoi(st);
ch[i] = s/100;
i = i+1;
}
while (1) {
scanf ("%s", code);
if ( (strcmp (code, "#") )==0) break;
scanf ("%ld", &min);
len = strlen (code);
if (code[0]!=48) {
printf("%-15s Local %13s %4ld 0.00 0.00\n",
code, code, min);
}
if (code[0]==48)
{
hi = 0;
for (j=0;j<=i;j++) cnt[j] = 0;
if (code[1]!=48)
{
for (j=1;j<i;j++) {
a = 1;
while (1)
{
if (tel[j][a]==code[a]) {
cnt[j]=cnt[j]+1;
a = a+1;
if (a>ln[j]) break;
}
else break;
}
if (cnt[j]>cnt[j-1]) {
hi = cnt[j];
b = j;
}
} /* End of for */
d = hi+1; m = 0;
while (1) {
tph[m] = code[d];
if (d==len) break;
d = d+1;
m = m+1;
}
} /* End of if */
if (code[1]==48)
{
for (j=1;j<i;j++) {
a = 2;
while (1)
{
if (tel[j][a]==code[a]) {
cnt[j]=cnt[j]+1;
a = a+1;
if (a>ln[j]) break;
}
else break;
}
if (cnt[j]>cnt[j-1]) {
hi = cnt[j];
b = j;
}
} /* End of for */
d = hi+2; m = 0;
while (1) {
tph[m] = code[d];
if (d==len) break;
d = d+1;
m = m+1;
}
} /* End of if */
e = min*(ch[b]);
if (hi==0) {
printf ("%-15s Unknown ", code);
printf ("%5ld -1.00\n", min);
}
if (hi>0) {
printf ("%-15s %-23s", code,state[b]);
printf(" %11s %4ld %5.2f %6.2f\n",tph,min,ch[b],e);
}
}
} /* End of while */
return 0;
}
Here is my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
long int a, b, c, d, i, j, k, l, m, len, hi, min, ln[500], cnt[500];
float s, e, ch[500];
char code[30],tph[30],area[50],tel[500][20],state[500][30];
char *st;
main ()
{
i = 1;
while (1)
{
gets (area);
if ( (strcmp (area,"000000") )==0) break;
st = strtok (area, " ");
strcpy (tel[i], st);
ln[i] = strlen (tel[i]);
st = strtok (NULL, "$");
strcpy (state[i], st);
st = strtok (NULL, " ");
s = atoi(st);
ch[i] = s/100;
i = i+1;
}
while (1) {
scanf ("%s", code);
if ( (strcmp (code, "#") )==0) break;
scanf ("%ld", &min);
len = strlen (code);
if (code[0]!=48) {
printf("%-15s Local %13s %4ld 0.00 0.00\n",
code, code, min);
}
if (code[0]==48)
{
hi = 0;
for (j=0;j<=i;j++) cnt[j] = 0;
if (code[1]!=48)
{
for (j=1;j<i;j++) {
a = 1;
while (1)
{
if (tel[j][a]==code[a]) {
cnt[j]=cnt[j]+1;
a = a+1;
if (a>ln[j]) break;
}
else break;
}
if (cnt[j]>cnt[j-1]) {
hi = cnt[j];
b = j;
}
} /* End of for */
d = hi+1; m = 0;
while (1) {
tph[m] = code[d];
if (d==len) break;
d = d+1;
m = m+1;
}
} /* End of if */
if (code[1]==48)
{
for (j=1;j<i;j++) {
a = 2;
while (1)
{
if (tel[j][a]==code[a]) {
cnt[j]=cnt[j]+1;
a = a+1;
if (a>ln[j]) break;
}
else break;
}
if (cnt[j]>cnt[j-1]) {
hi = cnt[j];
b = j;
}
} /* End of for */
d = hi+2; m = 0;
while (1) {
tph[m] = code[d];
if (d==len) break;
d = d+1;
m = m+1;
}
} /* End of if */
e = min*(ch[b]);
if (hi==0) {
printf ("%-15s Unknown ", code);
printf ("%5ld -1.00\n", min);
}
if (hi>0) {
printf ("%-15s %-23s", code,state[b]);
printf(" %11s %4ld %5.2f %6.2f\n",tph,min,ch[b],e);
}
}
} /* End of while */
return 0;
}
WA again :(
I've tried with some example inputs found on this board. For the input:
[quote]
088925 Broad-wood$ 81
03 Arrow.town$ 38
0061 Australia$ 140
0911 N
-
- New poster
- Posts: 15
- Joined: Tue Sep 10, 2002 1:56 am
- Location: Brasil
- Contact:
139 - Telephone Tangles
Hi!
This problem is really tricky and I couldn't got it yet :(.
I try everthing I could wonder.
Case like:
03 Arrow.town$ 38
0061 Australia$ 140
0911 N
This problem is really tricky and I couldn't got it yet :(.
I try everthing I could wonder.
Case like:
03 Arrow.town$ 38
0061 Australia$ 140
0911 N
Last edited by Pedrinho UFPE on Thu Apr 17, 2003 5:52 am, edited 1 time in total.
Interested
-
- New poster
- Posts: 15
- Joined: Tue Sep 10, 2002 1:56 am
- Location: Brasil
- Contact:
I found
I found my problem.
If someone has a problem with this problem cansend a e-mail and i'll reply.
This problem is REALLY TRICKY!!!
pmmc@cin.ufpe.br
If someone has a problem with this problem cansend a e-mail and i'll reply.
This problem is REALLY TRICKY!!!

pmmc@cin.ufpe.br
Interested