HDU 5158 Have meal

本文详细解读了Havemeal算法问题的输入、输出规范,并提供了算法实现的示例代码,帮助读者理解如何通过报数的方式确定最终指向的食堂编号。

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5158


Have meal


Problem Description
I have been in school for several years, so I have visited all messes here. Now I have lost intersts in all of the foods. So when during the meal time, I don’t know which mess I should go to. So I came up with a solution. 
There are 4 messes in our school, I number them from 0 to 3. Then I says “Big Bing Small Jiang, Point Who Is Who!”, when I say the first word I point to the mess which is numbered 0, when I say the i-th (i>1) word I point to the mess whose number is one larger than the previous one. In case of the number of previous mess is 3, I will point to 0 again. I will go to the mess which I point to last time. Thus in this case I will go to the mess which is numbered 3. The following table explains the course of my solution to this case.
 Word I say Mess id I point to Big 0 Bing 1 Small 2 Jiang 3 Point 0 Who 1 Is 2 Who 3
I will go to university after several days, I have heard that there are so many messes in it. So I will apply my solution again. Surpose there are n messes which are numberd through 0 to n-1, and I will say m words. When I say the first word I point to the mess which is numbered 0, when I say the i-th (i>1) word I point to the mess whose number is one larger than the previous one. In case of the number of previous mess is n-1, I will point to 0 again. I will go to the mess which I point to last time. So which mess will I point to?.
It is so time-consuming to count it through manual work. So I want you to write a program to help me. Would you help me?
 

Input
Multi test cases (about 10000), every case contain two integers n and m in a single line.

[Technical Specification]
1<=n, m<=100
 

Output
For each case, output the number of the mess which I should go to.
 

Sample Input
  
4 3 1 100
 

Sample Output
  
2 0

题意:

     题意很简单,n个数字,标记为0,1,2,……n-1,组成一个环,为0,1,2,3,……,n-1,0,1,2,……,  

m次报数,问第m次报到哪一个

思路:

           如果n>=m,那么报到的就是m-1

           如果 n<m,算出循环的轮数,减去循环节 x=m-(m/n)*n ,如果刚刚循环结束,那么报到的就是n-1,否则就是 x-1


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int main()
{
    int n,m,i;
    while(~scanf("%d%d",&n,&m))
    {
        if(n>=m)
        {
            printf("%d\n",m-1);continue;
        }
        if(n==1)
        {
            printf("0\n");continue;
        }
        int x=m/n;
        x=m-x*n;
        if(x==0)
        {
            printf("%d\n",n-1);continue;
        }
        /*
        int k=1;
        for(i=0;i<n;i++)
        {
            if(k==x)
            {
                 printf("%d\n",i);break;
            }
            k++;
        }
        */
        printf("%d\n",x-1);
    }
    return 0;
}



























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值