题解 | #尼科彻斯定理#
尼科彻斯定理
https://www.nowcoder.com/practice/dbace3a5b3c4480e86ee3277f3fe1e85
#include <stdio.h> #include <stdlib.h> /* * m * m 为中间那个数往前后算,m为奇数±2*i,m为奇数±(2*i + 1), */ int compar(const void* str, const void* str2) { return (*(int *)str - *(int *)str2); } int main() { int m; int i, j; int aa[100] = {0}; while (scanf("%d", &m) != EOF) { if (m == 1) { aa[0] = m*m; } else if (m == 2) { aa[0] = m*m - 1; aa[1] = m*m + 1; } else if (m % 2 != 0) { aa[0] = m * m; for (j = 1; j <= m / 2; j += 1) { aa[2*j - 1] = m * m - 2* j ; aa[2*j] = m * m + 2* j ; } } else { for (j = 0; j < m / 2; j += 1) { aa[j*2] = m * m - 2*j - 1 ; aa[j*2 + 1] = m * m + 2* j + 1; } } qsort(aa, 100, sizeof(int), compar); for(i = 0; i < 100; i ++) { if (aa[i] != 0) { printf("%d", aa[i]) ; if (aa[i+1] != 0) { printf("+") ; } } } } return 0; }