Yardım lütfen acil
Dec 18, 2012 at 10:53pm UTC
sadece for döngüsünü kullanarak
1 3 5
12 34 56
123 345 567
1234 3456 5678
çıktısını nasıl alabilirim ?
Dec 18, 2012 at 11:40pm UTC
I do not know your language but I have written some code.:)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <array>
#include <algorithm>
#include <iterator>
#include <iostream>
int main()
{
std::array<int , 3> a = { 1, 3, 5 };
for ( int i = 0; i < 4; i++ )
{
std::copy( a.begin(), a.end(), std::ostream_iterator<int >( std::cout, " " ) );
std::cout << std::endl;
std::transform( a.begin(), a.end(), a.begin(),
[]( int x ) { return ( 10 * x + x % 10 + 1 ); } );
}
}
EDIT: to simplify the code you can change
std::array<int, 3> a to usual array definition
int a[] and correspondingly change a.begin() and a.end() to std::begin( a ) and std::end( a ) everywhere in the code.
Last edited on Dec 18, 2012 at 11:55pm UTC
Dec 19, 2012 at 8:20am UTC
Thanks a lot for you help.
edit : Would you kindly write array without using?
Last edited on Dec 19, 2012 at 8:26am UTC
Dec 19, 2012 at 10:06am UTC
@exlarge
Would you kindly write array without using?
I have not understood what you mean, Do you mean to use an usual array? If so then the code will look the following way
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
int main()
{
const int N = 3;
const int base = 10;
int a[N] = { 1, 3, 5 };
for ( int i = 0; i < 4; i++ )
{
for ( int j = 0; j < N; j++ ) std::cout << a[j] << ' ' ;
std::cout << std::endl;
for ( int j = 0; j < N; j++ ) a[j] = base * a[j] + a[j] % base + 1;
}
}
By the way if it is required that after digit 9 there will be digit 0 then the main expression can be rewritten as
a[j] = base * a[j] + ( a[j] % base + 1 ) % base;
Last edited on Dec 19, 2012 at 10:37am UTC
Dec 19, 2012 at 11:00am UTC
If to use std::string then the code can look as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <iostream>
#include <string>
int main()
{
const int N = 3;
const int base = 10;
std::string s[N] = { "1" , "3" , "5" };
for ( int i = 0; i < 20; i ++ )
{
for ( int j = 0; j < N; j++ )
{
if ( i != 0 )
{
s[j].push_back( ( s[j].back() - '0' + 1 ) % base + '0' );
}
std::cout << s[j] << ' ' ;
}
std::cout << std::endl;
}
}
The output:
1 3 5
12 34 56
123 345 567
1234 3456 5678
12345 34567 56789
123456 345678 567890
1234567 3456789 5678901
12345678 34567890 56789012
123456789 345678901 567890123
1234567890 3456789012 5678901234
12345678901 34567890123 56789012345
123456789012 345678901234 567890123456
1234567890123 3456789012345 5678901234567
12345678901234 34567890123456 56789012345678
123456789012345 345678901234567 567890123456789
1234567890123456 3456789012345678 5678901234567890
12345678901234567 34567890123456789 56789012345678901
123456789012345678 345678901234567890 567890123456789012
1234567890123456789 3456789012345678901 5678901234567890123
12345678901234567890 34567890123456789012 56789012345678901234
Topic archived. No new replies allowed.