【每日一算】开始

【金色十月线上编程比赛规则】 一个小女孩正在用左手手指数数,从1数到n。她从拇指算作1开始数起,然后,食指为2,中指为3,无名指为4,小指为5。接下来调转方向,无名指算作6,中指为7,食指为8,大拇指为9,如此反复。问最后会停在那个手指上?用编号1、2、3、4、5依次表示大拇指、食指、中指、无名指、小指。 输入格式: 输入多组数据。每组数据占一行,只包含一个整数n(1<=n<=1000000000)。 输出格式: 每组数据占一行,只包含一个介于1和5之间的整数,表示最后停留的手指


public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        List<Integer> hands = new ArrayList<Integer>();
        System.out.println("输入格式: 输入多组数据。每组数据占一行,只包含一个整数n(1<=n<=1000000000),以0结束!");
        hands.add((Integer)sc.nextInt());
        while(hands.get(hands.size()-1) != 0){
            hands.add((Integer)sc.nextInt());
        }
        hands.remove(hands.size()-1);
        int num = 0;
        Integer result = null;
        for (Integer hand : hands) {
            while (num != hand) {
                for (int i = 1; i <= 5; i++) {
                    num++;
                    if (num == hand) {
                        result = i;
                        break;
                    }
                }
                if (num == hand) {
                    break;
                }
                for (int i = 4; i >= 2; i--) {
                    num++;
                    if (num == hand) {
                        result = i;
                        break;
                    }
                }
            }
            System.out.println(result);
            num = 0;
            result = null;
        }
    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值