CodeForces - 1217A Creating a Character
题目:
You play your favourite game yet another time. You chose the character you didn’t play before. It has str points of strength and int points of intelligence. Also, at start, the character has exp free experience points you can invest either in strength or in intelligence (by investing one point you can either raise strength by 1 or raise intelligence by 1).
Since you’d like to make some fun you want to create a jock character, so it has more strength than intelligence points (resulting strength is strictly greater than the resulting intelligence).
Calculate the number of different character builds you can create (for the purpose of replayability) if you must invest all free points. Two character builds are different if their strength and/or intellect are different.
Input
he first line contains the single integer T (1≤T≤100) — the number of queries. Next T lines contain descriptions of queries — one per line.
This line contains three integers str, int and exp (1≤str,int≤108, 0≤exp≤108) — the initial strength and intelligence of the character and the number of free points, respectively.
output
Print T integers — one per query. For each query print the number of different character builds you can create.
Sample Input
4
5 3 4
2 1 0
3 5 5
4 10 6
Sample Output
3
1
2
0
题目大意:
这题大意是分配exp点经验给力量s和智力i,求有多少种分配情况使s比i高
题目分析:
这个就是一个分情况讨论,不可以,模拟,会TLE
我先分 exp = 0 和 exp > 0 然后在 exp > 0 里再分,考虑的时候 我们先考虑把 s 和 i 补齐 (即 用exp来先初始分派 使 s=i )具体看代码,有注释。
AC代码:
#include <stdio.h>
int main()
{
long int T,st,inte,ex,ans,i=0;
scanf("%ld",&T);
for(i=0;i<T;i++)
{
scanf("%ld%ld%ld",&st,&inte,&ex);
if (ex==0)
{
if(st>inte)
ans=1;
else
ans=0;
}//没有经验可以分派,初始s > i ans就是 1 否则为 0;
if(ex>0)
{
if(st==inte)
ans=(ex+1)/2;//给 exp+1 是为了统一 exp是奇数的情况。可以在纸上试一下,
if(st>inte)
{
int c;
c=st-inte;//这是两个的差值,
if(c==ex) //如果一开始 s就比 i多 exp 个 ,那么每次单独给 s加1个
ans=ex; //都满足,所以一共可以有 exp种。
if(c>ex) //如果一开始 s就比 i多的比 exp还多,那在上一个的基础上 加一种
ans=ex+1; // 把经验全部分给 i 的情况 ( s分0个经验 )
if(c<ex) //如果一开始 s 比 i 多的比 exp 少,那么从 1 个开始给 s 加
// 可能造成 i比s多(因为剩下的 exp 给了 i),exp-c得到把 插多的补齐
//后剩下的exp 回到第一种,然后还要加上从差值,有了下面这个公式,可以自己在纸上模拟几个。
ans=(c+1)+(ex-c+1)/2-1; //ans=c+(ex-c+1)/2;
}
if(st<inte)//这种就比较简单了,大家更具上面的 理解一下就好
{int c;
c=inte-st;
if(ex<=c)
ans=0;
else
ans=(ex-c+1)/2;
}
}
printf("%ld\n",ans);
}
}
我来要赞了,如果觉得解释还算详细,可以学到点什么的话,点个赞再走吧
欢迎各位路过的dalao 指点,提出问题。