您的位置:首页 > 其它

【每日一算】开始

2014-10-24 02:09 176 查看
【金色十月线上编程比赛规则】 一个小女孩正在用左手手指数数,从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;
}
}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: