方法一:
import java.math.BigInteger;
import java.util.*;
class Main{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
BigInteger s=BigInteger.ONE;
for(int i=1;i<=n;i++)
s=s.multiply(BigInteger.valueOf(i));
System.out.println(s);
scan.close();
}
}
方法二:
import java.util.*;
class Main{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
scan.close();
int[] a=new int[3000];
a[2999]=1;
int p=2999,rem=0;
for(int i=2;i<=n;i++)
{
while(p>0)
{
int sum=a[p]*i+rem;
rem=0;
if(sum>9)
{
a[p]=sum%10;
rem=sum/10;
}
else
{
a[p]=sum;
}
p--;
}
p=2999;
rem=0;
}
int flag=0;
for(int i=0;i<a.length;i++)
{
if(a[i]!=0)
{
flag=i;
break;
}
}
for(int i=flag;i<a.length;i++)
{
System.out.print(a[i]);
}
}
}
import java.util.*;
class Main{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
String s1=scan.nextLine();
String s2=scan.nextLine();
scan.close();
int[] a=new int[102];
int[] b=new int[102];
int[] c=new int[102];
int cnt=0;
for(int i=s1.length()-1;i>=0;i--)
{
a[cnt++]=Integer.parseInt(String.valueOf(s1.charAt(i)));
}
cnt=0;
for(int i=s2.length()-1;i>=0;i--)
{
b[cnt++]=Integer.parseInt(String.valueOf(s2.charAt(i)));
}
int rem=0;
for(int i=0;i<c.length;i++)
{
int sum=a[i]+b[i]+rem;
if(sum>9)
{
c[i]=sum%10;
rem=sum/10;
}
else
{
c[i]=sum;
rem=0;
}
}
int count=0;
for(int i=c.length-1;i>=0;i--)
{
if(c[i]!=0||count==1)
{
System.out.print(c[i]);
count=1;
}
}
}
}
Huffman树
import java.util.*;
class Main{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
Queue<Integer> a=new PriorityQueue<Integer>();
for(int i=0;i<n;i++)
{
a.add(scan.nextInt());
}
scan.close();
int sum = 0;
for(int i=0;i<n-1;i++)
{
int num=a.poll()+a.poll();
a.add(num);
sum+=num;
}
System.out.println(sum);
}
}
import java.util.*;
class Main{
static int[][] x;//二维数组创建棋盘
static int sum=0;//用于记录放法数目
static int n;//棋盘的大小
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
x=new int[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
x[i][j]=sc.nextInt();
}
sc.close();
DFS(0);
System.out.println(sum);
}
public static void DFS(int a)//回溯
{
if(a==2*n)
{sum++;return;}
int wb=0;
if(a<n) wb=2;//白皇后
else wb=3;//黑皇后
for(int i=0;i<n;i++)
{
if(judge(a,i))
{
x[a%n][i]=wb;
DFS(a+1);
x[a%n][i]=1;
}
}
}
public static boolean judge(int a,int i)
{
int bs=0;
if(a<n) bs=2;//白皇后
else bs=3;//黑皇后
if(x[a%n][i]!=1) return false;
for(int j=0;j<a%n;j++)//同一列判断
{
if(x[j][i]==bs)
return false;
}
for(int j=0;j<a%n;j++)//对角线判断
for(int k=0;k<n;k++)
{
if(k!=i&&x[j][k]==bs&&Math.abs(a%n-j)==Math.abs(i-k))
{
return false;
}
}
return true;
}
}
import java.util.*;
class Main{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
int h=scan.nextInt();
int m=scan.nextInt();
String[] a=new String[] {"zero","one","two","three","four","five","six","seven","eight","nine","ten",
"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen",
"nineteen","twenty"};
String[] b=new String[] {"twenty","thirty","forty","fifty"};
if(h<=20) System.out.print(a[h]+" ");
else
{
h=h%20;
System.out.print(a[20]+" "+a[h]+" ");
}
if(m>20)
{
int w=m/10;
int r=m%10;
System.out.print(b[w-2]+" ");
if(r!=0) System.out.print(a[r]+" ");
System.out.print("o`clock");
}
else if(m<=20&&m!=0)
{
System.out.print(a[m]+" ");
System.out.print("o`clock");
}
else
{
System.out.print("o`clock");
}
}
}
import java.util.*;
class Main{
final static int INF=(int) Double.POSITIVE_INFINITY;
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
int m=scan.nextInt();
int n=scan.nextInt();
int[][] a=new int[m][n];
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
a[i][j]=scan.nextInt();
}
scan.close();
int mark=0;//往下
int r=0,c=0;
int sum=0;
while(sum!=m*n)
{
if(mark%4==0)
{
while(r<m&&a[r][c]!=INF)
{
System.out.print(a[r][c]);
sum++;
if(sum!=m*n) System.out.print(" ");
a[r][c]=INF;
r++;
}
r--;mark++;c++;
}
if(mark%4==1)
{
while(c<n&&a[r][c]!=INF)
{
System.out.print(a[r][c]);
sum++;
if(sum!=m*n) System.out.print(" ");
a[r][c]=INF;
c++;
}
r--;mark++;c--;
}
if(mark%4==2)
{
while(r>=0&&a[r][c]!=INF)
{
System.out.print(a[r][c]);
sum++;
if(sum!=m*n) System.out.print(" ");
a[r][c]=INF;
r--;
}
r++;mark++;c--;
}
if(mark%4==3)
{
while(c>=0&&a[r][c]!=INF)
{
System.out.print(a[r][c]);
sum++;
if(sum!=m*n) System.out.print(" ");
a[r][c]=INF;
c--;
}
r++;mark++;c++;
}
}
}
}
import java.util.*;
class Main{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
int v1=scan.nextInt();
int v2=scan.nextInt();
int t=scan.nextInt();
int s=scan.nextInt();
int l=scan.nextInt();
scan.close();
int stu=0,swu=0;
int i=0;
while(stu<l&&swu<l)
{
if(stu-swu>=t)
{
swu+=s*v2;
i+=s;
}
else
{
stu+=v1;
i++;
swu+=v2;
}
if(stu>=l&&swu<l)
System.out.println("R");
else if(stu>=l&&swu>=l)
System.out.println("D");
else if(swu>=l&&stu<l)
System.out.println("T");
}
System.out.println(i);
}
}
import java.util.*;
class Main {
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
int L=scan.nextInt();
String s=scan.next();
int max=0;
String smax="";
Map<String,Integer> map=new HashMap<>();
for(int i=0;i<s.length()-L+1;i++)
for(int j=i+L;j<s.length();j++)
{
String s1=s.substring(i,j);
if(map.containsKey(s1))
{
int value=map.get(s1);
value++;
if(value>max)
{
max=value;
smax=s1;
}
else if(max==value)
{
if(s1.length()>smax.length())
smax=s1;
}
map.put(s1, value);
}
else
map.put(s1,1);
}
System.out.println(smax);
}
}
import java.util.*;
class Main {
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
scan.close();
digui(n);
}
static void digui(int n)
{
String s=Integer.toBinaryString(n);
int j=s.length()-1;//7
boolean flag=true;
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)=='1')
{
if(j>2)
{
for(int k=i+1;k<s.length();k++)
{
if(s.charAt(k)=='0')
{
flag=true;
}
else
{
flag=false;
break;
}
}
if(flag)
{
System.out.print("2(");
digui(j);
System.out.print(")");
}
else
{
System.out.print("2(");
digui(j);
System.out.print(")+");
}
}
else if(j==2)
{
if(s.charAt(s.length()-1)=='1'||s.charAt(s.length()-2)=='1')
System.out.print("2(2)+");
else
System.out.print("2(2)");
}
else if(j==1)
{
if(s.charAt(s.length()-1)=='1')
System.out.print("2+");
else
System.out.print("2");
}
else if(j==0)
{
System.out.print("2(0)");
}
}
j--;
}
}
}