7-1
import java.util.Scanner;
public class S7_01 {
static int MOD = 2009; // 2009 = 49 * 41 所以到了41后面都 %尽 所以直接输出0
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
if (n >= 41) {
System.out.println(0);
continue;
}
int ans = 1;
for (int i = 1; i <= n; i++) {
ans = (ans * i) % MOD;
}
System.out.println(ans);
}
}
}
7-2
import java.util.Scanner;
public class S7_02 {
static String[] names = new String[1000];
static int i; //记录数量
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
String str = sc.nextLine();
if (str.equals(".")) {
break;
}
names[i++] = str;
}
if (i >= 14) {
System.out.println(names[1] + " and " + names[13] + " are inviting you to dinner...");
} else if (i >= 2 && i < 14) {
System.out.println(names[1] + " is the only one for you...");
} else
System.out.println("Momo... No one is for you ...");
}
}
7-3
Dfs + 剪枝:
一个数 = P1^a1 * P2^a2 * P3^a3········ P1,P2,P3都是质因子
那么约数个数 = (a1 + 1) * (a2 + 1) * (a3 + 1)
约数最多的数肯定能被2整除,所以直接从2开始枚举,而且上一个质因数的个数肯定比下一个多,所以下一个质因数的个数最多是上一个质因数的个数。
import java.util.Scanner;
public class S7_03 {
static int num,maxCnt;
static long ans,n;
static int[] prime = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
num = sc.nextInt();
while (num-- > 0) {
n = sc.nextLong();
maxCnt = 0;
dfs(0, 1, 1,15);
System.out.println(ans);
}
}
static void dfs(int u, long size, int cnt, int end) {
if (cnt > maxCnt) {
ans = size;
maxCnt = cnt;
} else if (cnt == maxCnt && size < ans) {
ans = size;
}
if (u == 15) return; //可行性 只能15个质因数
for (int i = 1; i < end; i++) { //这里i = 1从1开始 也可以剪去很多 因为我们要找因数最多的,那最多的话 必须从最小就拿起
size = size * prime[u];
if (size > n) return;
dfs(u + 1, size, cnt*(i + 1), i + 1); //因为质因子 肯定是递减的为最小的
}
}
}
7-4
import java.util.Scanner;
public class S7_04 {
static int n;
static long[] rec = new long[77];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
rec[0] = rec[1] = 1;
rec[2] = 2;
for (int i = 3; i < 77; i++) {
rec[i] = rec[i - 1] + rec[i - 2] + rec[i - 3];
}
System.out.println(rec[n]);
}
}
7-5
import java.util.Scanner;
public class S7_05 {
static int n;
static char[] help = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
n = sc.nextInt();
if (n == 0) {
break;
}
f(n);
}
}
static void f(int num) {
if (num == 1) {
System.out.print(help[num]);
return;
}
f(num - 1);
System.out.print(help[num]);
f(num - 1);
}
}
7-6
import java.util.Scanner;
public class S7_06 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while (n-- > 0) {
long a = sc.nextLong();
long b = sc.nextLong();
System.out.println(a + b + gcd(a,b));
}
}
static long gcd(long a,long b){
if (a == 0) return b;
return gcd(b%a,a);
}
}
7-7
import java.util.Scanner;
public class S7_07 {
static int n, ans = 1;
static int[] rec = new int[1000];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for (int i = 0; i < n; i++) {
rec[i] = sc.nextInt();
}
int left = 0;
for (int i = 1; i < n; i++) {
if (rec[i] < rec[i - 1]) {
ans = Math.max(i - left, ans); // i - left 是子序列的长度,和之前的ans比较一下
left = i; //重新开始
}
}
System.out.println(ans);
}
}
7-8
#include <iostream>
#include <algorithm>
using namespace std;
int arr[1000000];
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
scanf("%d",&arr[i]);
}
sort(arr, arr + n);
cout << arr[n - 1];
for (int i = n - 2; i >= n - m && i >= 0; i--) {
cout << " " << arr[i];
}
return 0;
}
7-9:Floyd的水题,求个多源最短路。
import java.util.Scanner;
public class S7_09 {
static int N = 305;
static int[][] G = new int[N][N];
static void init() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
G[i][j] = 0x3f3f3f3f; //初始化 将无边设置为无穷大
}
}
}
static int n, m, T;
public static void main(String[] args) {
init();
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
T = sc.nextInt();
int u, v, w;
for (int i = 1; i <= m; i++) {
u = sc.nextInt();
v = sc.nextInt();
w = sc.nextInt();
G[u][v] = w;
}
f();
while (T-- > 0) {
u = sc.nextInt();
v = sc.nextInt();
System.out.println(G[u][v] == 0x3f3f3f3f ? -1 : G[u][v]);
}
}
static void f() {
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
G[i][j] = Math.min(G[i][j],Math.max(G[i][k], G[k][j])); //i-k k-i 代表中间的那些边
//输出最小的 最大高度 在这些路径中找出最大的一个 然后比较出最小的那个 来代替“最短路的值”
}
}
}
}
}
7-10
import java.util.Scanner;
public class S7_10 {
static long ans;
static int a, n;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
a = sc.nextInt();
n = sc.nextInt();
long tem = 0;
for (int i = 1; i <= n; i++) {
ans += tem * 10 + a;
tem = tem * 10 + a;
}
System.out.println("s = " + ans);
}
}