for (int i = 1; i < 10; i++) { for (int j = 1; j <=i; j++) { Console.Write(i); } Console.WriteLine(); }
是不是给你55555,返回4444? public static int previous(int x) { int y = 0; int z = x % 10 - 1; for (int i = 0; i < z; i++ ) { y = y * 10 + 1; } return y * z; }
string x = "999999999"; for (int i = x.Length; i > 0; i--) { for (int j = 0; j < i; j++) { Console.Write(i); } Console.WriteLine(); }
用递归: 从头到尾: using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace class_for_csdn { class Program { static void Main(string[] args) { int a =int.Parse ( Console.ReadLine()); calculator(a); Console.ReadLine(); } static void calculator(int n) { if (n != 1) calculator(n-1); for (int i = 0; i < n; i++) Console.Write(n); Console.WriteLine(); } } } 从尾到头: using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace class_for_csdn { class Program { static void Main(string[] args) { int a =int.Parse ( Console.ReadLine()); calculator(a); Console.ReadLine(); } static void calculator(int n) { for (int i = 0; i < n; i++) Console.Write(n); Console.WriteLine(); if (n != 1) calculator(n-1); } } }
{
for (int j = 1; j <=i; j++)
{
Console.Write(i);
}
Console.WriteLine();
}
public static int previous(int x) {
int y = 0;
int z = x % 10 - 1;
for (int i = 0; i < z; i++ ) {
y = y * 10 + 1;
}
return y * z;
}
for (int i = x.Length; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
Console.Write(i);
}
Console.WriteLine();
}
从头到尾:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace class_for_csdn
{
class Program
{
static void Main(string[] args)
{
int a =int.Parse ( Console.ReadLine());
calculator(a);
Console.ReadLine();
}
static void calculator(int n)
{
if (n != 1)
calculator(n-1);
for (int i = 0; i < n; i++)
Console.Write(n);
Console.WriteLine();
}
}
}
从尾到头:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace class_for_csdn
{
class Program
{
static void Main(string[] args)
{
int a =int.Parse ( Console.ReadLine());
calculator(a);
Console.ReadLine();
}
static void calculator(int n)
{
for (int i = 0; i < n; i++)
Console.Write(n);
Console.WriteLine();
if (n != 1)
calculator(n-1);
}
}
}