1、Fibonacci数列
资源限制:时间限制:1.0s 内存限制:256.0MB
问题描述:Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1。当n比较大时,Fn也非常大,现在我们想知道,Fn除以10007的余数是多少。
输入格式:输入包含一个整数n。
输出格式:输出一行,包含一个整数,表示Fn除以10007的余数。
说明:在本题中,答案是要求Fn除以10007的余数,因此我们只要能算出这个余数即可,而不需要先计算出Fn的准确值,再将计算的结果除以10007取余数,直接计算余数往往比先算出原数再取余简单。
样例输入
10
样例输出
55
样例输入
22
样例输出
7704
数据规模与约定
1 <= n <= 1,000,000。
(1)开始代码写这样,然后提交完,结果是运行超时。
import java.util.Scanner;
public class Fibonacci{
public static int Fibonacci(int n) {
if(n == 1 || n == 2) {
return 1;
}
else {
return Fibonacci(n-1) + Fibonacci(n-2);
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if(n >= 1 && n <= 1000000) {
System.out.println(Fibonacci(n) % 10007);
}
}
}
(2)后来仔细一看题的要求,发现人家说不需要求出准确值,所以改动一番,代码如下:
import java.util.Scanner;
public class Fibonacci {
public static int Fibonacci(int n) {
int[] f = new int[1000001];
f[1] = 1;
f[2] = 1;
for(int i = 3; i <= n; i++) {
f[i] = (f[i - 1]+f[i - 2]) % 10007;
}
return f[n];
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if(n >= 1 && n <= 1000000) {
System.out.println(Fibonacci(n));
}
}
}
2、查找整数
资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
给出一个包含n个整数的数列,问整数a在数列中的第一次出现是第几个。
输入格式
第一行包含一个整数n。
第二行包含n个非负整数,为给定的数列,数列中的每个数都不大于10000。
第三行包含一个整数a,为待查找的数。
输出格式
如果a在数列中出现了,输出它第一次出现的位置(位置从1开始编号),否则输出-1。
样例输入
6
1 9 4 8 3 9
9
样例输出
2
数据规模与约定
1 <= n <= 1000。
import java.util.Scanner;
public class FindNum{
public static int FindNum(int [] num, int number) {
for(int i = 0; i < num.length ; i++) {
if(number == num[i]) {
return i+1;
}
}
return -1;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
if (n >= 1 && n <= 1000) {
int[] num = new int[n];
for (int i = 0; i < n; i++) {
int a = input.nextInt();
if (a <= 10000) {
num[i] = a;
}
}
int number = input.nextInt();
System.out.println(FindNum(num, number));
}
}
}
3、杨辉三角
资源限制:时间限制:1.0s 内存限制:256.0MB
问题描述:杨辉三角形又称Pascal三角形,它的第i+1行是(a+b)i的展开式的系数。
它的一个重要性质是:三角形中的每个数字等于它两肩上的数字相加。下面给出了杨辉三角形的前4行:
1
1 1
1 2 1
1 3 3 1
给出n,输出它的前n行。
输入格式:输入包含一个数n。
输出格式:输出杨辉三角形的前n行。每一行从这一行的第一个数开始依次输出,中间使用一个空格分隔。请不要在前面输出多余的空格。
样例输入
4
样例输出
1
1 1
1 2 1
1 3 3 1
数据规模与约定
1 <= n <= 34。
import java.util.Scanner;
/**
* Yang Hui triangle is also called Pascal triangle. Its i + 1 line is the
* coefficient of the expansion of (a + b)^i. One of its important properties is
* that each number in a triangle is equal to the sum of the numbers on its two
* shoulders.
* @author XW
*/
public class PascalTriangle{
/**
*
* @param n
* @return int[][]pt
*/
public static int[][] PT(int n) {
int[][] pt = new int[n][n + 1];
for (int i = 0; i < pt.length; i++) {
for (int j = 0; j < pt[i].length; j++) {
if (j == 1 && i == 0) {
pt[i][j] = 1;
} else
pt[i][j] = 0;
}
}
for (int i = 1; i < pt.length; i++) {
for (int k = 1; k < i + 2; k++) {
//core
pt[i][k] = pt[i - 1][k - 1] + pt[i - 1][k];
}
}
return pt;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
if (n >= 1 && n <= 34) {
int[][] pt = PT(n);
for (int i = 0; i < pt.length; i++) {
for (int j = 1; j < i + 2; j++) {
System.out.print(pt[i][j] + " ");
}
System.out.println();
}
}
}
}
4、数列排序
资源限制:时间限制:1.0s 内存限制:512.0MB
问题描述:给定一个长度为n的数列,将这个数列按从小到大的顺序排列。1<=n<=200
输入格式:第一行为一个整数n。第二行包含n个整数,为待排序的数,每个整数的绝对值小于10000。
输出格式:输出一行,按从小到大的顺序输出排序后的数列。
样例输入
5
8 3 6 4 9
样例输出
3 4 6 8 9
这里用插入排序,其实可以用其他排序方法。
import java.util.*;
public class SequenceSort{
public static void insertSort(int[]a) {
for(int i = 1 ; i < a.length ; i++) {
int key = a[i];
int j = i - 1;
while(j > - 1 && a[j] > key) {
a[j + 1] = a[j];
j = j - 1;
}
a[j + 1] = key;
}
}
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int []num = new int[n];
for(int i = 0; i < n; i++) {
num[i] = input.nextInt();
}
insertSort(num);
for(int i = 0 ; i < num.length ; i++) {
System.out.print(num[i]
+" ");
}
}
}
5、特殊的数字
资源限制
时间限制:1.0s 内存限制:512.0MB
问题描述
153是一个非常特殊的数,它等于它的每位数字的立方和,即153=1*1*1+5*5*5+3*3*3。编程求所有满足这种条件的三位十进制数。
输出格式
按从小到大的顺序输出满足条件的三位十进制数,每个数占一行。
/**
* 153 is a very special number, which is equal to the cubic sum of each number,
* that is, 153 = 1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3. Programming to find all
* three decimal numbers that meet this condition.
*
* @author XW
*
*/
public class SpecialNumber {
static int number1 = 100;
static int number2 = 999;
public static void main(String[] args) {
int []n = new int[number2 - number1 + 1];
for(int i = 0; i < n.length; i++) {
n[i] = i + 100;
}
for(int i = 0; i < n.length ; i++) {
int num1 = n[i] % 10;
int num2 = n[i] / 10 % 10;
int num3 = n[i] / 100;
int sum = (int) (Math.pow(num1, 3) + Math.pow(num2, 3) + Math.pow(num3, 3));
if(sum == n[i]) {
System.out.println(n[i]);
}
}
}
}
6、回文数
资源限制:时间限制:1.0s 内存限制:512.0MB
问题描述:1221是一个非常特殊的数,它从左边读和从右边读是一样的,编程求所有这样的四位十进制数。
输出格式:按从小到大的顺序输出满足条件的四位十进制数。
/**
* Problem Description: 1221 is a very special number. Reading from the left is
* the same as reading from the right. Program to find all such four digit
* decimal numbers.
*
* Output format: output the qualified four digit decimal number in the order
* from small to large.
*
* @author XW
*
*/
public class Palindrome {
public static void main(String[] args) {
int[] n = new int[9000];
for (int i = 0; i < n.length; i++) {
n[i] = 1000 + i;
}
for (int i = 0; i < n.length; i++) {
int num1 = n[i] % 10;
int num4 = n[i] / 1000;
int num2 = n[i] / 10 % 10;
int num3 = n[i] / 100 % 10;
if (num1 == num4 && num2 == num3) {
System.out.println(n[i]);
}
}
}
}