历届试题 蚂蚁感冒
2017-03-06 21:03
295 查看
问题描述
长100厘米的细长直杆子上有n只蚂蚁。它们的头有的朝左,有的朝右。
每只蚂蚁都只能沿着杆子向前爬,速度是1厘米/秒。
当两只蚂蚁碰面时,它们会同时掉头往相反的方向爬行。
这些蚂蚁中,有1只蚂蚁感冒了。并且在和其它蚂蚁碰面时,会把感冒传染给碰到的蚂蚁。
请你计算,当所有蚂蚁都爬离杆子时,有多少只蚂蚁患上了感冒。
输入格式
第一行输入一个整数n (1 < n < 50), 表示蚂蚁的总数。
接着的一行是n个用空格分开的整数 Xi (-100 < Xi < 100), Xi的绝对值,表示蚂蚁离开杆子左边端点的距离。正值表示头朝右,负值表示头朝左,数据中不会出现0值,也不会出现两只蚂蚁占用同一位置。其中,第一个数据代表的蚂蚁感冒了。
输出格式
要求输出1个整数,表示最后感冒蚂蚁的数目。
样例输入
3
5 -2 8
样例输出
1
样例输入
5
-10 8 -20 12 25
样例输出
3
长100厘米的细长直杆子上有n只蚂蚁。它们的头有的朝左,有的朝右。
每只蚂蚁都只能沿着杆子向前爬,速度是1厘米/秒。
当两只蚂蚁碰面时,它们会同时掉头往相反的方向爬行。
这些蚂蚁中,有1只蚂蚁感冒了。并且在和其它蚂蚁碰面时,会把感冒传染给碰到的蚂蚁。
请你计算,当所有蚂蚁都爬离杆子时,有多少只蚂蚁患上了感冒。
输入格式
第一行输入一个整数n (1 < n < 50), 表示蚂蚁的总数。
接着的一行是n个用空格分开的整数 Xi (-100 < Xi < 100), Xi的绝对值,表示蚂蚁离开杆子左边端点的距离。正值表示头朝右,负值表示头朝左,数据中不会出现0值,也不会出现两只蚂蚁占用同一位置。其中,第一个数据代表的蚂蚁感冒了。
输出格式
要求输出1个整数,表示最后感冒蚂蚁的数目。
样例输入
3
5 -2 8
样例输出
1
样例输入
5
-10 8 -20 12 25
样例输出
3
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); int N = in.nextInt(); int[] Loction = new int[N+1]; int[] direction = new int[N+1]; for ( int i = 1 ; i <= N ; i++){ Loction[i] = in.nextInt(); direction[i] = Loction[i] < 0 ?0:1; } int ill_direction = (Loction[1] < 0)?0:1; int right = 0; int left = 0; int x = 0; int y = 0; for ( int i = 2 ; i <= N ; i++){ //病蚂蚁向右走,蚂蚁i向左走,并且蚂蚁i在病蚂蚁的右边 if ( Math.abs(Loction[1]) < Math.abs(Loction[i]) && Loction[1] > 0 && direction[i] == 0){ right++; } //病蚂蚁向左走,蚂蚁i向右走,并且蚂蚁i在病蚂蚁的左边 if ( Math.abs(Loction[1]) > Math.abs(Loction[i]) && Loction[1] < 0 && direction[i] == 1){ left++; } if ( Math.abs(Loction[1]) > Math.abs(Loction[i]) && Loction[1] > 0 && direction[i] == 1){ x++; } if ( Math.abs(Loction[1]) < Math.abs(Loction[i]) && Loction[1] < 0 && direction[i] == 0){ y++; } } if ( ill_direction == 1){ if ( right != 0){ System.out.print(x+right+1); }else{ System.out.print(1); } }else{ if (left != 0){ System.out.print(y+left+1); }else{ System.out.print(1); } } in.close(); } }
相关文章推荐
- 【历届试题 蚂蚁感冒】(思维)
- 历届试题 蚂蚁感冒
- 蓝桥杯--历届试题 蚂蚁感冒 【思维】
- 算法笔记_175:历届试题 蚂蚁感冒(Java)
- 历届试题 蚂蚁感冒
- 历届试题 蚂蚁感冒
- 蓝桥杯 历届试题 蚂蚁感冒
- 历届试题 蚂蚁感冒(创建了对象、不创建对象)
- 蓝桥杯 历届试题 蚂蚁感冒
- 蓝桥杯历届试题——蚂蚁感冒
- 蓝桥杯 历届试题 蚂蚁感冒 (模拟)
- 历届试题 蚂蚁感冒
- 历届试题 PREV-27 蚂蚁感冒
- 蓝桥杯历届试题 蚂蚁感冒(模拟)
- 蓝桥杯 历届试题 蚂蚁感冒(模拟)
- 蓝桥杯历届试题---蚂蚁感冒
- 蓝桥杯历届试题——蚂蚁感冒
- 历届试题 蚂蚁感冒 蓝桥杯
- 【蓝桥杯】历届试题 蚂蚁感冒
- 蓝桥杯 历届试题 蚂蚁感冒 (思维题)