String Handling... *DUH*
Heya.
As i write this i admit i am blushing rather violently. This shouldn''t be a hard thing to but i just get my head around it...
Basically i am trying to do the following.
Get a program to generate a strings of 5 random numbers, using the numbers 1-5. OK so far! But the problems begin with the filtering rules:
(i) must only use each digit once (no repetitions within string)
(ii) the next string can''t begin with the same first digit as the last string
(iii) (the hard one i think) the next string can''t have any 3 numbers in the same order as they were in the preceeding string.
so this would be invalid:
1,5,4,3,2
5,4,3,2,1
(cos of the 4,3,2 thingy).
I would be embaressingly grateful for any help you might care to furnish me with! (C,C++ or any dialect of BASIC)
R.
First I would put the numbers to arrays. When done processing make the strings. Unless you need to make many strings it wouldn''t make any difference if your algorithms are n^2. So you can take the easiest sollution, namly check everything for everything. I am sure you know how, if you think.
Yeah...umm. That sort of how i''ve tried to implement it. But the checking everything for everything part is still a pain to do. I was sort of hoping that buried deep within C++ there was a some sort of funky string-handling fuction which would do it...
Well, My first incling would be use an array and store the previously generated string. Then I would generate a new string and then compare each element. If you find anything in order, just do a swap of the first one.
string 1
1,3,5,2,6
string 2a
3,1,5,2,6 -> after comparion
3,1,2,5,6
Problem solved!
Try that out
Kevin =)
string 1
1,3,5,2,6
string 2a
3,1,5,2,6 -> after comparion
3,1,2,5,6
Problem solved!
Try that out
Kevin =)
-----------------------------kevin@mayday-anime.comhttp://www.mayday-anime.com
Probably not the best way to do it. but it appears to work right and is pretty simple.
#include <stdio.h>#include <stdlib.h>#include <string.h>int check3(char *a, char *b) {char dat[6]; int i;for (i=0; i<3; i++) {strcpy(dat, a);dat[i+3]=0;if (strstr(b, dat+i)) return 1; }return 0;}char last[6];void randspecial(char *n) { int taken[5]={0,0,0,0,0}; int i; for (i=0; i<5; i++) { int r = rand()%5; while(taken[r]) {r++; r=r%5;} n<i>=r+''1''; taken[r]=1; } if (check3(n, last)) {randspecial(n);} strcpy(last, n);}void main() { while (getch()!=27) { char dat[6]; dat[5]=0; randspecial(dat); printf("\n%s", dat); }}
cmaker- I do not make clones.
You will need lots of loops. In the common loop you will need to test the new array with all the others, one by one, unfortunately.
import money.*;#include "cas.h"uses bucks;
I forgot to tell you, don´t test 3 against 3 all the time. Optimize it, use 1 by 1. If there is some equal numbers, test the next int of the matching arrays. Only if this happens you need to test the third. This will get it almost 3 times faster.
import money.*;#include "cas.h"uses bucks;
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement