跳到正文

题解

AT_abc001_1 [ABC001A] 積雪深差

akaryan约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟

版本

  1. v1当前

题目传送器

题目意思:

要求出H1-H2,也就是A-Bproblem

思路:

本题做法有很多,暴力、二分和普遍做法

暴力代码:

C++
#include<bits/stdc++.h>
using namespace std;
int n , m , i;
int main() {
    scanf("%d%d" , &n , &m);
    while(1){
        if (n - m == i){
            printf("%d\n" , i);
            return 0;
        }
        i ++;
    }
	return 0;
}

二分代码:

C++
#include <bits/stdc++.h>
using namespace std;
int n , m , l = -1e4 , r = 1e4;
int main(){
	 scanf("%d%d" , &n , &m);
     while(l + 1 < r){
         int mid = (l + r) / 2;
	     if (mid == n - m){
	         printf("%d\n" , mid);
             return 0;
         }
         else if(mid < n - m) l = mid;
		 else r = mid;
	 }
	 return 0;
 }

普通做法:

C++
#include <bits/stdc++.h>
using namespace std;
int n , m;
int main(){
	 scanf("%d%d" , &n , &m);
	 printf("%d\n" , n - m);
	 return 0;
 }
原文