NOIP幂次方
#include<stdio.h> int c[20] = { 1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768 };//由题意n最大为20000,所以最多会用到2的14次方 //为了防止mid+1出错,故写到15次方 int binarySearch(int x, int mid) { if (x >= c[mid] && x < c[mid + 1]) { return mid; } if (x >= c[mid + 1]) return 20; return -1; } int serch(int x) {//利用二分查找找到x的最大二次方 int left = 0; int right = 15; int mid; while (left <= right) { mid = (left + right) >> 1; if (binarySearch(x, mid) == -1) { right = mid - 1; } else if (binarySearch(x, mid) == 20) { left = mid + 1; } else { return mid; } } } void divite(int x) {//分治求解:因为每个整数的划分方法是一样的 if (x == 0) return; int flag = serch(x); int li = x - c[flag]; if (flag == 0) { printf("2(0)"); } if (flag == 1) { printf("2"); } if (flag > 1) { printf("2("); divite(flag); printf(")"); } if (li > 0) { printf("+"); divite(li); } } int main() { int n; scanf("%d", &n); divite(n); return 0; }