Problem mit einer Zählerschleife



  • Also bevor du unsere Ansätze als scheiße betrachtest ... guck dir mal diesen wirklich unschönen Code von dir an ... ne constante in ner schleife usw ... sowas geht garnet 😉



  • ^^ war wirklich net bös gemeint, aber 3 schleifen hätte ich gemacht, wenn es sinnvoll gewesen wäre.



  • meinst du sowas ?

    #include <iostream> 
    using namespace std; 
    
    int main(void){ 
    	int Ary[3] = {0,0,0};
    	cout << Ary[2] << Ary[1] << Ary[0]<<endl;
    	for(int i=0;i<28;i++){
    		if((i%3==0) && (Ary[0]!=0)){
    			Ary[0] = 0;
    			Ary[1] = Ary[1]+1;
    
    			if((Ary[1]%3 == 0)&& (Ary[1]!=0) ){
    				Ary[1] = 0;
    				Ary[2] = Ary[2]+1;
    			}
    
    		}	
    		else
    			Ary[0] = Ary[0]+1;
    		cout << Ary[2] << Ary[1] << Ary[0]<<endl;
    	}
    	cin.get();
        return 0; 
    }
    

    habs nicht getestet....
    aber wenn du das so meinst dann nimm lieber doch die schleifen -.-

    finde so einen code scheiße :P, aber wozu willst du das so ?



  • naja... ähnlihes problem; es werden nicht alle möglichkeiten durchprobiert. und das beispiel scheint im gegensatz zu meinem auch noch abhängig von der spaltenzahl zu sein.



  • meinste so?

    int main()
    {	
    	int ln = 3; 
    	int *array = new int[ln+1]; 
    	int limes = 3; 
    
    	for(int i = 0; i < ln+1; i++) { 
    		array[i] = 0;		
    	} 	
    
    	while(array[ln] == 0) { 
    		for(int j = ln-1; j >= 0; --j) { 
    			cout<<array[j]<<" "; 
    		} 
    		cout<<endl;
    
    		int i = 0;
    		for(; array[i] == limes; ++i) {
    			array[i] = 0;			
    		}
    		++array[i];		 
    	} 
    }
    


  • Blubb?

    #include <stdio.h>
    #include <cassert>
    
    bool inc_array(unsigned* array, unsigned limit, unsigned index)
    {
        bool complete = false;
        if (array[index] >= limit)
        {
            assert(array[index] == limit);
            if (index > 0)
            {
                array[index] = 0;
                complete = inc_array(array, limit, index - 1);
            }
            else
                complete = true;
        }
        else
            array[index]++;
    
        return complete;
    }
    
    void dump_array(unsigned* array, unsigned size)
    {
        printf("[");
        for (unsigned i = 0; i < size; i++)
        {
            printf("%d", array[i]);
            if ((i + 1) != size)
                printf(",");
        }
        printf("]\n");
    }
    
    int main()
    {
        unsigned array[3] = {};
    
        printf("beginning with:\n");
        dump_array(array, 3);
    
        unsigned step = 1;
        while (!inc_array(array, 5, 2))
        {
            printf("... step %d ...\n", step);
            dump_array(array, 3);
            step++;
        }
        printf("complete.\n");
        return 0;
    }
    

    So und nu sag mir ob ich doof bin (==die Frage falsch verstanden hab), oder ob das nu wirklich so einfach war... 🙂



  • Ist jetzt nur so eben hingekladdet, aber so etwa würd ich das anfangen:

    #include <algorithm>
    #include <iostream>
    #include <iterator>
    
    static void foo_aux(int         *v,
    		    std::size_t  v_len,
    		    unsigned     max,
    		    unsigned     rec_level) {
      if(rec_level == v_len){
        std::copy(v, v + v_len, std::ostream_iterator<int>(std::cout, " "));
        std::cout << std::endl;
      } else {
        foo_aux(v, v_len, max, rec_level + 1);
        while(v[rec_level]++ < max) {
          std::fill(v + rec_level + 1, v + v_len, 0);
    
          foo_aux(v, v_len, max, rec_level + 1);
        }
      }
    }
    
    void foo(unsigned len, unsigned max) {
      int *buf = new int[len];
      foo_aux(buf, len, max, 0);
      delete[] buf;
    }
    
    int main() {
      foo(3, 3);
    }
    


  • wie gesagt.. wenn es elgant machen willst dann "rekursiv" 😉



  • Äh, habe ich was verpasst? Wieso nicht einfach so:

    int const upper_limit = 255; // == (333)4
    
    for(int i = 0; i < upper_limit; ++i)
        cout << to_system_with_base(i, 4) << endl;
    

    'to_system_with_base' bleibt als Übung für den OP. Sollte sich aber im Archiv des Forums in tausendfacher Ausführung finden.



  • bei dem Problem weiß es wohl niemand -.-



  • ich denke mal den absoluten sieg hat sich life verdient^^

    int main()
    {   
        int ln = 3;
        int *array = new int[ln+1];
        int limes = 3;
    
        for(int i = 0; i < ln+1; i++) {
            array[i] = 0;       
        }    
    
        while(array[ln] == 0) {
            for(int j = ln-1; j >= 0; --j) {
                cout<<array[j]<<" ";
            }
            cout<<endl;
    
            int i = 0;
            for(; array[i] == limes; ++i) {
                array[i] = 0;           
            }
            ++array[i];         
        }
    }
    

    der code scheint am schnellsten zu sein!


Anmelden zum Antworten