Localhost Posted January 1, 2010 at 10:19 PM Report Share #303605 Posted January 1, 2010 at 10:19 PM Olá pessoal, estou nos problemas da USACO e agora preciso duma coisa que é tipo setar elementos de um vector para o outro, do género, tenho um vector com primeiros nomes desordenado e tenho outro vector com últimos nomes, como é que eu posso setar um elemento (o primeiro nome) de um vector para outro elemento (ultimo nome) de outro vector? Calculo que seja com ponteiros mas não estou a ver como o posso fazer :dontgetit: here since 2009 Link to comment Share on other sites More sharing options...
Baderous Posted January 1, 2010 at 10:22 PM Report Share #303608 Posted January 1, 2010 at 10:22 PM "Setar"? Link to comment Share on other sites More sharing options...
Localhost Posted January 1, 2010 at 10:24 PM Author Report Share #303609 Posted January 1, 2010 at 10:24 PM Sim, apontar 😛 Exemplo: vector que contem primeiros nomes: Joao, David, Antonio, Manuel vector que contem ultimos nomes: Fonseca, Flores, Moreira, outro qualquer Agora como é que eu relaciono, por exemplo, o Manuel com o Fonseca? here since 2009 Link to comment Share on other sites More sharing options...
Metaluim Posted January 1, 2010 at 10:28 PM Report Share #303610 Posted January 1, 2010 at 10:28 PM mas com que critérios associas o primeiro nome ao último nome? Isso há várias maneiras de fazer, depende do o que quiseres depois fazer com os nomes e como queres mexer com os dados. Talvez um map? typedef struct strMap { char *val; char *key; }_strMap; //exemplo strMap pessoa; pessoa.val = primeiroNome[3]; pessoa.key = segundoNome[0]; Link to comment Share on other sites More sharing options...
Localhost Posted January 1, 2010 at 10:30 PM Author Report Share #303611 Posted January 1, 2010 at 10:30 PM Eu quero fazer isto para poder alterar os dados de um vector mas sem perder a "identidade" do mesmo ou seja, quero por exemplo alterar o último nome mas não quero perder o primeiro nome ou seja, quero tê-lo relacionado na mesma com o primeiro nome Edit: Sim, era mesmo isso que eu queria, vou tentar aplicar, obrigado. here since 2009 Link to comment Share on other sites More sharing options...
Localhost Posted January 1, 2010 at 10:47 PM Author Report Share #303613 Posted January 1, 2010 at 10:47 PM Bem, aqui está o meu programa, alguém me podia ajudar? Não estou a conseguir neste caso: #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void){ FILE *in, *out; if(!(in = fopen("gift1.in","rb"))){ printf("Error\n"); exit(1); } if(!(out = fopen("gift1.out","wb"))){ printf("Error\n"); exit(1); } int n; fscanf(in,"%d",&n); struct map{ char *name; int *money; } people[n]; int i; char name[n][100]; int money[n]; for(i=0;i<n;i++){ money[i] = 0; } for(i=0;i<n;i++){ fscanf(in,"%s",name[i]); people[i].name = name[i]; people[i].money = &money[i]; } for(i=0;i<n;i++){ printf("%s\n", people[i].name); } fclose(in); fclose(out); return 0; } Já consegui fazer qualquer coisa, se eu depois tiver dúvidas venho cá here since 2009 Link to comment Share on other sites More sharing options...
Localhost Posted January 1, 2010 at 11:39 PM Author Report Share #303619 Posted January 1, 2010 at 11:39 PM Pessoal, já tenho 90% do meu código terminado, agora o que eu queria fazer era fazer um loop para verificar se o ficheiro acabou só que me dá um segmentation fault 😞 O código: #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void){ FILE *in, *out; if(!(in = fopen("gift1.in","rb"))){ printf("Error\n"); exit(1); } if(!(out = fopen("gift1.out","wb"))){ printf("Error\n"); exit(1); } int n; fscanf(in,"%d",&n); struct map{ char *name; int *money; } people[n]; int i; char name[n][100]; int money[n]; for(i=0;i<n;i++){ money[i] = 0; } for(i=0;i<n;i++){ fscanf(in,"%s",name[i]); people[i].name = name[i]; people[i].money = &money[i]; } char nome[100]; int mon; int j = 0; fscanf(in,"%s",nome); fscanf(in,"%d",&mon); while(strcmp(nome,name[j])){ j++; } *people[j].money = -mon; int numdoac; fscanf(in,"%d",&numdoac); int z; j=0; char recebe[100]; for(z=0;z<numdoac;z++){ fscanf(in,"%s",recebe); while(strcmp(recebe,name[j])){ j++; } *people[j].money = mon/numdoac; } for(j=0;j<=numdoac;j++){ printf("%s\n", people[j].name); printf("%i\n", *people[j].money); } fclose(in); fclose(out); return 0; } Alguém me podia ajudar? Alguém? 😛 here since 2009 Link to comment Share on other sites More sharing options...
Metaluim Posted January 2, 2010 at 12:14 AM Report Share #303622 Posted January 2, 2010 at 12:14 AM para ver se estás no final do ficheiro? if(ftell(in) == SEEK_END) { //you're at the end! } Link to comment Share on other sites More sharing options...
Localhost Posted January 2, 2010 at 01:57 PM Author Report Share #303661 Posted January 2, 2010 at 01:57 PM Bem, tive de começar o meu programa de novo, desta vez de uma maneira melhor, no entanto continua-me a dar o erro de segmentation fault, eu não percebo porque que é que isso acontece, eu quero criar um loop e estar a fazer as instruções até chegar ao final do ficheiro: #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void){ FILE *in, *out; int num=0,control=0,mondoac=0; char guy[100]; int numdoac=0; if(!(in=fopen("gift1.in","rb"))){ puts("Error opening\n"); exit(1); } if(!(out=fopen("gift1.out","wb"))){ puts("Error opening\n"); exit(1); } fscanf(in,"%d",&num); struct person{ char name[100]; int money; } people[num]; for(control=0;control<num;control++){ fscanf(in,"%s",people[control].name); people[control].money = 0; } control = 0; NOVO: fscanf(in,"%s",guy); fscanf(in,"%d",&mondoac); fscanf(in,"%d",&numdoac); while(strcmp(guy,people[control].name)){ control++; } int j = 0; people[control].money -= mondoac; char recebe[100]; for(control=0;control<numdoac;control++){ fscanf(in,"%s",recebe); while(strcmp(recebe,people[j].name)){ j++; } people[j].money = mondoac/numdoac; } for(j=0;j<num;j++){ printf("%s %d\n", people[j].name,people[j].money); } if(!(ftell(in) == SEEK_END)){ goto NOVO; } fclose(in); fclose(out); return 0; } Ajuda por favor 😛 here since 2009 Link to comment Share on other sites More sharing options...
TheDark Posted January 2, 2010 at 02:30 PM Report Share #303663 Posted January 2, 2010 at 02:30 PM A utilização de um goto raramente se justifica. Chamar-lhe "melhor" ainda é mais raro. Um do { ... } while() tinha precisamente o mesmo efeito. O número de linhas do ficheiro está descrito no próprio ficheiro, não precisas de "ler até terminar o ficheiro". Sabes quantas pessoas existem no grupo, e sabes quantos presentes cada um deles dá. Quando se esgotarem os presentes da última pessoa, paras de ler. Desaparecido. Link to comment Share on other sites More sharing options...
Localhost Posted January 2, 2010 at 05:41 PM Author Report Share #303727 Posted January 2, 2010 at 05:41 PM Bem, eu pensei que já tinha resolvido tudo deste problema mas afinal não e eu não consigo perceber este erro. Eles no exemplo em que dão do problema dois dizem que o Dave ganhou 302, e eu tenho 300, fiz as contas aqui e dá-me 300, não consigo perceber porque é que dá 302 a eles , será um problema de arredondamentos? here since 2009 Link to comment Share on other sites More sharing options...
TheDark Posted January 2, 2010 at 05:46 PM Report Share #303730 Posted January 2, 2010 at 05:46 PM O resto da divisão inteira de 200 (o dinheiro que o Dave dá) por 3 (número de pessoas a quem o Dave dá presentes) dá quanto? Desaparecido. Link to comment Share on other sites More sharing options...
Localhost Posted January 2, 2010 at 06:01 PM Author Report Share #303737 Posted January 2, 2010 at 06:01 PM Não faço a minima ideia. A divisão dá, 66,(6)7 here since 2009 Link to comment Share on other sites More sharing options...
TheDark Posted January 2, 2010 at 06:12 PM Report Share #303741 Posted January 2, 2010 at 06:12 PM Larga a máquina de calcular e faz a divisão à mão! 😛 dividendo |divisor resto quociente 200 |3 ? 66 Desaparecido. Link to comment Share on other sites More sharing options...
Localhost Posted January 2, 2010 at 06:16 PM Author Report Share #303743 Posted January 2, 2010 at 06:16 PM Pá, vou-te ser sincero, já não me lembro de como se faz isso ? Mas para que é que é necessário isso, para resolver o problema? Edit: Talvez 0,6 xD here since 2009 Link to comment Share on other sites More sharing options...
TheDark Posted January 2, 2010 at 06:31 PM Report Share #303746 Posted January 2, 2010 at 06:31 PM http://pt.wikipedia.org/wiki/Resto_da_divis%C3%A3o_inteira Desaparecido. Link to comment Share on other sites More sharing options...
Localhost Posted January 2, 2010 at 06:39 PM Author Report Share #303749 Posted January 2, 2010 at 06:39 PM lol, agora fizeste de proposito 😛 Mas quanto ao problema, em que é que eu posso melhorar com isso? here since 2009 Link to comment Share on other sites More sharing options...
TheDark Posted January 2, 2010 at 06:56 PM Report Share #303756 Posted January 2, 2010 at 06:56 PM Fizeste as contas e deu-te 300. Faltou-te levares em consideração esta frase: Each person sets aside a certain amount of money to give and divides this money evenly among all those to whom he or she is giving a gift. No fractional money is available, so dividing 3 among 2 friends would be 1 each for the friends with 1 left over -- that 1 left over stays in the giver's "account". Daí a conversa sobre divisão inteira e resto. Desaparecido. Link to comment Share on other sites More sharing options...
Localhost Posted January 2, 2010 at 07:00 PM Author Report Share #303757 Posted January 2, 2010 at 07:00 PM Hey, pois é 😛 Podes-me ajudar a fazer uma implementação disso nisto: #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void){ FILE *in, *out; int num=0,control=0,mondoac=0; char guy[100]; int z=0; int j = 0; int ctrl; int numdoac=0; if(!(in=fopen("gift1.in","rb"))){ puts("Error opening\n"); exit(1); } if(!(out=fopen("gift1.out","wb"))){ puts("Error opening\n"); exit(1); } fscanf(in,"%d",&num); struct person{ char name[100]; int money; } people[num]; for(control=0;control<num;control++){ fscanf(in,"%s",people[control].name); people[control].money = 0; } control = 0; for(ctrl=0;ctrl<num;ctrl++){ fscanf(in,"%s",guy); fscanf(in,"%d",&mondoac); fscanf(in,"%d",&numdoac); control = 0; while(strcmp(guy,people[control].name)){ control++; } people[control].money -= mondoac; char recebe[100]; for(z=0;z<numdoac;z++){ fscanf(in,"%s",recebe); j=0; while(strcmp(recebe,people[j].name)){ j++; } people[j].money += mondoac/numdoac; } } for(j=0;j<num;j++){ fprintf(out,"%s %d\n", people[j].name,people[j].money); } fclose(in); fclose(out); return 0; } here since 2009 Link to comment Share on other sites More sharing options...
TheDark Posted January 2, 2010 at 07:12 PM Report Share #303760 Posted January 2, 2010 at 07:12 PM Posso dizer-te que o resto da divisão inteira se obtém com mondoac % numdoac, e que só precisas de acrescentar uma linha ao código para somar esse valor ao money do ofertante. Desaparecido. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now