1 条题解

  • 0
    @ 2025-4-12 21:51:00

    C :

    /*
     * =====================================================================================
     *
     *       Filename:  902-2.c
     *
     *    Description: hahahhaha 
     *
     *        Version:  1.0
     *        Created:  2013/9/3 0:12:15
     *       Revision:  none
     *       Compiler:  gcc
     *
     *         Author:  mdk-vim.cpp-c (mdk), mengdaikun@gmail.com
     *        Company:  cjluacm-vim-mdk
     *
     * =====================================================================================
     */
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    
    int main()
    {
        //freopen("a.in","r",stdin);
    
        int n,m;
    
        int i;
    
        while(~scanf("%d %d",&n,&m))
        {
            int s = 0;
            
            for(i = 2 ; i <= n ;i++)
            {
                s = (s+m) % i;
            }
    
            printf("%d\n",s);
        }
    
        return 0;
    }
    
    

    C++ :

    #include <stdio.h>
    ///////////////////////////////////////////////////////////////////////
    // n integers (0, 1, ... n - 1) form a circle. Remove the mth from 
    // the circle at every time. Find the last number remaining 
    // Input: n - the number of integers in the circle initially
    //        m - remove the mth number at every time
    // Output: the last number remaining when the input is valid,
    //         otherwise -1
    ///////////////////////////////////////////////////////////////////////
    int LastRemaining_Solution2(int n, unsigned int m)
    {
          // invalid input
          if(n <= 0 || m < 0)
                return -1;
    
          // if there are only one integer in the circle initially,
          // of course the last remaining one is 0
          int lastinteger = 0;
    
          // find the last remaining one in the circle with n integers
          for (int i = 2; i <= n; i ++) 
                lastinteger = (lastinteger + m) % i;
    
          return lastinteger;
    }
    
    int main()
    {
    	int a,b;
    	while(scanf("%d%d",&a,&b)!=EOF)
    		printf("%d\n",LastRemaining_Solution2(a,b));
    	return 0;
    } 
    

    Pascal :

    program p2151;
    var i,m,n,ans:longint;
    begin
      while not eof do
       begin
        readln(n,m);
        ans:=0;
        for i:=2 to n do
         ans:=(ans+m) mod i;
        writeln(ans);
       end;
    end.
    

    Java :

    
    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class Main {
    
    
    	public static void main(String[] args) {
    		Scanner cScanner = new Scanner(System.in);
    		int n ,m;
    		while(cScanner.hasNext() )
    		{
    			n= cScanner.nextInt();
    			m= cScanner.nextInt();
    			getNum(n, m);
    		}
    	}
    
    	public static void getNum(int n,int m)
    	{
    		ArrayList<Integer> list = new ArrayList<Integer>();
    		
    		for( int i=0;i< n;i++)
    			list.add(i);
    		
    		int position =0;
    		while(list.size() !=1)
    		{
    			int nSize= list.size();
    			position =( position + m)% nSize -1;
    			if(position == -1)
    			{
    				position = nSize -1;
    			}
    			
    			int remove = list.remove(position);
    
    			position = position %(nSize-1);
    		}
    		System.out.println(list.get(0));
    	}
    }
    
    
    • 1

    信息

    ID
    1988
    时间
    1000ms
    内存
    32MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者