1209. 带分数
import java.io.*;
public class Main
{
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
static final int N = 10;
static int n, cnt;
static int[] a = new int[N];
static boolean[] st = new boolean[N];
public static int calc(int l, int r)
{
int res = 0;
for (int i = l; i <= r; i ++ )
res = res * 10 + a[i];
return res;
}
public static void dfs(int u)
{
if (u > 9)
{
for (int i = 1; i <= 7; i ++ )
for (int j = i + 1; j <= 8; j ++ )
{
int a = calc(1, i), b = calc(i + 1, j), c = calc(j + 1, 9);
if (a * c + b == c * n) cnt ++ ;
}
return;
}
for (int i = 1; i <= 9; i ++ )
if (!st[i])
{
a[u] = i;
st[i] = true;
dfs(u + 1);
a[u] = 0;
st[i] = false;
}
}
public static void main(String[] args) throws IOException
{
n = Integer.parseInt(br.readLine());
dfs(1);
pw.print(cnt);
pw.flush();
pw.close();
}
}
717. 简单斐波那契
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int f1 = 0;
int f2 = 1;
for(int i=1;i<=n;i++) {
int f3 = f1 + f2;
System.out.print(f1+" ");
f1 = f2;
f2 = f3;
}
}
}
P1255 数楼梯
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
static BigInteger f1 = new BigInteger("1");
static BigInteger f2 = new BigInteger("2");
static BigInteger f3 = new BigInteger("0");
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
//1 2 3 5 ...
if (n == 1) {
System.out.println(f1);
} else if (n == 2) {
System.out.println(f2);
} else {
for (int i = 3; i <= n; i++) {
f3 = f1.add(f2);
f1 = f2;
f2 = f3;
}
System.out.println(f3);
}
}
}
P1036 [NOIP2002 普及组] 选数
import java.util.Scanner;
public class Main {
static int n;
static int m;
static final int N = 21;
static int[] a = new int[N];
static int[] b = new int[N];
static int ans;
public static boolean isPrime(int n) {
for(int i=2;i<n;i++) {
if(n%i == 0)
return false;
}
return true;
}
public static void dfs(int u,int start) {
if(u > m) {
int sum = 0;
for(int i=1;i<=m;i++) {
sum+=b[i];
}
if(isPrime(sum)) {
ans++;
}
return ;
}
for(int i=start;i<=n;i++) {
b[u] = a[i];
dfs(u+1,i+1);
b[u] = 0;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
for(int i=1;i<=n;i++) {
a[i] = sc.nextInt();
}
dfs(1,1);
System.out.println(ans);
}
}
P1028 [NOIP2001 普及组] 数的计算
import java.util.Scanner;
public class Main{
//6
public static int dfs(int x) {
if (x == 1) {
return 1;
}
int tot = 1;//long
for (int i = 1; i <= x / 2; i++) {
tot += dfs(i);
}
return tot;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(dfs(n));
}
}
P1464 Function
import java.util.Scanner;
public class Main {
static int N = 21;
static long[][][] s = new long[N][N][N];
public static long w(long a,long b,long c) {
if(a<=0||b<=0||c<=0)
return 1;
if(a>20||b>20||c>20)
return s[20][20][20];
if(a<b&&b<c)
return s[(int)a][(int)b][(int)c-1]+s[(int)a][(int)b-1][(int)c-1]-s[(int)a][(int)b-1][(int)c];
return s[(int)a-1][(int)b][(int)c]+s[(int)a-1][(int)b-1][(int)c]+s[(int)a-1][(int)b][(int)c-1]-s[(int)a-1][(int)b-1][(int)c-1];
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long a,b,c;
for(int i=0;i<=20;i++)
for(int j=0;j<=20;j++)
for(int k=0;k<=20;k++)
s[i][j][k] = w(i,j,k);
// 输入a、b、c,计算w(a, b, c)并输出结果
while (true) {
a = sc.nextLong();
b = sc.nextLong();
c = sc.nextLong();
if (a == -1 && b == -1 && c == -1)
return ;
else
System.out.println("w(" + a + ", " + b + ", " + c + ") = " + w(a, b, c));
}
}
}