求算法

Messi_Messi 2009-12-08 06:08:40
1,22,333,4444,55555,..........nnnnn(n个n)
用后一个数表示前一个数,急求,谢谢啦
用C#
...全文
94 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
amoeet 2009-12-09
  • 打赏
  • 举报
回复
用递归:
从头到尾:
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);


}
}
}
hhc123 2009-12-08
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 lovetmlong 的回复:]
引用 3 楼 hhc123 的回复:
  for (int i = 1; i < 10; i++)
            {
                for (int j = 1; j <=i; j++)
                {
                    Console.Write(i);
                }
                Console.WriteLine();
            }

太谢谢了,谢谢
[/Quote]
这样是从头到尾啊,从尾到头的话就麻烦一点点
hhc123 2009-12-08
  • 打赏
  • 举报
回复
大于10之后肯定不一样了,呵呵

hhc123 2009-12-08
  • 打赏
  • 举报
回复
string x = "999999999";
for (int i = x.Length; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
Console.Write(i);
}
Console.WriteLine();
}
shine333 2009-12-08
  • 打赏
  • 举报
回复
是不是给你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;
}
Messi_Messi 2009-12-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hhc123 的回复:]
  for (int i = 1; i < 10; i++)
            {
                for (int j = 1; j <=i; j++)
                {
                    Console.Write(i);
                }
                Console.WriteLine();
            }
[/Quote]
太谢谢了,谢谢
Messi_Messi 2009-12-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hhc123 的回复:]
后一个数是前面一个数中的如+1
1+1=2;
2就是在第二位
就转出2个2就是22
怎么就做不出来啊
[/Quote]
你们都是牛人
fengling2001 2009-12-08
  • 打赏
  • 举报
回复
“用后一个数表示前一个数”
n如果大于10之后,情况应该不一样吧
hhc123 2009-12-08
  • 打赏
  • 举报
回复
for (int i = 1; i < 10; i++)
{
for (int j = 1; j <=i; j++)
{
Console.Write(i);
}
Console.WriteLine();
}
十八道胡同 2009-12-08
  • 打赏
  • 举报
回复
拼接字符串

第一个1个1,后面2个2,接着n个n
hhc123 2009-12-08
  • 打赏
  • 举报
回复
后一个数是前面一个数中的如+1
1+1=2;
2就是在第二位
就转出2个2就是22
怎么就做不出来啊

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧