boolean combination

자료구조 2015. 5. 1. 17:54

n 길이의

boolean combination 출력



#include <stdio.h>


void printboolean(int a[],int index, int n)

{

int i;


if (index == n)

{

for (i=0;i<n;i++){

if (a[i] == 1)

printf ("true ");

else

printf("false ");

}

printf ("\n");

return; 

}


a[index]=1;

printboolean(a,index+1,n);

a[index]=0;

printboolean(a,index+1,n);

}




int main(void)

{

int n;

int arr[1000]={0,};


scanf("%d",&n);


printboolean(arr,0,n);


}

'자료구조' 카테고리의 다른 글

simple adjacency list  (0) 2015.05.22
hash simple linear  (0) 2015.05.22
binary search tree ( insert, kth smallest, inorder, delete by key)  (0) 2015.05.15
link list(single)  (0) 2015.05.01
Posted by rjh
,

longest common subsequence

알고2 2014. 12. 3. 11:22

<input>

m n

a

b


54 98 

bnbaqclwzrfkbskuwydbufxlagfgzmfwysjigulxraxrbxwnazupop 

bsxicfbinhukhpunbcqnabqwbrnndwdgiwiskjbumjbwllcdddlleszybrrcntaecwtgcedpnapadfasxfnayogugndwyksije



<output>

22

bnbaqwrkbuwydfxaggwys



#include <stdio.h>

#include <stdlib.h>

#include <math.h>


#define left 1

#define up 2

#define dagak 3


char a[1000];

char b[1000];


int c[100][100];

int dir[100][100];

int n,m;

void printlcs(int dir[100][100], char* a, int i,int j)

{

if (i==0 || j==0)

return;

if (dir[i][j] == dagak)

{

printlcs(dir,a,i-1,j-1);

printf ("%c",a[i]);

}

else if (dir[i][j] == up)

{

printlcs(dir,a,i-1,j);

}

else if (dir[i][j] == left)

{

printlcs(dir,a,i,j-1);

}

}

int main(void)

{

int i,j;

int logn;

// n

// nxn matrix

FILE* f = fopen ("input2.txt","r");

fscanf (f,"%d %d\n",&m,&n);


fgets(a+1,sizeof(a),f);

fgets(b+1,sizeof(b),f);


for (i=0;i<m;i++){

c[i][0] = 0;}

for (i=0;i<n;i++){

c[0][i] = 0;}


for (i=1;i<=m;i++)

{

for (j=1;j<=n;j++)

{

if (a[i] == b[j])

{

c[i][j] = c[i-1][j-1] + 1;

dir[i][j] = dagak;

}

else if (c[i-1][j] >= c[i][j-1])

{

c[i][j] = c[i-1][j];

dir[i][j] = up;

}

else

{

c[i][j] = c[i][j-1];

dir[i][j] = left;

}

}

}

printf ("%d\n",c[m][n]);

printlcs(dir,a,m,n);

printf ("\n");


}

'알고2' 카테고리의 다른 글

Parallel Sorting  (0) 2014.12.03
bin packing2  (0) 2014.11.26
bin packing (first fit) winner tree  (0) 2014.11.19
가장 높은탑 쌓기  (0) 2014.11.12
topological sort + bellman  (0) 2014.11.12
Posted by rjh
,

Parallel Sorting

알고2 2014. 12. 3. 10:08

<input>

n

nxn matrix


16

387 120 190 124 376 339 452 425 126 11 289 327 353 451 295 119 

204 487 377 332 45 432 225 153 420 85 318 276 143 315 323 327 

394 113 373 63 221 425 93 425 329 389 457 141 204 359 7 459 

90 461 170 66 388 154 381 445 134 134 333 156 174 63 198 239 

288 473 284 22 455 295 156 375 430 192 197 0 146 42 478 382 

48 130 43 467 29 316 421 185 405 130 98 435 467 56 365 313 

268 175 271 449 413 215 1 371 143 252 185 448 412 465 88 315 

481 258 282 49 184 449 225 109 298 237 80 283 187 361 378 168 

240 371 59 135 389 381 124 190 91 175 381 481 399 109 456 278 

104 380 52 385 324 207 407 149 491 100 434 215 261 79 21 424 

327 178 162 425 32 305 458 13 4 336 233 134 467 387 35 390 

340 303 416 183 84 263 216 436 471 228 197 393 276 230 15 500 

450 291 173 5 264 321 69 124 131 385 367 97 324 326 216 363 

17 405 0 407 359 14 336 91 136 9 288 131 379 352 217 4 

250 478 473 7 249 394 258 190 218 443 99 2 349 477 213 307 

181 239 438 164 193 383 181 143 492 498 217 80 268 207 308 350 




<output>

sorting된 순서




#include <stdio.h>

#include <stdlib.h>

#include <math.h>


int arr[100][100];

int n;


void sort(int row,int type)

{

int i,j,temp;

if (type == 0)

{

for (i=0; i<n;i++)

{

for (j = i%2; j<n; j+=2)

{

if (j+1 < n && arr[row][j] > arr[row][j+1])

{

temp = arr[row][j];

arr[row][j] = arr[row][j+1];

arr[row][j+1] = temp;

}

}

}

}

else

{

for (i=0; i<n;i++)

{

for (j = i%2; j<n; j+=2)

{

if (j+1 < n && arr[row][j] < arr[row][j+1])

{

temp = arr[row][j];

arr[row][j] = arr[row][j+1];

arr[row][j+1] = temp;

}

}

}

}

}

void sortcol(int col)

{

int i,j,temp;

for (i=0; i<n;i++)

{

for (j = i%2; j<n; j+=2)

{

if (j+1 < n && arr[j][col] > arr[j+1][col])

{

temp = arr[j][col];

arr[j][col] = arr[j+1][col];

arr[j+1][col] = temp;

}

}

}

}

int main(void)

{

int i,j;

int logn;

// n

// nxn matrix

FILE* f = fopen ("input3.txt","r");

fscanf (f,"%d",&n);


for (i=0;i<n;i++){

for (j=0;j<n;j++)

fscanf (f,"%d",&arr[i][j]);}



logn = log( (double)n ) / log( (double)2 );


for (i=0;i<logn;i++)

{

for (j=0;j<n;j++)

{

sort(j,j%2);

}

for (j=0;j<n;j++)

{

sortcol(j);

}

}


for (j=0;j<n;j++)

{

sort(j,0);

}


for (i=0;i<n;i++)

{

for (j=0;j<n;j++)

{

printf ("%3d ",arr[i][j]);

}

printf ("\n");

}

}

'알고2' 카테고리의 다른 글

longest common subsequence  (0) 2014.12.03
bin packing2  (0) 2014.11.26
bin packing (first fit) winner tree  (0) 2014.11.19
가장 높은탑 쌓기  (0) 2014.11.12
topological sort + bellman  (0) 2014.11.12
Posted by rjh
,