说去下面数列的规律,并编程实现:
1
23
312
1231
23123
312312
1231231
大体想了下,应该会有更简洁的算法:
代码
public
static
void
Main(
string
[] args)
{
int a = 1 ;
int b = 1 ;
for ( int i = 1 ; i <= 7 ; i ++ )
{
if (b > 3 )
{
b = 1 ;
}
a = b;
for ( int j = 1 ; j <= i; j ++ )
{
if (a > 3 )
{
a = 1 ;
}
Console.Write(a);
a ++ ;
}
b ++ ;
Console.WriteLine();
}
Console.WriteLine( " Press any key to continue... " );
Console.ReadKey( true );
}
{
int a = 1 ;
int b = 1 ;
for ( int i = 1 ; i <= 7 ; i ++ )
{
if (b > 3 )
{
b = 1 ;
}
a = b;
for ( int j = 1 ; j <= i; j ++ )
{
if (a > 3 )
{
a = 1 ;
}
Console.Write(a);
a ++ ;
}
b ++ ;
Console.WriteLine();
}
Console.WriteLine( " Press any key to continue... " );
Console.ReadKey( true );
}