fork download
  1. #include <stdio.h>
  2. #define min(a, b) ((a) < (b) ? (a) : (b))
  3. #define max(a, b) ((a) < (b) ? (b) : (a))
  4. const char *f(int current, int size, int width) {
  5. static char buff[256], *p;
  6. int b = min(max(1, current - width / 2), size - width + 1), e = b + width, i;
  7. for (p = buff, i = b; i < e; i++) {
  8. if (b < i) *p++ = ' ';
  9. p += sprintf(p, i == current ? "[%d]" : "%d", i);
  10. }
  11. return buff;
  12. }
  13. int main() {
  14. #define g(a, b, c) printf("%s\n", f(a, b, c))
  15. g(1,10,5), g(5, 10, 5), g(10, 10, 5);
  16. return 0;
  17. }
  18.  
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
[1] 2 3 4 5
3 4 [5] 6 7
6 7 8 9 [10]