数论总结

地产大亨Q先生临终的遗愿是:拿出100万元给X社区的居民抽奖,以稍慰藉心中愧疚。
麻烦的是,他有个很奇怪的要求:
1. 100万元必须被正好分成若干份(不能剩余)。
  每份必须是7的若干次方元。
  比如:1元, 7元,49元,343元,...
  
2. 相同金额的份数不能超过5份。

3. 在满足上述要求的情况下,分成的份数越多越好!

请你帮忙计算一下,最多可以分为多少份?
package 训练;

public class 奇怪的捐赠
{

	public static void main(String[] args)
	{
		int number=1000000;
		String string=""+Integer.toString(number, 7);
		System.out.println(string);
		
	}

}

天平称重


用天平称重时,我们希望用尽可能少的砝码组合称出尽可能多的重量。
如果只有5个砝码,重量分别是1,3,9,27,81
则它们可以组合称出1到121之间任意整数重量(砝码允许放在左右两个盘中)。

本题目要求编程实现:对用户给定的重量,给出砝码组合方案。
例如:
用户输入:
5
程序输出:
9-3-1
用户输入:
19
程序输出:
27-9+1

要求程序输出的组合总是大数在前小数在后。
可以假设用户的输入的数字符合范围1~121。
package 数论;

import java.util.Scanner;

public class 天平称重
{
	static int n;
	private static Scanner scanner;
	public static void main(String[] args)
	{
		scanner = new Scanner(System.in);
		n=scanner.nextInt();
		int x[]={1,3,9,27,81};
		f(x,0,0,"");//dfs搜索做法
		
		f2(19);//进制做法 把2变-1
	}

	private static void f2(int i)
	{
		int a=i%3;
		if (i==0)
		{
			return;
		}
		if (a==2)
		{
			f2((i+1)/3);
			a=-1;
		}
		else {
			f2(i/3);
		}
		System.out.print(a);
	}

	private static void f(int[] x, int i,int count,String string)
	{
		if (i==x.length)
		{
			if (count==n)
			{
				if (string.charAt(0)=='+')
				{
					string=string.substring(1);
				}
				System.out.println(string);
			}
			return;
		}
		
		for (int j = -1; j <=1; j++)
		{
			
			if (j==-1)
			{
				f(x, i+1, count+j*x[i], new String(j*x[i]+string));
			}
			if (j==0)
			{
				f(x, i+1, count+j*x[i], new String(string));
			}
			if (j==1)
			{
				f(x, i+1, count+j*x[i], new String("+"+(j*x[i])+string));
			}
		}
	}
}

有理数表示


如果求 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + .... + 1/100 = ?

要求绝对精确,不能有误差。
package 训练;

import java.math.BigInteger;

public class 有理数
{

	public static void main(String[] args)
	{
		Rati rati=new Rati("1", "2");
		for (int i = 3; i <=10; i++)
		{
			rati=rati.add(new Rati("1", ""+i));
			System.out.println(rati);
		}
	}

}
class Rati
{
	BigInteger fenzi;
	BigInteger fenmu;
	public Rati(String string1, String string2)
	{
		super();
		this.fenzi = new BigInteger(string1);
		this.fenmu = new BigInteger(string2);
	}
	@Override
	public String toString()
	{
		return "Rati [fenzi=" + fenzi + ", fenmu=" + fenmu + "]";
	}
	public Rati add(Rati rati)//A/B+C/D=(AD+BC)/BD
	{
		BigInteger fenzi=this.fenzi.multiply(rati.fenmu).add(this.fenmu.multiply(rati.fenzi));
		BigInteger fenmu=this.fenmu.multiply(rati.fenmu);
		BigInteger gcd=fenzi.gcd(fenmu);
		return new Rati(fenzi.divide(gcd).toString(), fenmu.divide(gcd).toString());
		
	}
}

素数


第1个素数是2,第2个素数是3,...
求第100002(十万零二)个素数

筛选法与暴力法 在 100002的数量级 感觉两组方法时间都差不多
但是暴力法明显更快嘛

所以在如果是填空题就暴力咯
如果是编程题可能会有很大的数量级 所以就筛选

package 训练;

public class 筛选法求素数
{
	//求第100002(十万零二)个素数
	public static void main(String[] args)
	{
		int number=10000*1000;
		byte []bs=new byte[number];
		int x=100002;
		for (int i = 2; i < number/2; i++)
		{
			if (bs[i]==1)
			{
				continue;
			}
			for (int j = 2; j*i <= number; j++)
			{
				if (i*j<number)
				{
					bs[i*j]=1;
				}
			}
		}
		int count=0;
		for (int i = 2; i < number; i++)
		{
			if (bs[i]!=1)
			{
				count++;
				if (count==x)
				{
					System.out.println(i);
				}
			}
			
		}
		
		f();//暴力
	}

	private static void f()
	{
		int number=10000*1000;
		int count=0;
		int x=100002;
		for (int i = 2; i <=number; i++)
		{
			if (is(i))
			{
				count++;
				if (count==x)
				{
					System.out.println(i);
				}
			}
		}
		
	}

	private static boolean is(int i)
	{
		for (int j = 2; j <= Math.sqrt(i); j++)
		{
			if (i%j==0)
			{
				return false;
			}
		}
		return true;
	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值