n是层数,result为结果: string[] temp1=new string[n]; string[] temp2 = new string[n]; string[] result = new string[2*n]; for (int i = 0; i < n; i++) { int k = 0; for (int j = 0; j < n; j++) { if (k < (n - i)) { k++; } temp1[i] += Convert.ToString(n - k + 1); } for (int x = 1; x <= temp1[i].Length; x++) { temp2[i] += temp1[i][temp1[i].Length - x]; } result[n - i - 1] = temp1[i] + temp2[i]; result[n + i] = result[n - i - 1]; }
好象zswang出过这道题的。 重复了!!
for (int i = 1; i <= 9; i++) for (int j = 0; j < i * 2 - 1; j++) for (int k = 0; k < i * 2 - 1; k++) Console.Write(Math.Max(Math.Abs(j - i + 1), Math.Abs(k - i + 1)) + 1 + (k == i * 2 - 2 ? j == i * 2 - 2 ? "\n------\n" : "\n" : ""));
tju ACM上面的题目 好像!
for (int i = 1; i <= 9; i++) for (int j = 0; j < i * 2 - 1; j++) for (int k = 0; k < i * 2 - 1; k++) Console.Write(Math.Max(Math.Abs(j - i + 1), Math.Abs(k - i + 1)) + 1 + (k == i * 2 - 2 ? j == i * 2 - 2 ? "\n------\n" : "\n" : ""));几个星期前看的。还上过全论坛榜首!
class Program { static void Main(string[] args) { OutPutNum opm = new OutPutNum(); opm.OutPut(10); Console.ReadLine(); } } public class OutPutNum { public void OutPut(int times) { int[,] arries = new int[2 * times, 2 * times]; for (int i = 0; i < times; i++) { for (int j = 0; j < times; j++) { if (i == 0 || j == 0) arries[i, j] = times; else arries[i, j] = arries[i - 1, j - 1] - 1; arries[2 * times - i - 1, j] = arries[i, j]; arries[i, 2 * times - 1 - j] = arries[i, j]; arries[2 * times - i - 1, 2 * times - 1 - j] = arries[i, j]; } } for (int m = 0; m < 2 * times; m++) { for (int n = 0; n < 2 * times; n++) { Console.Write(arries[m,n]); Console.Write("\t"); } Console.WriteLine("\n"); } } }我的方法是不是最笨重最没效率的?!!!!
for (int i = 0; i < n; i++) { int k = 0; for (int j = 0; j < n; j++) { if (k < (n - i)) { k++; } temp1[i] += Convert.ToString(n - k + 1); } for (int x = 1; x <= temp1[i].Length; x++) { temp2[i] += temp1[i][temp1[i].Length - x]; } result[n - i - 1] = temp1[i] + temp2[i]; result[n + i] = result[n - i - 1]; }
凑凑热闹,用vb.net。呵呵,还不算很慢吧。 '算法 Private Sub OutPut(ByRef str As String, ByVal m As Integer, ByRef flag As Boolean, ByRef newStr As ArrayList) Dim n As Integer = str.Length - 2 * m '中间字符串的长度 Dim middleStr As String = str.Substring(m - 1, n) '中间字符串 Dim beginStr As String = str.Substring(0, m) '左边字符串 Dim endStr As String = strReturn(beginStr) ' 右边字符串 Dim tempStr As String Dim mStrFistChar As String = CInt(middleStr.Substring(0, 1)) - 1 '中间字符串首字母 For i As Integer = 0 To n - 1 tempStr = tempStr & mStrFistChar Next Dim resultStr As String If mStrFistChar = 1 Then '退出递归的条件 flag = True End If resultStr = beginStr & tempStr & endStr ' newStr = newStr & resultStr & vbCrLf newStr.Add(resultStr) If flag = False Then Call OutPut(resultStr, m + 1, flag, newStr) '递归 Else '退出递归 For k As Integer = newStr.Count - 1 To 0 Step -1 newStr.Add(newStr(k)) Next End If End Sub '颠倒字符串 Private Function strReturn(ByVal str As String) As String Dim newStr As String = "" Dim j As Integer = str.Length For i As Integer = j - 1 To 0 Step -1 newStr = newStr & str.Substring(i, 1) Next Return newStr End Function Private Sub Test_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim str As String = "999999999999999999" If str.Length <= CInt(str.Substring(0, 1)) * 2 Then MessageBox.Show("字符串的长度必须大于或等于字符的2倍") Exit Sub End If Dim newStr As New ArrayList newStr.Add(str) Dim flag As Boolean = False Dim m As Integer = 1 Call Me.OutPut(str, m, flag, newStr) Dim showStr As String For i As Integer = 0 To newStr.Count - 1 '循环输出 showStr &= newStr(i) & vbCrLf Next MessageBox.Show(showStr) End Sub
string[] temp2 = new string[n];
string[] result = new string[2*n];
for (int i = 0; i < n; i++)
{
int k = 0;
for (int j = 0; j < n; j++)
{
if (k < (n - i))
{ k++; } temp1[i] += Convert.ToString(n - k + 1); }
for (int x = 1; x <= temp1[i].Length; x++)
{
temp2[i] += temp1[i][temp1[i].Length - x];
}
result[n - i - 1] = temp1[i] + temp2[i];
result[n + i] = result[n - i - 1];
}
重复了!!
for (int i = 1; i <= 9; i++)
for (int j = 0; j < i * 2 - 1; j++)
for (int k = 0; k < i * 2 - 1; k++)
Console.Write(Math.Max(Math.Abs(j - i + 1), Math.Abs(k - i + 1)) + 1 + (k == i * 2 - 2 ? j == i * 2 - 2 ? "\n------\n" : "\n" : ""));
for (int j = 0; j < i * 2 - 1; j++)
for (int k = 0; k < i * 2 - 1; k++)
Console.Write(Math.Max(Math.Abs(j - i + 1), Math.Abs(k - i + 1)) + 1 + (k == i * 2 - 2 ? j == i * 2 - 2 ? "\n------\n" : "\n" : ""));几个星期前看的。还上过全论坛榜首!
{
static void Main(string[] args)
{
OutPutNum opm = new OutPutNum();
opm.OutPut(10);
Console.ReadLine();
}
} public class OutPutNum
{
public void OutPut(int times)
{
int[,] arries = new int[2 * times, 2 * times];
for (int i = 0; i < times; i++)
{
for (int j = 0; j < times; j++)
{
if (i == 0 || j == 0)
arries[i, j] = times;
else
arries[i, j] = arries[i - 1, j - 1] - 1;
arries[2 * times - i - 1, j] = arries[i, j];
arries[i, 2 * times - 1 - j] = arries[i, j];
arries[2 * times - i - 1, 2 * times - 1 - j] = arries[i, j];
}
}
for (int m = 0; m < 2 * times; m++)
{
for (int n = 0; n < 2 * times; n++)
{
Console.Write(arries[m,n]);
Console.Write("\t");
}
Console.WriteLine("\n");
}
}
}我的方法是不是最笨重最没效率的?!!!!
{
int k = 0;
for (int j = 0; j < n; j++)
{
if (k < (n - i))
{ k++; } temp1[i] += Convert.ToString(n - k + 1); }
for (int x = 1; x <= temp1[i].Length; x++)
{
temp2[i] += temp1[i][temp1[i].Length - x];
}
result[n - i - 1] = temp1[i] + temp2[i];
result[n + i] = result[n - i - 1];
}
'算法
Private Sub OutPut(ByRef str As String, ByVal m As Integer, ByRef flag As Boolean, ByRef newStr As ArrayList)
Dim n As Integer = str.Length - 2 * m '中间字符串的长度
Dim middleStr As String = str.Substring(m - 1, n) '中间字符串
Dim beginStr As String = str.Substring(0, m) '左边字符串
Dim endStr As String = strReturn(beginStr) ' 右边字符串
Dim tempStr As String
Dim mStrFistChar As String = CInt(middleStr.Substring(0, 1)) - 1 '中间字符串首字母
For i As Integer = 0 To n - 1
tempStr = tempStr & mStrFistChar
Next
Dim resultStr As String
If mStrFistChar = 1 Then '退出递归的条件
flag = True
End If
resultStr = beginStr & tempStr & endStr
' newStr = newStr & resultStr & vbCrLf
newStr.Add(resultStr)
If flag = False Then
Call OutPut(resultStr, m + 1, flag, newStr) '递归
Else '退出递归
For k As Integer = newStr.Count - 1 To 0 Step -1
newStr.Add(newStr(k))
Next
End If
End Sub '颠倒字符串
Private Function strReturn(ByVal str As String) As String
Dim newStr As String = ""
Dim j As Integer = str.Length
For i As Integer = j - 1 To 0 Step -1
newStr = newStr & str.Substring(i, 1)
Next
Return newStr
End Function Private Sub Test_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim str As String = "999999999999999999"
If str.Length <= CInt(str.Substring(0, 1)) * 2 Then
MessageBox.Show("字符串的长度必须大于或等于字符的2倍")
Exit Sub
End If
Dim newStr As New ArrayList
newStr.Add(str)
Dim flag As Boolean = False
Dim m As Integer = 1
Call Me.OutPut(str, m, flag, newStr)
Dim showStr As String
For i As Integer = 0 To newStr.Count - 1 '循环输出
showStr &= newStr(i) & vbCrLf
Next
MessageBox.Show(showStr)
End Sub
Response.Write("322223");
Response.Write("321123");
Response.Write("321123");
Response.Write("322223");
Response.Write("333333");楼主,结贴吧!