OJ1994题、OJ1995题、OJ1996题、OJ1998题、OJ1999题Java实现

Problem Description
为自行解决学费,chx勤工俭学收入10000元以1年定期存入银行,年利率为3.7% 。利率
按年计算,表示100元存1年的利息为3.7元.实际上有时提前有时推迟取,因此实际利息按天
计算,1年按365天计算,因此Q天的利息是
本金*3.7/100 *Q/365
存了100天后1年定期年利息提高到3.9%。如将存款提前全取出,再存1年定期。那么前面的
100天只能按活期利息1.7%计算。
100天的利息和本金:10000(1+1.7/100*100/365)=10046.6 
再存1年定期 :10046.6(1+3.9/100)=10438.4 
得到的利息加本金为10438.4 
如果无视利息的提高,再存1年。得到的利息加本金为(定期推迟取,利率不变) 
10000(1+3.7/100*(100+365)/365)=10471.4

Input
输入数据有多组,第1行为整数T,是数据的组数.每组占一行5个数,Y-存入的本金<=100000,
Q-已存天数<=365,e-活期利率,f-定期利率,g-提高后的定期利率.

Output
每组数据输出2行.
第1行,提前支取后再存1年所得本金和利息.
第2行,继续存1年,Q+365天后所得本金和利息.

Sample Input
   
   
4 10000 100 2.3 3.7 3.9 10000 100 1.7 3.7 3.9 10000 200 1.7 3.7 3.9 10000 300 1.7 3.7 3.9

Sample Output
   
   
10455.5 10471.4 10438.4 10471.4 10486.8 10572.7 10535.2 10674.1
<pre name="code" class="java"><span style="font-size:14px;">import java.util.Scanner;


public class Main{
	public static void main(String[] args) {
		int num=0,y=0,q=0,n=0;
		double e=0.0,f=0.0,g=0.0,a=0.0,b=0.0;
		
		Scanner input =new Scanner(System.in);
		n=input.nextInt();		
		while(input.hasNext())
		{
			num++;
			if(num>n)break;
			y=input.nextInt();
			q=input.nextInt();
			e=input.nextDouble();
			f=input.nextDouble();
			g=input.nextDouble();
			a=y*(1+e/100*q/365)*(1+g/100);	
			b=y*(1+f/100*(q+365)/365);				
			System.out.printf("%.1f%n", a);
			System.out.printf("%.1f%n", b);
		}
		input.close();		
	}
}
</span>


 
 
Problem Description
用1,2,...,n表示n个盘子,称为1号盘,2号盘,...。号数大盘子就大。经典的汉诺塔问
题经常作为一个递归的经典例题存在。可能有人并不知道汉诺塔问题的典故。汉诺塔来源于
印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往上按大小
顺序摞着64片黄金圆盘。上帝命令婆罗门把圆盘从下面开始按大小顺序重新摆放在另一根柱
子上。并且规定,在小圆盘上不能放大圆盘,在三根柱子之间一回只能移动一个圆盘。我们
知道最少需要移动2^64-1次.在移动过程中发现,有的圆盘移动次数多,有的少 。 告之盘
子总数和盘号,计算该盘子的移动次数.

Input
包含多组数据,首先输入T,表示有T组数据.每个数据一行,是盘子的数目N(1<=N<=60)和盘
号k(1<=k<=N)。

Output
对于每组数据,输出一个数,到达目标时k号盘需要的最少移动数。

Sample Input
    
    
2 60 1 3 1

Sample Output
    
    
576460752303423488 4
</pre><pre name="code" class="java"><span style="font-size:14px;">import java.util.Scanner;


public class Main {

	public static void main(String[] args) {
		int num=0,times=0,sum=0,ge=0;
		long s=1;
		Scanner input =new Scanner(System.in);
		times=input.nextInt();
		
		while(input.hasNext())
		{
			num++;
			if(num>times)break;
			sum=input.nextInt();
			ge=input.nextInt();
			for (int i=1;i<=sum-ge;i++)
			{
				s=s*2;
			}
			System.out.println(s);
			s=1;
		}
		input.close();
	}

}
</span>

Problem Description
n个盘子的汉诺塔问题的最少移动次数是2^n-1,即在移动过程中会产生2^n个系列。由于
发生错移产生的系列就增加了,这种错误是放错了柱子,并不会把大盘放到小盘上,即各柱
子从下往上的大小仍保持如下关系 :
n=m+p+q
a1>a2>...>am
b1>b2>...>bp
c1>c2>...>cq
计算所有会产生的系列总数.

Input
包含多组数据,首先输入T,表示有T组数据.每个数据一行,是盘子的数
目N<30.

Output
对于每组数据,输出移动过程中所有会产生的系列总数。

Sample Input
     
     
3 1 3 29

Sample Output
     
     
3 27 68630377364883
<span style="font-size:14px;">import java.util.Scanner;


public class Main {

	public static void main(String[] args) {
		int num=0,times=0,k=0;
		long s=1;
		Scanner input=new Scanner(System.in);
		times=input.nextInt();
		
		while(input.hasNext())
		{
			num++;
			if(num>times)break;
			k=input.nextInt();
			for(int i=1;i<=k;i++)
			{
				s=s*3;
			}
			System.out.println(s);
			s=1;
		}
		input.close();

	}

}
</span>
Problem Description
一个 n 阶方阵的元素是1,2,...,n^2,它的每行,每列和2条对角线上元素的和相等,这样
的方阵叫魔方。n为奇数时我们有1种构造方法,叫做“右上方” ,例如下面给出n=3,5,7时
的魔方.
3
8 1 6
3 5 7
4 9 2
5
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
7
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
第1行中间的数总是1,最后1行中间的数是n^2,他的右边是2,从这三个魔方,你可看出“右
上方”是何意。

Input
包含多组数据,首先输入T,表示有T组数据.每组数据1行给出n(3<=n<=19)是奇数。

Output
对于每组数据,输出n阶魔方,每个数占4格,右对齐

Sample Input
     
     
2 3 5

Sample Output
     
     
8 1 6 3 5 7 4 9 2 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
<span style="font-size:14px;">import java.util.Scanner;
/**
 * 思路,一般情况下,x-1,y+1就是下一个数的位置,
 * 遇到能被矩阵阶数整除的,下个数的位置只需要把x+1,y不变
 */

public class Main {

	public static void main(String[] args) {
		int num=0,times=0,x=0,y=0,n=0;
		int[][] arr=new int[100][100];
		Scanner input=new Scanner(System.in);
		times=input.nextInt();
		while(input.hasNext())
		{
			num++;
			if(num>times)break;
			n=x=y=input.nextInt();			
			x=0;y=(y-1)/2;
			arr[x][y]=1;
			for(int i=2;i<=n*n;i++)
			{				
				if((i-1)%n==0)
				{
					x=x+1;
					if(x>=n)
					{
						x=0;
					}
					arr[x][y]=i;
				}
				else
				{
					x=x-1;
					if(x<0)
					{
						x=n-1;
					}
					y=y+1;
					if(y>=n)
					{
						y=0;
					}
					arr[x][y]=i;
				}
			}
			for(int k=0;k<n;k++)	
			{
				for(int m=0;m<n;m++)
				{										
						System.out.printf("%4d",arr[k][m]);				
				}
				System.out.println();			
			}	
		}
		input.close();
	}

}
</span>

Problem Description
s(n)是正整数n的真因子之和,即小于n且整除n的因子和.例如s(12)=1+2+3+4+6=16.如果任何
数m,s(m)都不等于n,则称n为不可摸数.

Input
包含多组数据,首先输入T,表示有T组数据.每组数据1行给出n(2<=n<=1000)是整数。

Output
如果n是不可摸数,输出yes,否则输出no

Sample Input
      
      
3 2 5 8

Sample Output
      
      
yes yes no
<span style="font-size:14px;">import java.util.Scanner;


public class Main {

	public static void main(String[] args) {
		int num=0,times=0,touch=0,k=0,s=0;
		Scanner input =new Scanner(System.in);
		times=input.nextInt();
		while(input.hasNext())
		{
			num++;
			if(num>times)break;
			touch=input.nextInt();
			for(int i=2;i<=500;i++)
			{
				s=0;
				for(int j=1;j<i;j++)
				{
					if (i%j==0)
					{
						s=s+j;
					}
					if(s==touch)
					{
						k=1;
						break;
					}	
				}				
			}	
			if(k==0)
			{
				System.out.println("yes");
			}
			else if(k==1)
			{
				System.out.println("no");
			}
		}
		input.close();
	}

}
</span>
































  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值