Skip navigation

Category Archives: technics

昨晚备课准备了一晚上,结果下午被两个小孩放鸽子掉了,郁闷中拿起Inside C#,很久没碰了呶
 
看到一篇文章(是JAVA语言的,好象可以通用来看看),想到考试前季GG给我们的复习材料中的题目,觉得应该可以补充一下,拿来大家看看了。
 
 
问题一:我声明了什么!

 
String s = "Hello world!";

        许多人都做过这样的事情,但是,我们到底声明了什么?回答通常是:一个String,内容是“Hello world!”。这样模糊的回答通常是概念不清的根源。如果要准确的回答,一半的人大概会回答错误。
        这个语句声明的是一个指向对象的引用,名为“s”,可以指向类型为String的任何对象,目前指向"Hello world!"这个String类型的对象。这就是真正发生的事情。我们并没有声明一个String对象,我们只是声明了一个只能指向String对象的引用变量。所以,如果在刚才那句语句后面,如果再运行一句:
       String string = s;

我们是声明了另外一个只能指向String对象的引用,名为string,并没有第二个对象产生,string还是指向原来那个对象,也就是,和s指向同一个对象。

 
问题二:"=="和equals方法究竟有什么区别?
 
==操作符专门用来比较变量的值是否相等。比较好理解的一点是:
int a=10;
int b=10;
a==b将是true
但不好理解的地方是:
String a=new String("foo");
String b=new String("foo");

a==b将返回false
//一模一样的题目,我还和小补丁他主人讨论半天呢

 

        根据前一帖说过,对象变量其实是一个引用,它们的值是指向对象所在的内存地址,而不是对象本身。a和b都使用了new操作符,意味着将在内存中产生两个内容为"foo"的字符串,既然是“两个”,它们自然位于不同的内存地址。a和b的值其实是两个不同的内存地址的值,所以使用"=="操作符,结果会是false。诚然,a和b所指的对象,它们的内容都是"foo",应该是“相等”,但是==操作符并不涉及到对象内容的比较。对象内容的比较,正是equals方法做的事。

看一下Object对象的equals方法是如何实现的:
boolean equals(Object o){
return this==o;
}
Object对象默认使用了==操作符。所以如果你自创的类没有覆盖equals方法,那你的类使用equals和使用==会得到同样的结果。同样也可以看出,Object的equals方法没有达到equals方法应该达到的目标:比较两个对象内容是否相等。因为答案应该由类的创建者决定,所以Object把这个任务留给了类的创建者。

看一下一个极端的类:
Class Monster{
private String content;

boolean equals(Object another){ return true;}
}
覆盖了equals方法。这个实现会导致无论Monster实例内容如何,它们之间的比较永远返回true

所以当是用equals方法判断对象的内容是否相等,请不要想当然。因为可能认为相等,而这个类的作者不这样认为,而类的equals方法的实现是由他掌握的。

问题三:String到底变了没有?

没有

因为String被设计成不可变(immutable)类,所以它的所有对象都是不可变对象。请看下列代码:

String s = "Hello";
s = s + " world!";

s所指向的对象是否改变了呢?从本系列第一篇的结论很容易导出这个结论。我们来看看发生了什么事情。在这段代码中,s原先指向一个String对象,内容是"Hello",然后我们对s进行了+操作,那么s所指向的那个对象是否发生了改变呢?答案是没有。这时,s不指向原来那个对象了,而指向了另一个String对象,内容为"Hello world!",原来那个对象还存在于内存之中,只是s这个引用变量不再指向它了。
通过上面的说明,我们很容易导出另一个结论,如果经常对字符串进行各种各样的修改,或者说,不可预见的修改,那么使用String来代表字符串的话会引起很大的内存开销。因为String对象建立之后不能再改变,所以对于每一个不同的字符串,都需要一个String对象来表示。这时,应该考虑使用StringBuffer类,它允许修改,而不是每个不同的字符串都要生成一个新的对象。并且,这两种类的对象转换十分容易。
同时,我们还可以知道,如果要使用内容相同的字符串,不必每次都new一个String。例如我们要在构造器中对一个名叫s的String引用变量进行初始化,把它设置为初始值,应当这样做:
public class Demo {
private String s;

public Demo {
s = "Initial Value";
}

}
而非  s = new String("Initial Value");
后者每次都会调用构造器,生成新对象,性能低下且内存开销大,并且没有意义,因为String对象不可改变,所以对于内容相同的字符串,只要一个String对象来表示就可以了。也就说,多次调用上面的构造器创建多个对象,他们的String类型属性s都指向同一个对象
上面的结论还基于这样一个事实:对于字符串常量,如果内容相同,Java认为它们代表同一个String对象。而用关键字new调用构造器,总是会创建一个新的对象,无论内容是否相同。
至于为什么要把String类设计成不可变类,是它的用途决定的。其实不只String,很多Java标准类库中的类都是不可变的。在开发一个系统的时候,我们有时候也需要设计不可变类,来传递一组相关的值,这也是面向对象思想的体现。不可变类有一些优点,比如因为它的对象是只读的,所以多线程并发访问也不会有任何问题。当然也有一些缺点,比如每个不同的状态都要一个对象来代表,可能会造成性能上的问题。所以Java标准类库还提供了一个可变版本,即StringBuffer。

 

还有问题四五六,自己去看啦,不做修改了,上面有连接的开学聚聚!

第一题:
Problem Statement

In the factorial number system the value of the first digit (from the right) is 1!, the value of the second digit is 2!, …, and the value of the n-th digit is n!. This means that any decimal number d can be written in this system as: anan-1…a2a1, where d = an * n! + an-1 * (n-1)! + … + a2 * 2! + a1 * 1! and 0 <= ai <= i for all i.
Given an int num in decimal, return its representation in the factorial number system.

Definition

Class:FactorialSystem
Method:convert
Parameters:int
Returns:int
Method signature:int convert(int num)
(be sure your method is public)

Notes – n! = 1 * 2 * … * n
Constraints – num will be between 1 and 3628799 (10!-1), inclusive.

Examples
0) 1               Returns: 1
1) 24             Returns: 1000             24 = 4!
2) 153           Returns: 11111           153 = 1! + 2! + 3! + 4! + 5!.
3) 133           Returns: 10201
4) 3628799    Returns: 987654321    Largest possible input.

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

=======================================================

using System;
class FactorialSystem
{   static void Main()
    {   Console.Write("Please input an non-zero positive integer number");
        Convert s=new Convert();
        s.Convert(d);
        Console.Write("The return value is {0}.",s.Convert(d));
     }
}
class Convert
{   public int Convert(int d)
    {   public int a=int.Parse(Console.Read());
        set
        {   if(a>=0)
            {   a=value;   }
            else
            {   Console.Write("you’ve input an error number.");   }
        }
        get
        {   return value;  }
        for(int i=a.Length-1;a[i]>=0;i–)
        {   for(int i=a.Length-1;i>=0;i–)
            {   int e=i*(i-1);   }
        int f=a[i]*e;
        convert d=0;
        d+=f;
        }
        return d;
      } 
}
—————————————————————————————————————

第二题:
Problem Statement

We can substitute each digit of a number with a unique letter from ‘A’ to ‘Z’. This way we can write groups of numbers in a single representation. For example "ABA" can represent any of the following:

101, 151, 343, 767, 929. However, "ABA" cannot mean 555 because each letter must stand for a distinct digit. Furthermore, numbers cannot begin with 0 and thus ‘A’ cannot be replaced by 0. Given two such representations num1 and num2 and the result of their summation return the total number of possible combinations of numbers for which the equation holds. If no combinations are possible then return 0.

Definition

Class:SecretSum
Method:countPossible
Parameters:String, String, String
Returns:int
Method signature:int countPossible(String num1, String num2, String result)
(be sure your method is public)

Constraints- num1, num2, and result will each contain exactly 3 uppercase letters (‘A’ – ‘Z’).

Examples
0) "AAA"
    "BBB"
    "CCC"
    Returns: 32
1) "ABB"
    "DEE"
    "TTT"
    Returns: 112
2) "ABC"
    "ABA"
    "ACC"
    Returns: 0
    Leading zeroes are not allowed.
3) "AAA"
    "CDD"
    "BAA"
    Returns: 32
4) "TEF"
    "FET"
    "AAA"
    Returns: 12
5) "ABC"
    "ABC"
    "BCE"
    Returns: 5
    We can have the following 5 sums:
    124 + 124 = 248
    125 + 125 = 250
    249 + 249 = 498
    374 + 374 = 748
    375 + 375 = 750
6) "AAA"
    "AAA"
    "BBB"
    Returns: 4
    We can have the following 4 sums:
   111 + 111 = 222
   222 + 222 = 444
   333 + 333 = 666
   444 + 444 = 888

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

======================================================

第三题:
Problem Statement
A rational number is defined as a/b, where a and b are integers, and b is greater than 0. Furthermore, a rational number can be written as a decimal that has a group of digits that repeat indefinitely. A common method of writing groups of repeating digits is to place them inside parentheses like 2.85(23) = 2.852323 … 23…
Given a decimal representation of a rational number in decimalNumber, convert it to a fraction formatted as "numerator/denominator", where both numerator and denominator are integers. The fraction must be reduced. In other words, the denominator must be as small as possible,
but greater than zero.
Definition
Class:RecurringNumbers
Method:convertToFraction
Parameters:string
Returns:string
Method signature:string convertToFraction(string decimalNumber)
(be sure your method is public)

Constraints – decimalNumber will have between 3 and 10 characters inclusive.
                – decimalNumber will contain only characters ’0′ – ’9′, ‘.’, ‘(‘ and ‘)’.
                – The second character in decimalNumber will always be ‘.’.
                – There will be at most one ‘(‘ and ‘)’ in decimalNumber. 
                - ‘(‘ in decimalNumber will be followed by one or more digits (’0′ – ’9′), followed by’)’.
                – ‘)’ in decimalNumber will not be followed by any other character.

Examples

0) "0.(3)"
    Returns: "1/3"
    0.(3) = 0.333… = 1/3
1) "1.3125"
    Returns: "21/16"
    Note there are no recurring digits here, although we could write it as 1.3125(0) or 1.3124(9).
2) "2.85(23)"
    Returns: "14119/4950"
    2.85(23) = 2.852323… = 285/100 + 23/9900 = 28238/9900 = 14119/4950.
    Make sure to reduce the fraction, as shown in the final step.
3) "9.123(456)"
    Returns: "3038111/333000"
4) "0.111(1)"
    Returns: "1/9"
5) "3.(000)"
    Returns: "3/1"

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

Google™ Code Jam – 中国编程挑战赛 

English Version
 

你有用技术改变世界的梦想吗?你有挑战难度的决心吗?你想和国内计算机精英一决高下吗?全球编程界知名的“Google编程挑战赛Code Jam”即将登陆中国。这项比赛每年都是全球计算机界的一次盛事。今年 Google 首次专门为中国举办这项比赛,旨在弘扬计算机科学的艺术,推进中国计算机编程教育,鼓励并嘉奖中国顶级编程人才。

竞赛的题目具有相当的挑战性,竞赛奖品也非常丰厚。 有志之士可借此机会一展才能,成为脱颖而出的中国最佳。

这里有极富挑战性的题目,高科技的奖品,以及令人赞叹的荣耀,你还在等什么?

赛事运作细则

此次挑战赛以计时赛的形式举行,所有的参赛者都将在限定的时间内在线完成相同的竞赛题目。

参赛者在竞赛过程当中,可以选用以下4种编程语言的一种-Java,C++,C#和VB。

熟悉C语言但是不熟C++?没有关系!我们能帮您解决这个问题。请参看针对C语言程序员的C++。这个网页简单介绍了如何针对这次比赛学习C++。此外,请点击上面的竞技场链接,在训练室中花一些时间熟悉环境。

以下是竞赛相关过程说明:

下载竞赛平台
参赛者将从TopCoder®公司的竞赛平台(一个Java程序)开始踏入竞赛的第一步。下载平台程序,仔细阅读竞赛题目,然后将解决方案编写成代码形式,编译并测试方案,最后提交方案代码,得到相应的分数。参赛者可以在正式竞赛前下载竞赛平台,通过提供的样例来体验和熟悉平台的操作环境。

编码阶段
在指定的日期和时间,参赛者进入竞赛平台,以每10人一组被安排进入相应的虚拟房间。所有参赛者都将获得相同的3道竞赛题,3题的难度递增。此阶段竞赛中,竞赛者须在最短时间内完成题目,提供正确的方案代码,代码提交得越早,竞赛者得到的分数越高。在竞赛的整个过程中,排名榜会显示竞赛者的累计分数。

挑战阶段
在挑战阶段,参赛者不但可以看到其他参赛者提交的方案代码,还可以给出测试数据,使其他参赛者提交的程序得到错误的运算结果,从而推翻其他参赛者所提交的方案。这种方式,对于编程人员来说是最直接的竞赛形式。在这个阶段,参赛者的测试数据若能成功推翻他人提交的代码则可得分;反之,将被扣分。

系统测试
在系统测试阶段,系统会自动对每个提交方案代码进行测试,确定其正确程度和可行性,并以此给出参赛者相应的分数。整个评测过程耗时很短,参赛者当场可以知道自己的比赛结果。

参赛须知

报名注册时间:从北京时间2005年11月21日星期一上午9时开始,至北京时间2005年12月12日上午9时结束。报名注册没有人数限制,但是只有通过资格赛的前500名(12月12日举行)可以晋级此次正式比赛的第一轮。第一轮比赛将在12月19日举行。

首轮名次前250名将于12月22日晋级第二轮,第二轮的前50名则将参加在中国举行的冠军赛,争夺总数达25万元的高额奖品。

加关注

Get every new post delivered to your Inbox.