learn java in 21 minutes for C++ Programmers

先放资料:

Learning a New Programming Language: Java for C++ Programmers

 

java package

先说几条重要的人话:

  • 一个java文件第一行可以声明该文件所属于的package,package的名字必须与整个工作目录的路径名相同。
  • 同一个package下的class默认有互相访问的权限。
  • 访问属性设置为public的class,如果该class所在的file声明了package,那么可以被其他package下的class访问到。
  • .java的文件名必须与文件中设置为public的class名保持一致(如果没有public的类,那么名称任意)
  • Every class is part of some package.
  • All classes in a file are part of the same package.
  • You can specify the package using a package declaration:
      • package

    name

       ;

    as the first (non-comment) line in the file.

  • Multiple files can specify the same package name.
  • If no package is specified, the classes in the file go into a special unnamed package (the same unnamed package for all files).
  • If package name is specified, the file must be in a subdirectory called name (i.e., the directory name must match the package name).
  • You can access public classes in another (named) package using:package-name.class-nameYou can access the public fields and methods of such classes using:package-name.class-name.field-or-method-nameYou can avoid having to include the package-name using:
      • import

    package-name

      .*;

    or

      • import

    package-name.class-name

      ;

    at the beginning of the file (after the package declaration). The former imports all of the classes in the package, and the second imports just the named class. You must still use:class-nameto access the classes in the packages, andclass-name.field-or-method-nameto access the fields and methods of the class; the only thing you can leave off is the package name.

下面是一些例子:

Assume that you are working in a directory called Javadir, and that you create four files, whose contents are shown below.

Here are the directories and file names you must use:

  • File 1 must be in a subdirectory named ListPkg, in a file named List.java.
  • File 2 must also be in the ListPkg subdirectory, in a file named NoNextItemException.java.
  • File 3 must be in a file named Test.java (in the Javadir directory).
  • File 4 can be in any .java file (in the Javadir directory).

And here are the classes that can be accessed by the code in each file:

  • Files 1 and 2:
    • The code in the first two files (ListPkg/List.java and ListPkg/NoNextItemException.java) can access the classes defined in the same package (List, ListNode, and NoNextItemException). (No access was specified for those classes, so they get the default, package access.)
    • The code in files 1 and 2 cannot access class Test, even though it is a public class. The problem is that Test is in an unnamed package, so the code in the ListPkg package has no way to import that package, or to name class Test.
    • The code in files 1 and 2 cannot access classes Utils and Test2, because they have default (package) access, and are in a different package.
  • Files 3 and 4:
    • The code in file 3 (Test.java) can access classes ListPkg.List, ListPkg.NoNextItemException, Test, Utils, and Test2 (the first two because they are public classes in a named package, and the last three because they are in the same, unnamed package, and have either public or package access). Note however, that if the code in Test.java uses the class Test2, and that class is not in a file called Test2.java, then the file that contains class Test2 must be compiled first, or else the class will not be found.
    • The code in file 4 (the file that contains class Test2) can access the same classes as the code in file 3 (Test.java).

[dp专题000]uva 10328 Coin Toss (java 大数+dp)(Unsolved)

题目链接

题意:问长度为n,每个位置由且仅有‘H’和’T’组成的序列中,至少有连续k个‘H’出现的方案数。

思路:不会做,参考了题解 不过没有完全搞懂。

根据题解,正面考虑比较麻烦,所以反面考虑。[j]

dp[i][j]表示长度为i,前面最后连续的‘H’的个数不超过j个的方案数。

考虑转移方程为:

总的情况为:dp[i][j] = dp[i-1][j] * 2;

但是其中有多考虑的情况,就是第i位是’H’,且i位之前的最后j个位置都是’H’(即从i-j位到第i-1位都是‘H’,此时第i-j-1位必然是’T’)

有i个硬币时,如果i < j+1,那么它对于i-1的情况来说,最后一个硬币的位置就可以随便放了。

如果i > j + 1,dp[ i ] [ j ]  = dp [ i – 1 ] [ j ] * 2 – x(不满足条件的部分)

然后我们来考虑这个x怎么求,既然是不满足条件,那么肯定是第i的位置放了H,前面的都是H,从而这些连续的H大于j。那么就考虑dp[ i – 1 – j – 1 ](中间这 j – 1 个(kk:疑似作者笔误。应该位j个)全为H,加上第i个H,就不满足条件了),所以:

dp [ i ] [ j ] = dp [ i – 1 ] [ j ] * 2 – dp [ i – j – 2 ] [ j ](kk:仍然不是很懂这个式子…)

那么我们就差最后一个了:i == j + 1的情况了。(因为 i – j – 2 就等于 -1 了,用递推公式会RE的)

这种情况只有一种,即是全是H。

那么它就为:dp [ i ] [ j ] = dp [ i – 1 ] [ j ] * 2 – 1

 

 

 

最后,要用到高精度,干脆写了java