1. 程式人生 > >CodeForces-467A George and Accommodation(語法練習題)

CodeForces-467A George and Accommodation(語法練習題)

George and Accommodation

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
George has recently entered the BSUCP (Berland State University for Cool Programmers). George has a friend Alex who has also entered the university. Now they are moving into a dormitory.

George and Alex want to live in the same room. The dormitory has n rooms in total. At the moment the i-th room has pi people living in it and the room can accommodate qi people in total (pi ≤ qi). Your task is to count how many rooms has free place for both George and Alex.

Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of rooms.

The i-th of the next n lines contains two integers pi and qi (0 ≤ pi ≤ qi ≤ 100) — the number of people who already live in the i-th room and the room’s capacity.

Output
Print a single integer — the number of rooms where George and Alex can move in.

Examples
inputCopy
3
1 1
2 2
3 3
outputCopy
0
inputCopy
3
1 10
0 10
10 10
outputCopy
2

問題簡述:

  George和他朋友要住旅館,而且要同一個房間。第一行給出房間總數,後面給出每個房間的已入住人數和可容納的總人數。請問有幾間房間可以選擇?

問題分析:

  對於每個房間,判斷其空位數是否大於等於2即可。將符合條件的房間數加起來。

程式說明:

  得到房間數n後,申請空間建立一個長度為2n的陣列儲存房間資訊。用一個for迴圈判斷遍歷房間資訊,統計符合條件的房間數。

程式實現:

#include<iostream>
using namespace std;

int main()
{
    int n,sum=0;
    cin>>n;
    int *a=new int[2*n];
    for(int i=0;i<2*n;i+=2)
    {
        cin>>a[i]>>a[i+1];
        if(a[i]<a[i+1]-1) sum++;
    }
    cout<<sum;
}