uva846简单数学题

来源:互联网 发布:linux怎么改变用户目录 编辑:程序博客网 时间:2024/05/01 16:18

一道数学题,因为开始和结束的步数都是1,而且每一步只能比前一步大1,小1,或者和上一步相等。所以我从头和尾一起增加步数,所以用一个flag变量控制,第一次是从头开始,然后判断下还剩下的步数是否大于0,在从尾部增加。

/***************** * Author:fisty * Data:2014-12-8 * ****************/#include <cstdio>#include <cstring>#include <math.h>#include <algorithm>using namespace std;int main(){        int n, m;        int t;        scanf("%d", &t);        for(int i = 0;i < t; i++){                scanf("%d%d", &n, &m);                int cnt  = m-n;                int flag = 0;                int step = 1;                int ans = 0;                while(cnt > 0){                        cnt -= step;                        ans++;                        if(flag) ++step;//首尾推进后加1                        flag = !flag;                }                printf("%d\n", ans);        }        return 0;}


0 0
原创粉丝点击