maverick
BAN USERbool validChar(char a,char b)
{
if(10*(a-'0')+(b-'0')<26)
return true;
return false;
}
void print(int i,string s,string p)
{
if(i==s.length())
{
cout<<p<<endl;
return;
}
if(s[i]=='0')
return;
print(i+1,s,p+(char)('a'+(s[i]-'0'-1)));
if(i+1<s.length() && validChar(s[i],s[i+1]))
print(i+2,s,p+(char)('a'-1+(10*(s[i]-'0')+(s[i+1]-'0'))));
}
def reverseReplace(str1, str2, str3):
return helper(str1.replace(str2,str3))
def helper(text):
t=""
for c in text:
if c==' ':
text=text.replace(t,"".join(reversed(t)))
t=""
else:
t+=c
text=text.replace(t,"".join(reversed(t)))
text="".join(reversed(text))
return text
print reverseReplace("I like cats", "cats", "dogs")
Consider each hotel as struct of 5 attributres
struct hotel
{
int star;
int regular_weekdays;
int regular_weekends;
int rewardee_weekdays;
int rewardee_weekends;
};
Now check each hotel and maintain the lowest cost hotel. In case of tie with the lowest maitained hotel check if the current hotel's star is more thhan the maintained one, if it is then the replace the maintained hotel with current hotel.
node *swap(node *pt,node *a,node *b)
{
node *root=pt;
node *x_l,*x_r;
node *y_l,*y_r;
node *prev=NULL;
while(pt!=b)
{
if(pt==a)
{
x_l=prev;
x_r=pt->next;
}
prev=pt;
pt=pt->next;
}
y_l=prev;
y_r=pt->next;
x_l->next=b;
b->next=x_r;
y_l->next=a;
a->next=y_r;
return root;
};
head is passed in first argument.
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
void print_comb(string temp,int i,int j,int k)
{
if(j-i+1<k || k==0)
{
cout << temp<<endl;
return;
}
char ch[50];
string rem=temp;
for(int a=i;a<=j-k;a++)
{
sprintf(ch,"%d",a);
temp=rem;
temp+=ch;
print_comb(temp,a+1,j,k-1);
}
}
int main()
{
int n,k;
cout << "Enter n : ";
cin >> n;
cout << "Enter k : ";
cin >> k;
print_comb("",1,n,k);
return 0;
}
string t="";
void indent(int indent_counter)
{
while(indent_counter--)
t+="\t";
}
int main()
{
string s;
cin >> s;
int len=s.length();
int indent_counter=0;
s+='p';
for(int i=0;i<len;i++)
{
if(s[i]=='{')
{
indent(indent_counter);
if(s[i+1]!='{' || s[i+1]!='}')
{
t+="\n";
indent(indent_counter);
t+="{\n";
}
else
{
t+="\n";
indent(indent_counter);
t+="{";
}
indent(++indent_counter);
}
else if(s[i]=='}')
{
if(s[i+1]!='}' || s[i+1]!='{')
{
t+="\n";
indent(--indent_counter);
t+="}\n";
}
else
{
t+="\n";
indent(--indent_counter);
t+="}";
}
indent(indent_counter);
}
else if(s[i]==',')
{
t+="\n";
indent(indent_counter);
}
else
{
t+=s[i];
//indent(indent_counter);
}
//cout <<i<<"\t"<< s[i]<<endl;
}
cout << t<<endl;
return 0;
}
int count;
void check_factorial_prime(int var)
{
int a=sqrt(var-1);
int b=sqrt(var+1);
int i;
for(i=2;i<=a;i++)
{
if((var-1)%i==0)
break;
}
if(i==a+1)
{
cout << var-1<<endl;
count++;
}
for(i=2;i<=b;i++)
{
if((var+1)%i==0)
break;
}
if(i==b+1)
{
cout << var+1<<endl;
count++;
}
}
int main()
{
int n;
cout << "Enter n : ";
cin >> n;
int i=3;
count=2;
int mult=2;
if(n==1)
cout << "2\n";
else
{
cout << "2\n";
cout << "3\n";
while(count!=n)
{
mult=mult*(i++);
check_factorial_prime(mult);
}
}
return 0;
}
struct node
{
node *previous;
node *next;
node *child;
int value;
};
node *pt;
node *last=tail;
for(pt=head;pt!=last;pt=pt->next)
{
if(pt->child!=NULL)
{
traverse(pt->child,tail);
child=NULL;
}
}
if(last->child!=NULL)
{
traverse(last->child,tail);
child=NULL;
}
void traverse(node *pt,node *tail)
{
node *pt1;
for(pt1=pt;pt1!=NULL;pt=pt->next)
{
tail->next=pt1;
pt1->previous=tail;
tail=pt1;
if(pt1->child!=NULL)
{
traverse(pt1->child,tail);
pt1->child=NULL;
}
}
}
Let k be the given number then code would be similar to the following
int starting_index(int *a,int start,int end)
{
int mid=(start+end)/2;
if(end!=mid)
{
if(a[mid-1]<k && a[mid]==k)
return mid;
else if(a[mid]<k)
starting_index(a,mid+1,end);
else
starting_index(a,start,mid-1);
}
else
{
if(a[mid]==k)
return mid;
else
return -1;
}
}
-1 is returned when there is no occurence of given number.
Make an array of n elements. Lets say it is 'a'.
Initialize the array with zero.
now traverse the given array. Lets say it is 'b'.
Now do a[b[i]]++;
After complete traversing check each element of array 'a'.
The element which remains zer is the missing element and the one which has value two will be the duplicated one.
- maverick September 13, 2013