Sdnu 1206. Ants Cold "code so simple, thinking practice" "July 29"

Source: Internet
Author: User

Ants Cold

Descriptionthere are n ants on a slender straight pole 100 centimeters long. They have their heads on the left, some facing to the right. Each ant can only climb along the pole, at a speed of 1 centimeters per second. When two ants meet, they will turn around at the same time and crawl in the opposite direction. Of these ants, 1 ants have a cold. And when they meet with other ants, they will infect the ant with the cold. Please calculate how many ants have a cold when all the ants are climbing away from the pole. Input

The first line enters an integer n (1 < n < 50), which represents the total number of ants.

The next line is n a space-separated integer XI ( -100 < Xi <), the absolute value of Xi, indicating the distance the ant leaves the left end of the pole. A positive value indicates a head to the right, a negative value indicates a head to the left, no 0 values appear in the data, and no two ants occupy the same position. Among them, the first data represents the ants catching a cold.

Outputrequires an output of 1 integers, indicating the number of last cold ants. Sample Input
35-2 8
Sample Output
1
isn't there a train of thought? The first time to do this problem, really no train of thought. But at the beginning of Rujia's training guide, two ants meet and then move on, looking from a distance as if they were wearing each other. If you think like that, it's easy. Just determine how many of the ants on the left side of the cold to climb to the right, in judging how many of the right to the left to climb, add up is the answer. Don't forget that you are also a cold ant. The code is as follows:

#include <cstdio>int main () {    int n,x,f[110]={0},t,sum=1;    scanf ("%d", &n);    for (int i=0;i<n;i++) {        scanf ("%d", &x);        if (i==0) x>0?t=x:t=-x;        x>0?f[x]=1:f[-x]=-1;    }    for (int i=0;i<t;i++)        if (f[i]==1)        sum++;    for (int i=t+1;i<110;i++)        if (f[i]==-1)        sum++;    printf ("%d\n", sum);    return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Sdnu 1206. Ants Cold "code so simple, thinking practice" "July 29"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.