切蛋糕、切西瓜这类问题一般都有固定的公式,告诉大家一个技巧:二维的一般是an^2+bn+c,三维的一般是an^3+bn^2+cn+d.
用带定系数法求出各个系数就OK了,不用想破脑筋找规律。。。
这种方法对类似问题都可以,变通一下好多问题都可以迎刃而解了。。。
贴出HUSTOJ上的两道题目:
Problem 1577
切西瓜
Time Limit: 1 Sec Memory Limit: 128 MB
Submissions: 233 Solved: 35
Description
众所周知 一刀可以把一个西瓜切成两半 两刀最多可以把西瓜切成四半 三刀最多可以把西瓜切成八块 那么问你 N 刀最多可以把西瓜切成多少块咧?
Input
总共有T(T<=1000)组测试数据 在第一行中给出 接下来有T行 每一行有一个整数N(N<=1000)
Output
输出结果共有T行,每行一个整数,即该行的N对应的答案
Sample Input
3
1
2
3
Sample Output
2
4
8
#include <stdio.h>
#include <stdlib.h>
int main()
{
int cas;
int n;
/*freopen("1.txt","r",stdin);*/
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&n);
printf("%d\n",(n*n*n+5*n+6)/6);
}
return 0;
}
Problem 1578
切蛋糕
Time Limit: 1 Sec Memory Limit: 128 MB
Submissions: 185 Solved: 66
Description
众所周知 一刀可以把一个蛋糕切成两半 两刀最多可以把一个蛋糕切成四半 三刀最多可以把一个蛋糕切成七块 那么问你 N 刀最多可以把一个蛋糕切成多少块咧?
Input
总共有T(T<=1000)组测试数据 在第一行中给出 接下来有T行 每一行有一个整数N(N<=1000)
Output
输出结果共有T行,每行一个整数,即该行的N对应的答案
Sample Input
3
1
2
3
Sample Output
2
4
7
#include <stdio.h>
#include <stdlib.h>
int main()
{
int cas;
int n;
/*freopen("1.txt","r",stdin);*/
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&n);
printf("%d\n",(n*n*n+5*n+6)/6);
}
return 0;
}