Saturday, October 10, 2015

Dr Wu 2 uva 2787

Loop style :
Style 1 ->
                       loop(x = 1 to N)
                          {
                               loop(y=1 to N)
                                    {
                                    }
                          }

(x,y) = 1 1 , 1 2 , 1 3, ....,2 2,......,3 3,.....N N
Style 2 ->
                       loop(x = 1 to N)
                          {
                               loop(y=x+1 to N)
                                    {
                                    }
                          }

(x,y) = 1 2 , 1 3 ,...,1 N,2 3 , 2 4 ,........ ,N-1 N  [x!=y]
If on a same list then Style 1 will include such (x,y) pairs that x==y meaning same index twice in the pair.
The second style only considers NC2 pairs meaning only 2-subsets and is useful in some problems.

Code :-


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using namespace std;
int arr[1000];
int main()
{

while(true)
{
    int idx = 1;

int temp = 5;
while(true)
{
    cin>>temp;
    if(temp==0)
        break;
    if(temp==-1)
        return 0;
    arr[idx++] = temp;

}
idx--;
int ans = 0;
for(int x  =1;x<=idx;x++)
{
    for(int y=x+1;y<=idx;y++)
    {
        if( arr[x] == 2*arr[y] || arr[y]==2*arr[x] )
            ans++;
    }
}

   cout<<ans<<endl;
}


    return 0;
}

No comments:

Post a Comment