CheckThisResume.com
BAN USER
#define SIZE 7
int tab[SIZE]={ 1,0,2,0,0,3,4};
void f(void)
{
int swapped, i, j, temp;
swapped=1;
i=0;
j=0;
while(swapped)
{
swapped=0;
while((i<SIZE)&&(j<SIZE))
{
while((tab[i]==0)&&(i<SIZE))
i++;
while((tab[j]!=0)&&(j<SIZE))
j++;
if((i<SIZE)&&(j<SIZE))
{
temp = tab[i];
tab[i] = tab[j];
tab[j] = temp;
swapped=1;
}
}
}
}
void main()
{
int i;
f();
for(i=0; i<SIZE ; i++)
{
printf("\n %d: %d", i, tab[i]);
}
exit(000);
}
I replied above for the "inefficient". Let me reply for the "incorrect". I made a small change with "while" instead of "if" and tested it OK.
#include <stdio.h>
int keyval[] = { 1000, 900, 500, 400, 100,90, 50, 40, 10, 9, 5, 4, 1 };
char keystr[13][3] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
char str[] = "XXIV";
void decode()
{
char *ptr=str;
int i, l;
int val=0;
for(i=0; i<13 ; i++)
{
l=strlen(keystr[i]);
while(!strncmp(ptr,keystr[i],l))
{
val+=keyval[i];
ptr+=l;
}
}
printf("\n val: %d", val);
}
void main()
{
int i;
decode();
exit(000);
}
This is one pass scan Sir!
if the strlen bothers you, replace 'l' with ls[i] with ls defined as follow: int ls[]={ 1, 2, 1, 2, etc...
int keyval[] = { 1000, 900, 500, 400, 100,90, 50, 40, 10, 9, 5, 4, 1 };
char keystr[13][] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
int i;
char str[] = "XXIV";
char *ptr=str;
int val=0;
for(i=0; i<13 ; i++)
{
l=strlen(keystr[i]);
if(!strncmp(ptr,keystr[i],l))
{
val+=keyval[i];
ptr+=l;
}
}
printf("\n val: %d", val);
#define SIZE 3
int tab[SIZE]={4, 10, 15};
int score=80;
int f(s)
{
printf(" %d", s);
if(s<0)
{
printf(" nope\n");
return(0);
}
else
if(!s)
{
printf(" found\n");
return(1);
}
else
{
return(f(s-tab[0]) || f(s-tab[1]) || f(s-tab[2]));
}
}
The lines ending with found is the results
- CheckThisResume.com March 08, 2012#define SIZE 3
int tab[SIZE];
int pages[]={1,3};
for(i=0; i<SIZE ; i++)
{
tab[i]=0;
}
for(i=0; i<SIZE-1 ; i++)
{
tab[pages[i]]=1;
}
for(i=0; i<SIZE ; i++)
{
if(!tab[i]) printf("\n i: %d is missing", i);
}
Use bubble sort because it is simple and it use a constant space + 1 cell for the swap.
Order from high to low.
#define SIZE 3
int tab[SIZE];
int swapped=1;
while(swapped)
{
swapped=0;
for(i=0; i<SIZE-1 ; i++)
{
if(tab[i]<tab[i+1])
{
temp = tab[i];
tab[i] = tab[i+1];
tab[i+1] = temp;
swapped=1;
}
}
}
for(i=0; i<SIZE ; i++)
{
printf("%d", tab[i]);
}
typedef struct TreeNode
{
int element;
struct TreeNode *left, *right;
} TreeNode;
TreeNode *displayTree(TreeNode *node)
{
//display the full tree
if(node==NULL)
{
return;
}
displayTree(node->left);
printf("| %d ", node->element);
displayTree(node->right);
}
oops typo >=
for(i=strlen(str)-1; i>=0; i--)
char str[] = "Hello World";
char str2[] = "";
int i;
for(i=strlen(str)-1; i<=0; i--)
{
if(str[i]==' ')
{
strcat(str2,str+i+1);
strcat(str2," ");
str[i]=0; // cut
}
}
// first that becomes last
strcat(str2,str);
printf("\n str: %s", str2);
char str1[] = "abcrfghwetf";
char str2[] = "abrfghwwetxyab";
int max, i, j, l1, l2, cnt, sovi, sovj;
max=0;
l1=strlen(str1);
l2=strlen(str2);
for(i=0; i<l1 ; i++)
{
cnt=0;
for(j=i; j<l2 ; j++)
{
if((str1[i]==str2[j]))
{
cnt++;
i++;
j++
}
else
break;
}
if(cnt>max)
{
max=cnt;
sovi=i;
sovj=j;
}
}
printf("\n str: %s", str1+i-cnt);
This returns the 4th largest number. replace 4 by k and 3 with k-1 to answer the question.
- CheckThisResume.com March 05, 2012char str[] = "Hello WorldHe";
int i, j, l, cnt;
l=strlen(str);
for(i=0; i<l ; i++)
{
cnt=0;
for(j=i+1; j< ; j++)
{
if(str[j]==str[i])
cnt++;
}
if(!cnt)
{
printf("\n i: %d", i);
break;
}
}
Yes, but this is not in place.
- CheckThisResume.com March 10, 2012