1. 程式人生 > >演算法筆記3.1 codeup課後習題

演算法筆記3.1 codeup課後習題

問題 A: 剩下的樹

時間限制: 1 Sec  記憶體限制: 32 MB

題目描述

有一個長度為整數L(1<=L<=10000)的馬路,可以想象成數軸上長度為L的一個線段,起點是座標原點,在每個整數座標點有一棵樹,即在0,1,2,...,L共L+1個位置上有L+1棵樹。     現在要移走一些樹,移走的樹的區間用一對數字表示,如 100 200表示移走從100到200之間(包括端點)所有的樹。     可能有M(1<=M<=100)個區間,區間之間可能有重疊。現在要求移走所有區間的樹之後剩下的樹的個數。

輸入

兩個整數L(1<=L<=10000)和M(1<=M<=100)。     接下來有M組整數,每組有一對數字。

輸出

 可能有多組輸入資料,對於每組輸入資料,輸出一個數,表示移走所有區間的樹之後剩下的樹的個數。

樣例輸入

4 2
1 2
0 2
11 2
1 5
4 7
0 0

樣例輸出

2
5
另一臺電腦上待插入

問題 B: A+B

時間限制: 1 Sec  記憶體限制: 32 MB  

題目描述

給定兩個整數A和B,其表示形式是:從個位開始,每三位數用逗號","隔開。 現在請計算A+B的結果,並以正常形式輸出。

輸入

輸入包含多組資料資料,每組資料佔一行,由兩個整數A和B組成(-10^9 < A,B < 10^9)。

輸出

請計算A+B的結果,並以正常形式輸出,每組資料佔一行。

樣例輸入

-234,567,890 123,456,789
1,234 2,345,678

樣例輸出

-111111101
2346912
另一臺電腦上待插入

問題 C: 特殊乘法

時間限制: 1 Sec  記憶體限制: 32 MB  

題目描述

寫個演算法,對2個小於1000000000的輸入,求結果。特殊乘法舉例:123 * 45 = 1*4 +1*5 +2*4 +2*5 +3*4+3*5

輸入

 兩個小於1000000000的數

輸出

 輸入可能有多組資料,對於每一組資料,輸出Input中的兩個數按照題目要求的方法進行運算後得到的結果。

樣例輸入

24 65
42 66666
3 67

樣例輸出

66
180
39
另一臺電腦上待插入

問題 D: 比較奇偶數個數

時間限制: 1 Sec  記憶體限制: 32 MB  

題目描述

第一行輸入一個數,為n,第二行輸入n個數,這n個數中,如果偶數比奇數多,輸出NO,否則輸出YES。

輸入

輸入有多組資料。 每組輸入n,然後輸入n個整數(1<=n<=1000)。

輸出

如果偶數比奇數多,輸出NO,否則輸出YES。

樣例輸入

1
67 
7
0 69 24 78 58 62 64 

樣例輸出

YES
NO
#include <iostream>
#include<cstring>
using namespace std;
int main()
{
    int n;
    int a[1010];
    int even,odd;
    while(cin>>n){
        memset(a,0,sizeof(a));
        even=0;
        odd=0;
        for(int i=0;i<n;i++){
            cin>>a[i];
            if(a[i]%2==0){//even
                even++;
            }else{
                odd++;
            }
        }
        if(even>odd){
            cout<<"NO"<<endl;
        }else{
            cout<<"YES"<<endl;
        }

    }
    return 0;
}

問題 E: Shortest Distance (20)

時間限制: 1 Sec  記憶體限制: 32 MB  

題目描述

The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits.

輸入

Each input file contains one test case. For each case, the first line contains an integer N (in [3, 105]), followed by N integer distances D1 D2 ... DN, where Di is the distance between the i-th and the (i+1)-st exits, and DN is between the N-th and the 1st exits. All the numbers in a line are separated by a space. The second line gives a positive integer M (<=104), with M lines follow, each contains a pair of exit numbers, provided that the exits are numbered from 1 to N. It is guaranteed that the total round trip distance is no more than 107.

輸出

For each test case, print your results in M lines, each contains the shortest distance between the corresponding given pair of exits.

樣例輸入

5 1 2 4 14 9
3
1 3
2 5
4 1

樣例輸出

3
10
7
另一臺電腦上待插入