alexandru.doro
BAN USERstudent
i think its a rather good answer... why do you call it stupid ? I think we could work on the idea and optimize it a bit more.. like dividing it into something better.. like... 11*9 < 10*10 .. but i didnt really do the math... good answer anyway ! Please explain when you call an answer stupid :) thanks
- alexandru.doro June 15, 2012#include <iostream>
#include <fstream>
using namespace std;
class Point
{
public:
int x,y;
Point( int x, int y)
{
this->x=x;
this->y=y;
}
Point(){}
void setValues( int x, int y)
{
this->x=x;
this->y=y;
}
static int getDistance ( Point p1, Point p2 )
{
int xd=abs(p1.x-p2.x);
int yd=abs(p1.y-p2.y);
return max(xd,yd);
}
};
int main()
{
int n;
Point puncte[10];
ifstream f("date.in");
f>>n;
for ( int i=0; i<n; i++ )
{
int x,y;
f>>x>>y;
puncte[i].setValues(x,y);
}
f.close();
// mediile
int sx=0, sy=0, mx, my;
for ( int i=0; i<n; i++ )
{
sx+=puncte[i].x;
sy+=puncte[i].y;
}
mx=sx/n;
my=sy/n;
if (sx%n!=0) mx++;
if (sy%n!=0) my++;
Point temp(mx,my);
int min=32000, poz_min=0;
for ( int i=0; i<n; i++ )
{
int minTemp=Point::getDistance(temp,puncte[i]);
if ( minTemp < min ) {
poz_min=i;
min=minTemp;
}
}
cout<<puncte[poz_min].x<<" , "<<puncte[poz_min].y<<endl;
return 1;
}
#include <iostream>
using namespace std;
int number=3;
int st[100]; // stack
int getSum(int n)
{
int s=0;
for (int i=0; i<n; i++ )
s+=st[i];
return s;
}
void backTrack(int n)
{
int sum=getSum(n);
if ( sum==number )
{
for ( int i=0; i<n; i++ )
cout<<st[i]<<" ";
cout<<endl;
}
else
{
for ( int i=1; i<=number-sum; i++ )
{
if ( i>= st[n-1] ) // to avoid getting 3=2+1 and also 1+2
{
st[n]=i;
backTrack(n+1);
}
}
}
}
int main()
{
backTrack(0); // 0 = number of elements in stack in the beginning
return 1;
}
well it's your typical bubble sort, just that knowing you only have 2 values.. you can do a lot less tests:). You dont have to swap 0-s and you dont have to take each 1 to the last position. ez!
- alexandru.doro May 22, 2012well yeah! i told you its a modified bubble sort. It has to be one since you can only swap with your neighbor.
- alexandru.doro May 22, 2012it's n/2 in worst case when its palindrom... so it is better than O(n)
- alexandru.doro May 22, 2012the most efficient way i can think of... thinking i NEED to swap is a modified bubble sort:
int swapsDone=0;
for ( int i=0; i<n-swapsDone; i ++ )
if ( ch[i]=='1' )
for ( int j=i; j< n-swapsDone-1; j++ )
{
swap ( ch[j+1], ch[j] );
swapsDone++;
}
// i don't think i need to explain the code
- alexandru.doro May 22, 2012max_no_path = pow ( 2, ( abs(x1-x2)* abs(y1-y2) )
explanation :
---->you pretty much are allowed to travel within a rectangle, where you can either go up or right ... you have 2 choices... thus pow(2, x)
---->the (x1-x2) * (y1-y2) are the decision nodes that create a new path
can you give more details on what a infix sequence and postfix sequence is ? an example maybe?
- alexandru.doro May 22, 2012when you divide 300 to x.. it will convert x to int.. and do the computation and will return a int.. so you'll have to manually cast it to char....
- alexandru.doro April 18, 2012when you divide 300 to x.. it will convert x to int.. and do the computation and will return a int.. so you'll have to manually cast it to char....
- alexandru.doro April 18, 2012
we just save the local min and max
- alexandru.doro June 22, 2012