zoukankan      html  css  js  c++  java
  • [每日一题]:牛客练习赛61 A:打怪

    题目:

    考察点:

    数学、模拟
    

    侃侃:

    其实是一道水题,但是比较菜的我最后差了一点,总之还是能力不行,没有AC,最后依靠模拟实现 AC。
    因为这道题的数据量不大,最多 10^6,所以模拟是完全可以过的。
    不过总算还是学到了一点知识,当用循环去解决问题时,看是否可以转化成一个式子进行解决。
    这点非常重要。
    

    模拟代码:

    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
     
    using namespace std;
     
    int t;
    int h,a,H,A;
     
    int main(void) {
        scanf("%d",&t);
        while(t --) {
            int ans = 0;
            scanf("%d%d%d%d",&h,&a,&H,&A);
            if(A <= 0 && a != 0) {
                puts("-1");
                continue;
            }
            if(a <= 0) {
                puts("0");
                continue;
            }
            if(a >= H) {
                printf("%d
    ",-1);
                continue;
            }
            if(h <= A && a < H) {
                puts("0");
                continue;
            }
            while(h > 0) {
                int value = H;
                while(value > 0) {
                    value -= a;
                    if(value <= 0) break;
                    h -= A;
                    if(h <= 0) break;
                }
                if(h > 0) ans ++;
                else break;
            }
            printf("%d
    ",ans);
     
        }
    

    数学式子 代码:

    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    int t;
    int h,a,H,A;
    
    int main(void) {
    	scanf("%d",&t);
    	while(t --) {
    		int ans = 0;
    		scanf("%d%d%d%d",&h,&a,&H,&A);
    		if(A <= 0 && a != 0) {
    			puts("-1");
    			continue;
    		}
    		if(a <= 0) {
    			puts("0");
    			continue;
    		}
    		if(a >= H) {
    			printf("%d
    ",-1);
    			continue;
    		}
    		if(h <= A && a < H) {
    			puts("0");
    			continue;
    		}
    		int res = H % a;
    		if(res == 0) {
    			int cnt = H / a;
    			int flood = (cnt - 1) * A;
    			// 这里我竟然傻傻的去用循环,还没搞对,至少要留一滴血,不然自己就死了,还如何战斗 
    			ans = (h - 1) / flood;
    		} else {
    			int cnt = H / a + 1;
    			int flood = (cnt - 1) * A;
    			ans = (h - 1) / flood;
    		}
    		printf("%d
    ",ans);
    	}
    	return 0;
    }
    

    用循环可以这样写:

    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
     
    using namespace std;
     
    int t;
    int h,a,H,A;
     
    int main(void) {
        scanf("%d",&t);
        while(t --) {
            int ans = 0;
            scanf("%d%d%d%d",&h,&a,&H,&A);
            if(A <= 0 && a != 0) {
                puts("-1");
                continue;
            }
            if(a >= H) {
                puts("-1");
                continue;
            }
            int res = H % a;
            if(res == 0) {
                int cnt = H / a;
                int flood = (cnt - 1) * A;
                while(h > 1) {
                    h -= flood;
                    if(h <= 0) break;
                    ans ++;
                }
            } else {
                int cnt = H / a + 1;
                int flood = (cnt - 1) * A;
                while(h > 1) {
                    h -= flood;
                    if(h <= 0) break;
                    ans ++;
                }
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    

    后记:

    学会去勇敢的面对你所畏惧的一切,当你选择面对时,那时的你一定很棒。
  • 相关阅读:
    vim delete
    npm ERR! network connect ETIMEDOUT
    在 ubuntu 14.04 Unity 中清除和关闭 Totem 播放记录
    ubuntu 14.04 上 jvpn 使用说明
    LWP::Protocol::https not installed
    perl 安装模块
    触摸屏工作方式
    如何检测死锁并快速定位死锁位置
    如何用 yum 的一个包替换另一个包
    shell 中 here documemt << 与 <<- 的区别
  • 原文地址:https://www.cnblogs.com/prjruckyone/p/12682953.html
Copyright © 2011-2022 走看看