c - Please explain the code? -


int main() {   int n, k, i, j, k, x, final, cur, a[22];    for(i=!!scanf("%d %d",&n,&k), printf("%d\n",(final=n*n)-n);i<=n;a[i++]=i);   for(i=(cur=n)-1; i>=1; i--)     for(j=1; j<=i; printf("%d %d min\n%d %d max\n",a[j],a[j+1],a[j],a[j+1]),a[j]=++cur, a[j+1]=++cur, j++);      for(printf("%d",final-1+(cur=final)*0+(x=2)*0); cur>n; printf(" %d",cur), cur-=x, x+=2);   return 0; } 

please explain use of 2 exclamation marks in first "for" statement.

i explain first loops, last 3 loops easy understand. step step explanation.

step 1:

for(i=!!scanf("%d %d",&n,&k), printf("%d\n",(final=n*n)-n);i<=n;a[i++]=i); 

here, scanf("%d %d",&n,&k) returns 2 (number of integer read successfully.)

step 2: single negation, !2 = 0, negate 0, !0 = 1. so, i = !!2 = 1

step 3: suppose input 3 5 [n=3, k=5]. output of printf("%d\n",(final=n*n)-n) final = (3*3)-3 = 6

step 4: checking condition: i<=n means, 1<=n? if yes loop continues until condition becomes false. in process a[i++] = i, first index of set increases 1, value of a[i] set i. following array:

a[1] = 2; a[2] = 3; a[3] = 4;  

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -