Crazy Number
Time Limit: 1000 ms
Memory Limit: 65536 KiB
Problem Description
给定一个整数 n ,把 n 分解成从 1 开始连续的若干个整数(最大为 9)及 n' 的和,即 n = 1 + 2 + 3 + ... + 9 + n',直到 n’ 为 0 时停止分解。
例如:给定 n = 9 时:
- 第一次分解:9=1+2+3+n'(9 < 1+2+3+4,因此最多只能分解为 1+2+3+n',剩余值 n' 作为下一次分解的 n)。
- 第二次分解:3=1+2。
Input
输入数据有多组(数据组数不超过 2600),到 EOF 结束。
对于每组数据,输入一行,包含一个整数 n (0 <= n <= 2575)。
Output
对于每组数据:
- 第一行输出 ”Case #t:”,t 从 1 开始计数。
- 接下来输出分解的结果,每个结果占一行。特别地,当 n = 0 时,分解结果仅在一行中输出一个 "0"。
Sample Input
1 9 46 55 0
Sample Output
Case #1: 1=1 Case #2: 9=1+2+3+n' 3=1+2 Case #3: 46=1+2+3+4+5+6+7+8+9+n' 1=1 Case #4: 55=1+2+3+4+5+6+7+8+9+n' 10=1+2+3+4 Case #5: 0
Hint
#include <cstdio> #include <algorithm> #include <bits/stdc++.h> using namespace std; int main() { int n,t=0; while(cin>>n) { t++; printf("Case #%d:\n",t); if(n==0) cout<<0<<endl; else { int s=0; printf("%d=",n); while(1) { for(int i=1;i+s<=n&&i<10;i++) { s+=i; if(i==1) printf("1"); else printf("+%d",i); } if(s==n) { printf("\n"); break; } else { printf("+n'\n"); n-=s; printf("%d=",n); s=0; } } } } return 0; }