设为首页收藏本站

八达网

 找回密码
 注册
查看: 590|回复: 36
打印 上一主题 下一主题

编程达人帮我看道题。。。。

[复制链接]

2

主题

0

好友

6万

积分

仲裁者

A-CUP 才是王道!

2007年度八达十大杰出青年 2008年度八达十大水友 2009年度八达十大水友

跳转到指定楼层
1
发表于 2011-2-1 18:29 |只看该作者 |正序浏览
请看下面有规律的数字循环
# 1
# 11
# 21
# 1211
# 111221
# 312211
# 13112221
# 1113213211

就是说当前看到是什么数字就把它累计到下一个不同的数字,然后输出当前累计数和数字

比如第五行是一个1,一个2,两个1...正好是第四行的内容

现在需要编一个程序,说出某一行的值是多少

比如

give number: 3
result: 21

give number: 7
result: 13112221

程序可以用C++写,不过最好用java,下面有模板

import java.util.Scanner;

public class FunnyNumbers {
    static Scanner reader = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.print("give number: ");
        int n = reader.nextInt();
        System.out.println( "result "+makenumber(n) );
    }

    public static String makenumber(int number) {
        // write ur code here
            return "";
    }
}

上八达,日熊逼!

11

主题

0

好友

2万

积分

大和

37
发表于 2011-2-2 00:29 |只看该作者
哎 罪过 我java考试都挂了。。随便写写而已。。
回复

使用道具 举报

2

主题

0

好友

6万

积分

仲裁者

A-CUP 才是王道!

2007年度八达十大杰出青年 2008年度八达十大水友 2009年度八达十大水友

36
发表于 2011-2-1 23:33 |只看该作者
这种题目都不会还是退学吧
nttstar 发表于 2011-2-1 16:29

.................

上八达,日熊逼!
回复

使用道具 举报

2

主题

0

好友

7471

积分

大象

35
发表于 2011-2-1 23:29 |只看该作者
晕,貌似标准的也是O(n^2)
回复

使用道具 举报

2

主题

0

好友

7471

积分

大象

34
发表于 2011-2-1 23:27 |只看该作者
靠,看了标准答案,羞愧飘过,标准答案是O(n),俺的是O(n^2)
回复

使用道具 举报

2

主题

0

好友

7471

积分

大象

33
发表于 2011-2-1 22:33 |只看该作者
晕能给我500水晶么,8王。
回复

使用道具 举报

2

主题

0

好友

7471

积分

大象

32
发表于 2011-2-1 22:32 |只看该作者
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

string getnextnumber(string number)
{
        string rs;
        int count = 0;
        string::iterator it = number.begin(), ia = it;
        for(; it != number.end(); it++)
        {
                if(*it == *ia)
                        ++count;
                else
                {
                        ostringstream s1;
                        ostringstream s2;
                        s1 << count;
                        rs += s1.str();
                        s2 << *ia;
                        rs += s2.str();
                        ia = it;
                        count = 1;
                }       
        }
        ostringstream s1;
        ostringstream s2;
        s1 << count;
        rs += s1.str();
        s2 << *ia;
        rs += s2.str();

        return rs;

}

string makenumber(int n)
{
        string rs("1");
        for(int i = 0; i < n - 1; i++)
        {
                rs = getnextnumber(rs);
        }
    return rs;
}

int main()
{
        cout << makenumber(8) << endl;

        return 0;
}
回复

使用道具 举报

1

主题

0

好友

3万

积分

大和

bb

31
发表于 2011-2-1 22:29 |只看该作者
这种题目都不会还是退学吧
回复

使用道具 举报

23

主题

0

好友

4万

积分

大和

30
发表于 2011-2-1 22:02 |只看该作者
atoi(),应该用这个吧
回复

使用道具 举报

2

主题

5

好友

11万

积分

黑暗执政官

29
发表于 2011-2-1 21:05 |只看该作者
学习了
回复

使用道具 举报

2

主题

1

好友

3万

积分

大和

28
发表于 2011-2-1 21:04 |只看该作者
回复

使用道具 举报

2

主题

1

好友

3万

积分

大和

27
发表于 2011-2-1 21:04 |只看该作者
Date: Mon, 5 Mar 2007 21:21:24 -0300
From: Paulo Ortolan (paulo.ortolan(AT)gmail.com)

Java Code for A005150

*public class DescribePrevious {
    public static void main(String[] args) {
        char[] cSeq = new char[] {'1'};
        char s = 0;
        int count = 0;
        String newSeq = "";

        System.out.println(new String(cSeq));

        for(int i = 0; i < 20; i++) {
            for(int j = 0; j < cSeq.length; j++) {
                if(j == 0) {
                    s = cSeq[j];
                    count++;
                } else {
                    if(s == cSeq[j]) {
                        count++;
                    } else {
                        newSeq += count + "" + s;
                        count = 1;
                        s = cSeq[j];
                    }
                }
            }

            newSeq += count + "" + s;
            System.out.println(newSeq);
            cSeq = newSeq.toCharArray();
            newSeq = "";
            count = 0;
        }
    }
}

--
Paulo Henrique Ortolan
Java Developer
回复

使用道具 举报

2

主题

5

好友

11万

积分

黑暗执政官

26
发表于 2011-2-1 20:25 |只看该作者
不懂啊
回复

使用道具 举报

2

主题

0

好友

6万

积分

仲裁者

A-CUP 才是王道!

2007年度八达十大杰出青年 2008年度八达十大水友 2009年度八达十大水友

25
发表于 2011-2-1 20:11 |只看该作者
给了你1000水晶

上八达,日熊逼!
回复

使用道具 举报

2

主题

0

好友

6万

积分

仲裁者

A-CUP 才是王道!

2007年度八达十大杰出青年 2008年度八达十大水友 2009年度八达十大水友

24
发表于 2011-2-1 20:07 |只看该作者
21楼V5

上八达,日熊逼!
回复

使用道具 举报

64

主题

0

好友

7284

积分

大象

23
发表于 2011-2-1 19:59 |只看该作者
回复

使用道具 举报

0

主题

4

好友

10万

积分

黑暗执政官

=,=

22
发表于 2011-2-1 19:54 |只看该作者
非灌水機,純正手動輸入,管理員明鑒.
回复

使用道具 举报

11

主题

0

好友

2万

积分

大和

21
发表于 2011-2-1 19:54 |只看该作者
import java.util.Scanner;

public class funny {
    static Scanner reader = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.print("give number: ");
        int n = reader.nextInt();
        System.out.println( "result "+makenumber(n) );
    }

    public static String makenumber(int number) {
            String num = "1";
             
            for (int i=1;i<=number;i++) {
                    System.out.println(num);
                     
                    StringBuilder result= new StringBuilder();
                     
                    char repeat= num.charAt(0);
                    num= num.substring(1) + " ";
                    int times= 1;
             
                    for(char actual: num.toCharArray()){
                            if(actual != repeat){
                                    result.append(times + "" + repeat);
                                    times= 1;
                                    repeat= actual;
                            }else{
                                    times+= 1;
                            }
                    }
                    num=result.toString();
            }
            return num;
    }
}
1

查看全部评分

回复

使用道具 举报

11

主题

0

好友

2万

积分

大和

20
发表于 2011-2-1 19:39 |只看该作者
哦 。LZ那个格式比较麻烦啊   要在一个函数里面写完

不然的话可以另外建一个函数,然后再在LZ那个格式里面写个for循环调用

哎 可惜我没时间啊 我要出门了。。
回复

使用道具 举报

11

主题

0

好友

2万

积分

大和

19
发表于 2011-2-1 19:34 |只看该作者
这。。。我帮你改改
回复

使用道具 举报

0

主题

0

好友

1672

积分

坦克

18
发表于 2011-2-1 19:33 |只看该作者
回复

使用道具 举报

2

主题

0

好友

6万

积分

仲裁者

A-CUP 才是王道!

2007年度八达十大杰出青年 2008年度八达十大水友 2009年度八达十大水友

17
发表于 2011-2-1 19:30 |只看该作者
话说那个网址不是都有源码了么
越睡越困 发表于 2011-2-1 13:27

程序稍微有点不一样,不会改啊

上八达,日熊逼!
回复

使用道具 举报

11

主题

0

好友

2万

积分

大和

16
发表于 2011-2-1 19:27 |只看该作者
话说那个网址不是都有源码了么
回复

使用道具 举报

56

主题

0

好友

5万

积分

光明执政官

15
发表于 2011-2-1 19:25 |只看该作者
回复

使用道具 举报

2

主题

0

好友

6万

积分

仲裁者

A-CUP 才是王道!

2007年度八达十大杰出青年 2008年度八达十大水友 2009年度八达十大水友

14
发表于 2011-2-1 19:25 |只看该作者
写几个具体应用比研究这些有用多了
OruA 发表于 2011-2-1 13:24


说风凉话的Orua大大!!

上八达,日熊逼!
回复

使用道具 举报

190

主题

5

好友

4万

积分

管理员

小姐别发贴,是我

Rank: 9Rank: 9Rank: 9Rank: 9Rank: 9

战队
[B.D]=
种族
Protoss

2013年夜饭

13
发表于 2011-2-1 19:24 |只看该作者
写几个具体应用比研究这些有用多了
向还在为STL奋斗的玩家致敬!
回复

使用道具 举报

11

主题

0

好友

2万

积分

大和

12
发表于 2011-2-1 19:23 |只看该作者
An example:

    * Starting with the number 1, you have one 1 which produces 11.
    * Starting with 11, you have two 1's i.e. 21
    * Starting with 21, you have one 2, then one 1 i.e. (12)(11) which becomes 1211
    * Starting with 1211 you have one 1, one 2, then two 1's i.e. (11)(12)(21) which becomes 111221


贴这一段估计就都能看懂题目了
回复

使用道具 举报

2

主题

0

好友

6万

积分

仲裁者

A-CUP 才是王道!

2007年度八达十大杰出青年 2008年度八达十大水友 2009年度八达十大水友

11
发表于 2011-2-1 19:01 |只看该作者
找到规律了就能写出来了啊~
肥熊大BB 发表于 2011-2-1 12:58

问题是不会写啊= ="

上八达,日熊逼!
回复

使用道具 举报

7

主题

0

好友

5194

积分

飞龙

10
发表于 2011-2-1 18:58 |只看该作者
找到规律了就能写出来了啊~
回复

使用道具 举报

2

主题

0

好友

6万

积分

仲裁者

A-CUP 才是王道!

2007年度八达十大杰出青年 2008年度八达十大水友 2009年度八达十大水友

9
发表于 2011-2-1 18:53 |只看该作者
8daer智商太低,哥看到这几个数字一下子就找到规律了

上八达,日熊逼!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

手机版|Archiver|八达网    

GMT+8, 2026-1-27 11:23

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部