Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
hilbert
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
/* Hilbert Curve */ #include <iostream> #include <math.h> using namespace std; int hilbert(int x, int y, int iter){ if(iter ==0) return 1; int n = iter-1; int num = 1<<(2*n); // 4^n = 2^(2n) = 2^n * 2^n = 1<<(2*n) int len = 1<<n; // 2^n = 1<<n; int res = num; if(x >= len && y >=len) // The first quadrant res = num * 2 + hilbert(x-len, y-len, iter-1); // shape is the same else if (x<len && y>=len) // 2nd quadrant res = num + hilbert(x, y-len, iter-1); else if (x<len && y < len) // 3rd res = hilbert(y, x, iter-1); //clock-wise 90 degree else res = num*3 + hilbert( len-y, len-x+len, iter-1); // len-1-y, len-1-x return res; } int main(){ cout << "(0,1,1)=2: " << hilbert(0,1,1) << endl; cout << "(1,1,2)=3: " << hilbert(1,1,2) << endl; cout << "(2,2,2)=9: " << hilbert(2,2,2) << endl; return 0; }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.43 sec, absolute running time: 0.07 sec, cpu time: 0.01 sec, memory peak: 3 Mb, absolute service time: 0,51 sec
edit mode
|
history
|
discussion
(0,1,1)=2: 2 (1,1,2)=3: 3 (2,2,2)=9: 9