1341-A-B Game(找规律) ZCMU

本文介绍了一个特殊的二人游戏算法问题:给定两个整数A和B,玩家通过一系列操作使A减少到小于或等于B,目标是最小化操作次数。文章提供了输入输出样例及解析程序代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Fat brother and Maze are playing a kind of special (hentai) game by two integers A and B. First Fat brother write an integer A on a white paper and then Maze start to change this integer. Every time Maze can select an integer x between 1 and A-1 then change A into A-(A%x). The game ends when this integer is less than or equals to B. Here is the problem, at least how many times Maze needs to perform to end this special (hentai) game.

Input

The first line of the date is an integer T, which is the number of the text cases.

Then T cases follow, each case contains two integers A and B described above.

1 <= T <=100, 2 <= B < A < 100861008610086

Output

For each case, output the case number first, and then output an integer describes the number of times Maze needs to perform. See the sample input and output for more details.

Sample Input

2

5 3

10086 110

Sample Output

Case 1: 1

Case 2: 7

代码

#include<stdio.h>
int main()
{
    int T,kase=1;
    scanf("%d",&T);
    while(T--)
    {
        long long a,b,cnt;
        scanf("%lld%lld",&a,&b);
        cnt=0;
        while(a>b)
        {
            if(a%2==0)
                a-=a/2-1;
            else
                a-=a/2;
            cnt++;
        }
        printf("Case %d: %lld\n",kase++,cnt);
    }
    return 0;
}

 

### zcmu Java 解与代码实现 在讨论与 zcmu 相关的 Java 内容时,可以参考一些常见的算法解和代码实现。以下是基于提供的引用内容以及其他知识背景生成的内容。 #### 1. Java 实现字符串头尾相连问 此问的核心在于判断一个字符串是否可以通过头尾相连后形成回文串。通过将字符串复制并反转,检查原字符串是否为反转后字符串的子串来实现[^5]。 ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); scanner.nextLine(); // 清除换行符 while (t-- > 0) { String xl = scanner.nextLine(); String s = xl + xl; // 将字符串复制一遍 StringBuilder reversed = new StringBuilder(s).reverse(); // 反转字符串 if (reversed.toString().contains(xl)) { // 判断是否包含原字符串 System.out.println("YES"); } else { System.out.println("NO"); } } } } ``` #### 2. Java 实现最短路径问 对于类似灯塔覆盖范围的问,可以通过遍历所有灯塔并更新可达范围来解决。如果能够到达终点且返回起点,则答案为 `(终点 - 起点) * 3`,否则输出 `-1`[^3]。 ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while (t-- > 0) { int n = scanner.nextInt(); long startX = scanner.nextLong(); long startR = scanner.nextLong(); long m = startX + startR; boolean canReach = true; for (int i = 1; i < n; i++) { long x = scanner.nextLong(); long r = scanner.nextLong(); if (x <= m && x + r > m) { m = x + r; } else if (x > m) { canReach = false; break; } } if (!canReach || startX > m) { System.out.println("-1"); continue; } m = startX - startR; for (int i = n - 2; i >= 0; i--) { long x = scanner.nextLong(); long r = scanner.nextLong(); if (x >= m && x - r < m) { m = x - r; } else if (x < m) { canReach = false; break; } } if (!canReach || startX < m) { System.out.println("-1"); } else { System.out.println((startX - startX) * 3); } } } } ``` #### 3. Java 实现字符串统计问 对于字符串统计问,可以使用 `HashMap` 来记录每个字符串出现的次数,并到出现次数最多的字符串[^2]。 ```java import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNextInt()) { int n = scanner.nextInt(); if (n == 0) break; Map<String, Integer> map = new HashMap<>(); for (int i = 0; i < n; i++) { String str = scanner.next(); map.put(str, map.getOrDefault(str, 0) + 1); } String result = ""; int maxCount = 0; for (Map.Entry<String, Integer> entry : map.entrySet()) { if (entry.getValue() > maxCount) { maxCount = entry.getValue(); result = entry.getKey(); } } System.out.println(result); } } } ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值