Beispiel #1
0
Datei: p3.c Projekt: ws23/102ADHW
int counting(int e){
	if(e==1)
		return 0;
	if(e%2==0)
		return counting(e/2)+1;
	return counting(e/2)+2;
}
Beispiel #2
0
void counting()
{
	if(r==1&&c==(n+1))
	{	// all combinations exhausted
		printf("THE NUMBER OF SOLUTIONS ARE:   %ld\n",count);
		exit(1);
	}
	if(r==n+1)
	{	//a possible solution reached
		r=n;
		c=pos[r];
		mat[r][c]=' ';
		c=c+1;
		counting();

	}
	if((c<=n)&&(check_fnc(r,c)) )
	{
		mat[r][c]='q'; //to check the postion in current row
		if(check_count())
			{
				count++;
				if(count==1||count==2)
					disp();
			}
		pos[r]=c;
		r=r+1;
		c=1;
		counting();

	}
	else
	{
	    if(c<n)
	    {
		c++;
		counting();
	    }
	    else
	    {  // to move from curent row to the previous one and rearranging the previous one 
		c=pos[r];
		mat[r][c]=' ';
		r=r-1;
		c=pos[r];
		mat[r][c]=' ';
		c=c+1;
		counting();
	    }
	}

}
void inserting(int data,int loc)
{
    int n,x;
    struct node *p,*t;
    p=(struct node *)malloc(sizeof(struct node));
    p->info=data;
    n=counting();
    if(loc<0||loc>n)
    {
        printf("Can not insert at this position");
    }
    else if(loc==1)
    {
        p->next=NULL;
        start=p;
    }
    else
    {
        x=0;
        t=start;
        while(x!=loc)
        {
            t=t->next;
            x++;
        }
        p->next=t->next;
        t->next=p;

    }
}
Beispiel #4
0
void makeOperation(plst &head1,plst &head2)
{
	int a = front(head2);
	popNext(head2);
	int b = front(head2);
	popNext(head2);
	push(head2, counting(front(head1), a, b));
	popPrev(head1);
}
void SPGEnumerator::enumeration() {
    std::map<int, std::set<int> > curAdjSet;
    constructAdjSet(curAdjSet);
    std::set<int> edge;
    int a, b;

    for (std::map<int, std::set<int> >::iterator it = curAdjSet.begin(); it != curAdjSet.end(); it++) {
        a = it->first;
        for (std::set<int>::iterator sit = (it->second).begin();
                sit != (it->second).end(); sit++) {
            b = *sit;
            edge.clear();
            edge.insert(a);
            edge.insert(b);

            g.addNewEdge(edge, e2n[edge]);
            if (graphVisited.find(g.hashEdgeSet()) == graphVisited.end()) {

                graphVisited.insert(g.hashEdgeSet());
#ifdef DEBUG
                std::cout << "SUB-GRAPH:"<< std::endl;
                g.printGraph();
#endif
                enumeration();
                if (!isSPG) {
                    spgi.initialization(g.realEdgeSet);
                    if (spgi.identify()) {
                        counting();
                        enumeration();
                    }
                } else {
                    counting();
                    enumeration();
                }
            }
            g.removeEdge(edge, e2n[edge]);
        }
    }

}
Beispiel #6
0
Datei: p3.c Projekt: ws23/102ADHW
int main(){
	int t, n, i, j, bin, tmp, multi[32], count;
	int f[32][4], ans[4];

	f[0][0] = 0;
	for(i=1;i<=3;i++)
		f[0][i] = 1;

	for(i=1;i<=30;i++)
		mul(f[i], f[i-1], f[i-1]);

	scanf("%d", &t);
	while(t--){
		scanf("%d", &n);
		count = 0;
		for(i=0;i<31;i++)
			multi[i] = 0;
		ans[0] = ans[3] = 1;
		ans[1] = ans[2] = 0;
		for(i=0, bin=1;i<30;i++)
			bin *= 2;
		for(tmp=n-1,i=30;tmp>0;bin/=2, i--){
			if(tmp>=bin){
				tmp -= bin;
				multi[i] = 1;
			}
		}
		for(i=0;i<=30;i++)
			if(multi[i]==1)
				mul(ans, ans, f[i]);
		i = counting(n);
		i = counting(n-1)<i?counting(n-1):i;
		if(n<20)
			printf("%d %d\n", i, ans[3]);
		else
			printf("%d %04d\n", i, ans[3]);
	}
	return 0;
}
    inline static bool 
    center_generate(OutputIterator& sink, Context& ctx, 
        Delimiter const& d, Parameter const& param, Embedded const& e, 
        unsigned int const width, Padding const& p) 
    {
        // make sure all generator parameters are valid
        BOOST_MPL_ASSERT_MSG(
            (spirit::traits::is_component<karma::domain, Embedded>::value), 
            embedded_is_not_convertible_to_a_generator, (Context, Embedded));

        BOOST_MPL_ASSERT_MSG(
            (spirit::traits::is_component<karma::domain, Padding>::value), 
            padding_is_not_convertible_to_a_generator, (Context, Padding));

        typedef 
            typename result_of::as_component<karma::domain, Embedded>::type 
        embedded;
        typedef 
            typename result_of::as_component<karma::domain, Padding>::type 
        padding;

        // wrap the given output iterator to allow left padding
        detail::enable_buffering<OutputIterator> buffering(sink, width);

        // first generate the embedded output 
        embedded ec = spirit::as_component(karma::domain(), e);
        typedef typename embedded::director director;
        bool r = director::generate(ec, sink, ctx, d, param);

        buffering.disable();    // do not perform buffering any more
        
        // generate the left padding
        detail::enable_counting<OutputIterator> 
            counting(sink, (sink.buffer_size() + width) / 2);

        padding pc = spirit::as_component(karma::domain(), p);
        typedef typename padding::director padding_director;
        while (r && sink.count() < width) 
            r = padding_director::generate(pc, sink, ctx, unused, unused);

        if (r) {
            // copy the embedded output to the target output iterator
            sink.buffer_copy();
        
            // generate the right padding
            std::size_t const max_count = width + (width - sink.buffer_size()) / 2;
            while (r && sink.count() < max_count) 
                r = padding_director::generate(pc, sink, ctx, unused, unused);
        }
        return r;
    }
void SPGEnumerator::start() {
    std::map<int, std::set<int> > curAdjSet;
    for (std::set<int>::iterator it = edgeSet.begin(); it != edgeSet.end(); it++) {
        g.addNewEdge(n2e[*it], *it);
        graphVisited.insert(g.hashEdgeSet());
#ifdef DEBUG
        std::cout << "SUB-GRAPH:"<< std::endl;
        g.printGraph();
#endif
        counting();
        enumeration();
        g.removeEdge(n2e[*it], *it);
    }
}
/* Funcion que determina en que conjunto esta el problema del 8puzzle (el de los pares o impares) */
static int what_parity(int cells[][N]) {
	int i, j, cnt;	
	int aux[N*N];
	
	/* transformo el array bidimensional en un array unidimensional */
	for (cnt = i = 0; i < N; i++)
		for (j = 0; j < N; j++)
			aux[cnt++] = cells[i][j];
	
	/* voy mirando las casillas que despues de i son más pequeñas */
	cnt = 0;
	for (i = 0; i < N*N; i++)
		cnt += counting(aux, i);
		
	return (cnt % 2 == 0);
}
void main(void){
	//TSCR2_TCRE  = 1;
	TSCR1_TEN   = 1;
	// TFLG2_TOF   = 1;
	

 	DDRA        = 0x00;
	DDRB        = 0x00;
	DDRT        = 0xFF;
  
	while(1){
		// sw1 = PORTA_BIT0;
		while ( PORTA_BIT0 ) {
		  delay();
			// sw2 = PORTB_BIT4;
			counting( PORTB_BIT4 );
		}
	}
}
int PrintMat(const CvMat *mat, const char *strName)
{
    double   fValue;
    double total[CHECKSIZE][CHECKSIZE] = { 0 };
    double zero[CHECKSIZE][CHECKSIZE] = { 0 };
    int area_x = mat->cols / CHECKSIZE;
    int area_y = mat->rows / CHECKSIZE;
    //printf(" %s  \n =  \n", strName);
    for (int i = 0; i < mat->rows; i++)
    {
        for (int j = 0; j < mat->cols; j++)
        {
            fValue = cvGetReal2D(mat, i, j);
//            printf("%3d", cvRound(fValue));
        }
//        printf("\n");
    }
    for (int i = 0; i < CHECKSIZE; i++){
        for (int j = 0; j < CHECKSIZE; j++){
            for (int y = area_y*i; y < area_y*(i + 1); y++)
            {
                for (int x = area_x*j; x < area_x*(j + 1); x++)
                {
                    fValue = cvGetReal2D(mat, y, x);
                    if (cvRound(fValue) == 0)
                        zero[i][j] += 1;
                    total[i][j] += 1;
                }
            }
            total[i][j] = zero[i][j] / total[i][j];
        }
    }
    show_zeroArea(total);
    cout<<counting(total);
    
    return 0;

}
    inline static bool 
    left_align_generate(OutputIterator& sink, Context& ctx, 
        Delimiter const& d, Parameter const& param, Embedded const& e, 
        unsigned int const width, Padding const& p) 
    {
        // make sure all generator parameters are valid
        BOOST_MPL_ASSERT_MSG(
            (spirit::traits::is_component<karma::domain, Embedded>::value), 
            embedded_is_not_convertible_to_a_generator, (Context, Embedded));

        BOOST_MPL_ASSERT_MSG(
            (spirit::traits::is_component<karma::domain, Padding>::value), 
            padding_is_not_convertible_to_a_generator, (Context, Padding));
            
        typedef 
            typename result_of::as_component<karma::domain, Embedded>::type 
        embedded;
        typedef 
            typename result_of::as_component<karma::domain, Padding>::type 
        padding;

        // wrap the given output iterator to allow counting
        detail::enable_counting<OutputIterator> counting(sink);
        
        // first generate the underlying output 
        embedded ec = spirit::as_component(karma::domain(), e);
        typedef typename embedded::director director;
        bool r = director::generate(ec, sink, ctx, d, param);

        // pad the output until the max width is reached
        padding pc = spirit::as_component(karma::domain(), p);
        while(r && sink.count() < width) {
            typedef typename padding::director padding_director;
            r = padding_director::generate(pc, sink, ctx, unused, unused);
        }
        return r;
    }
Beispiel #13
0
int main()
{
	int i,j;
	printf("enter the value of n\n");
	scanf("%d",&n);
	mat=(char**)malloc(sizeof(char)*n*n);
	for(i=1;i<=n;i++)
	*(mat+i)=(char*)malloc(sizeof(char)*n);

	pos=(int*)malloc(sizeof(int)*n);
	
	for(i=1;i<=n;i++) //intialize
	{
		for(j=1;j<=n;j++)
			{
				mat[i][j]=' ';
			}
		pos[i]=0;
	}

	r=1;c=1;
	counting();//function to count the number of solutions
return 1;
}
int main(){
        scanf("%d",&n);
        char str[n][200];
        int i;
        scanf("%s",str[0]);
        counting(str[0]);
        for(i=1;i<n;i++){
                scanf("%s",str[i]);
                if(i==n-1)
                        check(str[i],1);
                else
                        check(str[i],0);
        }
        for(i=0;i<26;i++){
                if(alpha[i]!=0){
                        dis[0]=i+'a';
                        break;
                }
                //printf("%c -> %d\n",('a'+i),alpha[i]);
        }
        dis[l]='\0';
        printf("%s",dis);
        return 0;
}
Beispiel #15
0
void extrait_outgroup(string inFile,string outFile,list<string> &outgroups,int nb){
     int n;//the number of internal nodes
	 int m;//the number of branches
     int n1,m1;
    counting(inFile,n,m,nb);
    int *P = new int[m+1];//tableau de predecesseurs
    double *B = new double[m+1];//branch lengths
    string *Support = new string[m+1];
    string* Labels = new string[m+1];
	FILE * tree = fopen(inFile.c_str(),"rt");
	if (tree==NULL) cout<<"Can not open the tree file"<<endl;
	else{
         FILE * w = fopen(outFile.c_str(),"wt");
         bool rooted=false;
         for (int y=1;y<=nb;y++){
             if (rooted){
                n++;
                m++;
             }
             tree2dataS(tree,n,m,P,B,Support,Labels);
             int * Suc1= new int[n];
             int * Suc2= new int[n];
             computeSuc(P,Suc1,Suc2,m+1,n);
             list<int> P1;
             list<double> B1;
             list<string> Support1;
             list<string> Labels1;
             list<double> T1;
             int s;
             if (m==2*n){
                rooted=true;
                rooted2unrooted(n,m,P,Suc1,Suc2,B,Labels);
                m--;
                n--;
                computeSuc(P,Suc1,Suc2,m+1,n);
             }
             s= computeSuc_unrooted(P,Suc1,Suc2,m+1,n);
             list<int> out;
             list<int> in;
             for (int i=n;i<=m;i++){
                 bool flag = true;
                 for (list<string>::iterator iter=outgroups.begin();iter!=outgroups.end();iter++){
		             if (Labels[i].compare(*iter)==0){
                        out.push_back(i);                   
                        flag=false;
                        break;
                     }
                 }
                 if (flag) in.push_back(i);
             }
             if (out.size()==0){
                cout<<"Tree "<<y<<": the tree does not contain any outgroup"<<endl;  
                exit( EXIT_SUCCESS );
             }
             else{
                 int *P_new= new int[m+1];
                 int *Suc1_new= new int[n];
                 int *Suc2_new= new int[n];
                 double *B_new= new double[m+1];
                 string* Support_new = new string[m+1];
                 int t=out.front();
                 int t1=in.front();
                  if (out.size()==1){
                     reroot(n,m,t,P,s,Suc1,Suc2,B,Support,P_new,Suc1_new,Suc2_new,B_new,Support_new);
                     subTree(n,m,P[t],P_new,Suc1_new,Suc2_new,B_new,Support_new,Labels,P1,B1,Support1,Labels1);
                     int *Po=new int[m];
                     listToArray(P1,Po);
                     double *Bo=new double[m];
                     listToArray(B1,Bo);
                     string* Supporto=new string[m];
                     listToArray(Support1,Supporto);
                     string* Labelso = new string[m];
                     listToArray(Labels1,Labelso);
                     int *Suc1o=new int[n];
                     int *Suc2o=new int[n];
                     computeSuc(Po,Suc1o,Suc2o,m,n);
                     newicktree(n,Po,Suc1o,Suc2o,Labelso,Bo,Supporto,w);
                      delete[]  Po;
                      delete[]  Bo;
                      delete[] Labelso;
                      delete[] Suc1o;
                      delete[] Suc2o;
                      delete[] Supporto;
                  }
                  else{
	                   bool flag = false;
	                   while (!flag && P[t]!=-1){
                             t = P[t];
                              flag=true;
                              for (list<int>::iterator ia=out.begin();ia!=out.end();ia++){
                                  int j=*ia; 
                                  if (!isAncestor(P,t,j)) {
                                     flag=false;break;
                                  }
                              }
                       }//t is lca of outgroups
                       if (P[t]!=-1){//lca of outgroups is not the root
                          reroot(n,m,t,P,s,Suc1,Suc2,B,Support,P_new,Suc1_new,Suc2_new,B_new,Support_new);
                          subTree(n,m,P[t],P_new,Suc1_new,Suc2_new,B_new,Support_new,Labels,P1,B1,Support1,Labels1);
                       }
                       else{//lca of outgroups is the root
	                        bool flag = false;
	                        while (!flag && P[t1]!=-1){
                                  t1 = P[t1];
                                  flag=true;
                                  for (list<int>::iterator ia=in.begin();ia!=in.end();ia++){
                                      int j=*ia;
                                      if (!isAncestor(P,t1,j)) {flag=false;break;}
                                  }
                            }
                           //t1 is lca of ingroups
                            if (P[t1]==-1){//lca of ingroups is the root
                               cout<<"Tree "<<y<<": The outgroups are not separated from the ingroups"<<endl; 
                               exit( EXIT_FAILURE );
                            }
                            else{//lca of ingroups is not the root
                                reroot(n,m,t1,P,s,Suc1,Suc2,B,Support,P_new,Suc1_new,Suc2_new,B_new,Support_new);
                                subTree(n,m,t1,P_new,Suc1_new,Suc2_new,B_new,Support_new,Labels,P1,B1,Support1,Labels1);
                          }
                      }
                      m1=(int)P1.size()-1;
                      n1=m1/2;
                      if ((n1+out.size())==n+1){
                              int *Po = new int[m1+1];
                              listToArray(P1,Po);
                              double *Bo = new double[m1+1];
                              listToArray(B1,Bo);
                              string* Supporto = new string[m1+1];
                              listToArray(Support1,Supporto);
                              string* Labelso = new string[m1+1];
                              listToArray(Labels1,Labelso);
                              int *Suc1o = new int[n1];
                              int *Suc2o = new int[n1];
                              computeSuc(Po,Suc1o,Suc2o,m1+1,n1);
                              newicktree(n1,Po,Suc1o,Suc2o,Labelso,Bo,Supporto,w);
                              delete[]  Po;
                              delete[]  Bo;
                              delete[]  Supporto;
                              delete[] Labelso;
                              delete[] Suc1o;
                              delete[] Suc2o;
                      }
                      else {
                              cout<<"Tree "<<y<<": The outgroups are not separated from the ingroups"<<endl;
                              exit( EXIT_FAILURE );
                      }
                      delete[] P_new;
                      delete[] Suc1_new;
                      delete[] Suc2_new;
                      delete[] B_new;
                      delete[] Support_new;
                  }
             delete[] Suc1;
             delete[] Suc2;
             }
             
         }
         fclose(tree);
        fclose(w);
    }
    delete[]  P;
    delete[]  B;
    delete[]  Support;
    delete[] Labels;
}
Beispiel #16
0
int main(int argc, char** argv) {

	Player **Gamer;
	int numberOfGamers = 6; // количество игроков за столом
	int numberOfRounds=4;
	Gamer = new Player*[6]; // 6 игроков
	Pack Cards(0); // колода карт
	Card FiveCard[5]; // 5 общих карт
	int nRound = 1; // первый круг торговли
	/* создадим numberOfGamers игроков */
	for (int i = 0; i < numberOfGamers; i++) {
		Gamer[i] = new Player("Gamer" + from_int(i), 50, i);
		Gamer[i][0].SetCards(Cards.getCard(), Cards.getCard());
		std::cout << Gamer[i][0].getLogin() << " "
				<< Gamer[i][0].GetFirstCard().getRank() << " of "
				<< Gamer[i][0].GetFirstCard().getSuit() << " ";
		std::cout << Gamer[i][0].GetSecondCard().getRank() << " of "
				<< Gamer[i][0].GetSecondCard().getSuit() << std::endl;
	}
	// ставки игроков для теста
	/*
	int Stavki[4][6] = {
			{ 2, 2, 2, 2, 2, 2 },
			{ 6, 5, 6, 6, 6, 6 },
			{ 6, 0, 4, 5, 6, 6 },
			{ 2, 0, 0, 0, 4, 5 } };*/
	int **Stavki= new int*[numberOfRounds];
		for (int i=0;i<numberOfRounds;i++)
			Stavki[i]= new int[numberOfGamers];
	createSetOfStakes(Stavki,numberOfRounds,numberOfGamers);
	// объявление банка
	Bank bank(Gamer, numberOfGamers);
	// круг торговли и заполнение банка
	std::cout << "Round " << nRound << std::endl;
	// ставки игроков
	for (int i = 0; i < numberOfGamers; i++) {
		std::cout << "Gamer" << i + 1 << " stake is " << Stavki[nRound - 1][i]
				<< " ";
		bank.addPlayer(*Gamer[i], Stavki[nRound - 1][i], nRound - 1);
	}
	std::cout << std::endl;

	std::cout << "------------Card Pack-----------" << std::endl;
	for (int i = 0; i < 3; i++) {
		FiveCard[i] = Cards.getCard();
		std::cout << FiveCard[i].getRank() << " of " << FiveCard[i].getSuit()
				<< std::endl;
	}

	// круг торговли и заполнение банка
	std::cout << "Round " << ++nRound <<std::endl;
	// ставки игроков
	for (int i = 0; i < numberOfGamers; i++) {
		std::cout << "Gamer" << i + 1 << " stake is" << Stavki[nRound - 1][i]
				<< " ";
		bank.addPlayer(*Gamer[i], Stavki[nRound - 1][i], nRound - 1);
	}
	std::cout << std::endl;

	std::cout << "------------Card Pack-----------" << std::endl;
	FiveCard[3] = Cards.getCard();
	for (int i = 0; i < 4; i++) {
		std::cout << FiveCard[i].getRank() << " of " << FiveCard[i].getSuit()
				<< std::endl;
	}

	// круг торговли и заполнение банка
	std::cout << "Round " << ++nRound << std::endl;;
	// ставки игроков
	for (int i = 0; i < numberOfGamers; i++) {
		std::cout << "Gamer" << i + 1 << " stake is " << Stavki[nRound - 1][i]
				<< " ";
		bank.addPlayer(*Gamer[i], Stavki[nRound - 1][i], nRound - 1);
	}
	std::cout << std::endl;

	std::cout << "------------Card Pack-----------" << std::endl;
	FiveCard[4] = Cards.getCard();
	for (int i = 0; i < 5; i++) {
		std::cout << FiveCard[i].getRank() << " of " << FiveCard[i].getSuit()
				<< std::endl;
	}

	// последний круг торговли и заполнение банка
	std::cout << "Round " << ++nRound << std::endl;
	// ставки игроков
	for (int i = 0; i < numberOfGamers; i++) {
		std::cout << "Gamer" << i + 1 << " stake is" << Stavki[nRound - 1][i]
				<< " ";
		bank.addPlayer(*Gamer[i], Stavki[nRound - 1][i], nRound - 1);
	}
	std::cout << std::endl;

	Calculation counting(Gamer, FiveCard, 6);
	counting.WinnersCalc();
	for (int i = 0; i < 6; i++) {
		std::cout << Gamer[i][0].getLogin() << " is in "
				<< Gamer[i][0].getWinPlace() << " place " << " has "
				<< counting.GetCombinationName(Gamer[i]) << std::endl;
	}
	// раздача денег
	bank.winners();

	std::cout << std::endl;
	for (int i = 0; i < numberOfGamers; i++) {
		std::cout << "Position table: " << Gamer[i]->getPosition() << " "<<Gamer[i]->getLogin()<<" ";
		std::cout << Gamer[i]->getPurse() << "$"<<std::endl;
	}
	std::cout << "SumBank: " << bank.getSumBank() << std::endl;

	for (int i = 0; i < 6; i++) {
		delete[] Gamer[i];
	}
	delete[] Gamer;
	return 0;
}