1.int型变String型
String s=String.valueOf(i);
A题:sum必须要用long型,如果用int会溢出,打印出的是-1636549443
B题:由与题目的要求我们只需求得最后四位,所以模10000即可 不模的话很快就会溢出
3.类的使用
class shop
{
int time[];
public shop (int t)
{
time=new int [t+1];
}
}
//.....
for(int i=0;i<n;i++) {
s[i]=new shop(t);
}
for(int i=0;i<m;i++)
{
temp=in.nextInt();
sh=in.nextInt();
s[sh-1].time[temp]++;
}
3.读取一行字符
for (int i = 0; i < n; i++) {
String line = in.nextLine();
mapp[i] = line.toCharArray();
}
4.定义比较长的变量,用long或者double,再不行就用BigInteger
5.计算程序运行时间
long startTime = System.currentTimeMillis(); //获取开始时间
...
long endTime = System.currentTimeMillis(); //获取结束时间
System.out.println("程序运行时间:" + (endTime - startTime) + "ms"); //输出程序运行时间
6.dfs 当前有 /当前没有
package 题目;
import java.util.ArrayList;
import java.util.List;
public class _求幂集 {
public static void main(String[] args) {
List<List<Integer>> list=new ArrayList<List<Integer>>(); //注意这个初始化
int a[]=new int[]{1,2,3};
dfs(list,new ArrayList(),a,0);
System.out.print(list);
}
public static void dfs(List<List<Integer>> res,List<Integer> now,int a[],int i)
{
if(i>=a.length)
{
res.add(new ArrayList(now));
return;
}
now.add(a[i]);
dfs( res , now,a, i+1);
now.remove(now.size()-1);
dfs(res,now,a,i+1);
}
}
7 . switch +dfs
static boolean dfs(int x, int y) {
if (x < 0 || x >= 10 || y < 0 || y >= 10)
return true;
if (vis[x][y])
return false;
vis[x][y] = true;
switch (mp[x][y]) {
case 'L':
return dfs(x, y - 1);
case 'R':
return dfs(x, y + 1);
case 'U':
return dfs(x - 1, y);
case 'D':
return dfs(x + 1, y);
}
return false;
}