• 2013.6.15吃果子问题


    问题描述如下:


    代码 :

    #include <stdio.h>
    #define  N 300
    
    int A[N];
    
    int main()
    {
    	int n,m;
    	int step,pos;    //记录第几步,当前位置
    	int count;   //当前果子数目;
    	int tag;		//记录当前吃掉的果子
    	int lastPos;    
    	bool flag;
    	while (scanf("%d%d",&n,&m)==2)
    	{
    		for (int i=1;i<=n;++i)
    		{
    			A[i]=i;
    		}
    		step=1; pos=1;
    		count=n;
    		tag=0;
    		lastPos=0;
    		flag=true;
    		while (step<=m)
    		{
    			pos +=(step*step*step%5+1);   //jump
    			if (pos>count)
    			{
    				if (pos==lastPos)
    				{
    					//printf("-1
    ");
    					flag=false;
    					break;
    				}
    				lastPos=pos;
    				pos=1;                   //回到起点重新跳
    				continue;
    			}else
    			{
    				tag=A[pos];
    				//printf("%d
    ",tag);
    				A[pos]=-1; 
    				for (int j=pos+1;j<=count;++j)
    				{
    					A[j-1]=A[j];          //果子下落
    				}
    				count--;
    			}
    			step++;
    		}
    		if (flag)
    		{
    			printf("%d
    ",tag);
    		}
    		else
    		{
    			printf("-1
    ");
    		}
    		
    	}
    	return 0;
    }


  • 相关阅读:
    PHP验证码
    c#属性
    框架数据连接类
    mysqli
    C# 委托
    Windows系统,文件和文件夹命名规则:
    固态硬盘与普通硬盘的区别
    windows快捷键
    1.认识计算机
    计算机题解
  • 原文地址:https://www.cnblogs.com/wuhayaoshenmeai/p/3361875.html
Copyright © 2020-2023  润新知