Не так красиво, как предыдущая запись С, но вот моя.
примечание: я пишу, потому что он использует другой подход, чем предыдущий, в основном
он работает с вводом> 9 (две цифры - без трюка -47
)
enum directions_e { dx, up, sx, dn } direction;
int main (int argc, char **argv) {
int len = atoi(argv[1]);
int offset = 2*len-1;
int size = offset*offset;
char *matrix = malloc(size);
int startfrom = 2*len*(len-1);
matrix[startfrom] = 1;
int next = startfrom;
int count = 1;
int i, step = 1;
direction = dx ;
for (;; step++ )
do {
for ( i = 0 ; i < step ; i++ ) {
switch ( direction ) {
case dx:
next++;
break;
case up:
next = next - offset;
break;
case sx:
next--;
break;
case dn:
next = next + offset;
}
int div = ++count;
do {
div--;
} while ( count % div );
if ( div > 1 ) {
matrix[next] = ' ';
}
else {
matrix[next] = '*';
}
if (count >= size) goto dontusegoto;
}
direction = ++direction % 4;
} while ( direction %2);
dontusegoto:
for ( i = 0 ; i < size ; i++ ) {
putchar(matrix[i]);
if ( !((i+1) % offset) ) putchar('\n');
}
return 0;
}
, который, адекватно переведенный в нечитаемый С, становится 339 символами.
компилировать с: gcc -o ulam_compr ulam_compr.c
работает с osx
i686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5465)
и Debian Lenny.
main(int a,char**v){
int l=atoi(v[1]),o=2*l-1,z=o*o,n=2*l*(l-1),c=1,i,s=1,d;
char*m=malloc(z);
m[n]=1;
for(;;s++)do{
for(i=0;i<s;i++){
if(d==0)n++;
else if(d==1)n-=o;
else if(d==2)n--;
else n+=o;
int j=++c;
while(c%--j);
if(j>1)m[n]=' ';else m[n]='*';
if(c>=z)goto g;
}d=++d%4;}while(d%2);
g:for(i=0;i<z;i++){
putchar(m[i]);
if(!((i+1)%o))putchar('\n');
}
}
Вот некоторые результаты:
$ ./ulam_compr 3
* *
* *
* **
*
*
$ ./ulam_compr 5
* *
* *
* * *
* * *
* ** *
* *
* *
* *
* *
Вы не t необходимо проверить, больше ли оно 0 и меньше W, поскольку положение указателя x не может быть меньше 0 или больше W. То же самое относится к оси Y. Следующего должно быть достаточно:
if((x>w2 || x<w1) || (y>h2 || y<h1)){
console.log("We are in the outer area");
}
Вы можете обернуть всю свою страницу в DIV (называемый внешний
), а затем подключиться к наведению
событие.