
In today's Koffee with Karan, Rishi Kapoor said
"Don't let failure get to your heart and Don't let success get to your head".
The macro assert() generates no code, and hence does nothing
at all. Otherwise, the macro assert() prints an error message to stan-
dard output and terminates the program by calling abort() if expression
is false (i.e., condition equal to zero).
The purpose of this macro is to help the programmer find bugs in his
program. The message "assertion failed in file
/*
* Don Knuth's Permutation Algorithm
*/
public class permu {
int[] a;
int j, k, l, n;
permu(int n) {
a = new int[n+1];
this.n = n;
for (int i = 0; i <= n; i++)
a[i] = i;
}
void visit() {
for (int i = 1; i <= n; i++)
System.out.print(a[i] + " ");
System.out.println();
}
void swap(int j, int k) {
if (j == k) return;
int tmp = a[j];
a[j] = a[k];
a[k] = tmp;
}
//
// print all permutations of the numbers 1 to n
//
void permute() {
do {
visit();
j = n - 1;
while (j != 0 && a[j] >= a[j+1]) {
j--;
}
if (j == 0) break;
l = n;
while (a[j] >= a[l]) {
l--;
}
swap(j, l);
k = j+1;
l = n;
while (k < l) {
swap(k, l);
k++;
l--;
}
} while (j >= 0);
}
void rpermute(int k, int m) {
int i;
if (m == k) {
visit();
return;
}
for (i = k; i <= m; i++) {
swap(i, k);
rpermute(k+1, m);
swap(i, k);
}
}
public static void main(String [] args) {
int n;
if (args.length != 1) {
System.out.println("usage is permu");
return;
} else {
n = Integer.parseInt(args[0]);
}
permu p = new permu(n);
p.permute();
permu p2 = new permu(n);
p2.rpermute(1, n);
}
}
#include < stdlib.h >
int
max(int a, int b)
{
printf("calling function max\n");
return ((a > b) ? a : b);
}
#define max(a, b) ((a > b) ? (a) : (b))
int
main(void)
{
int i = 10, j = 5;
printf("1) %d\n", max(i, j));
i = 5;
j = 10;
printf("2) %d\n", (*max)(i, j));
return 0;
}
a = a^b;
b = a^b;
a = a^b;
inline
swap(long *a, long *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
You can see the following message behind Maggie 2 minutes noodles pack:
Step 1: boil one cup of water
Step 2: as soon as ganguly goes for batting, put the noodles in the
boiled water and add the tastemaker.
Step 3: stir till ganguly is on the field.
Step 4: As soon as ganguly is back in pavilion, your noodles are ready to eat.
I've been a big fan of Skiena's Algorithm Design Manual , I recently found my first edition of the book (although I own the third ed...