Codeforces #667 (Div. 3) A. Yet Another Two Integers Problem

A. Yet Another Two Integers Problem(水题)

Rating 800 Context ~~
题目链接入口 Click here~~
在这里插入图片描述
题目描述
输入a,b两个数,通过a+k或者a-k操作k∈[1,10],将a变成b。
输入
第一行输入案例个数
第二行一次输入 a , b a,b a,b
输出
输出在n次 a-- 或者 b-- 的操作后,a×b 最小的结果。
案例
输入案例

6
5 5
13 42
18 4
1337 420
123456789 1000000000
100500 9000

输出案例

0
3
2
92
87654322
9150

题解:
例子:13 → 23 → 33 → 42
先把A和B的是大小调整为,A≤B,由于k∈[1,10],所以可以直接枚举从10到1,详情看代码(水题,一看就懂的)
C++代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>

#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define int ll
#define INF 0x3f3f3f3f
using namespace std;
int read()
{
	int w = 1, s = 0;
	char ch = getchar();
	while (ch < '0' || ch>'9') { if (ch == '-') w = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0';    ch = getchar(); }
	return s * w;
}
//------------------------ 以上是我常用模板与刷题几乎无关 ------------------------//
signed main()
{
	int t = read();
	while(t--)
	{
		int a = read();
		int b = read();
		if(a > b)
			swap(a ,b);
		int tmp = b - a;
		int cnt = 0;
		if (tmp / 10)
		{
			cnt = tmp / 10;
			tmp = tmp % 10;
		}
			
		if (tmp / 9)
		{
			cnt += tmp / 9;
			tmp = tmp % 9;
		}
			
		if (tmp / 8)
		{
			cnt += tmp / 8;
			tmp = tmp % 8;
		}
			
		if (tmp / 7)
		{
			cnt += tmp / 7;
			tmp = tmp % 7;
		}
		if (tmp / 6)
		{
			cnt += tmp / 6;
			tmp = tmp % 6;
		}
		if (tmp / 5)
		{
			cnt += tmp / 5;
			tmp = tmp % 5;
		}
		if (tmp / 4)
		{
			cnt += tmp / 4;
			tmp = tmp % 4;
		}
		if (tmp / 3)
		{
			cnt += tmp / 3;
			tmp = tmp % 3;
		}
		if (tmp / 2)
		{
			cnt += tmp / 2;
			tmp = tmp % 2;
		}
		cnt += tmp;
		printf("%d\n", cnt);
	}
	return 0;
} 

Java代码:

import java.util.Scanner;
public class codeforces {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- != 0) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            if (a > b) {
                int temp = a;
                a = b;
                b = temp;
            }
            int tmp = b - a;
            int cnt = 0;
            if (tmp / 10 != 0) {
                cnt = tmp / 10;
                tmp = tmp % 10;
            }
 
            if (tmp / 9 != 0) {
                cnt += tmp / 9;
                tmp = tmp % 9;
            }
 
            if (tmp / 8 != 0) {
                cnt += tmp / 8;
                tmp = tmp % 8;
            }
 
            if (tmp / 7 != 0) {
                cnt += tmp / 7;
                tmp = tmp % 7;
            }
            if (tmp / 6 != 0) {
                cnt += tmp / 6;
                tmp = tmp % 6;
            }
            if (tmp / 5 != 0) {
                cnt += tmp / 5;
                tmp = tmp % 5;
            }
            if (tmp / 4 != 0) {
                cnt += tmp / 4;
                tmp = tmp % 4;
            }
            if (tmp / 3 != 0) {
                cnt += tmp / 3;
                tmp = tmp % 3;
            }
            if (tmp / 2 != 0) {
                cnt += tmp / 2;
                tmp = tmp % 2;
            }
            cnt += tmp;
            System.out.println(cnt);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值