四.数学知识的运用
2018-03-28 21:58
288 查看
主要内容:
①并非数学竞赛
②进制问题及其巧妙运用
③整数与整除问题
④欧几里得扩展定理
⑤有理数表示,大数问题
麻烦的是,他有个很奇怪的要求:
100万元必须被正好分成若干份(不能剩余)。每份必须是7的若干次方元。比如:1元, 7元,49元,343元,…
相同金额的份数不能超过5份。
在满足上述要求的情况下,分成的份数越多越好!
【源代码】
【JAVA:于航】
public class A
{
public static void main(String[] args){
// 直接求一个数字的7进制表示
// 如果没有函数,可以自己用长除法取余数的方式
String s = Integer.toString(1000*1000,7);
int sum=0;
for(int i=0; i<s.length(); i++){
sum += s.charAt(i)-'0';
}
System.out.println(s);
System.out.println("result=" + sum);
}
}
用天平称重时,我们希望用尽可能少的砝码组合称出尽可能多的重量。
如果只有5个砝码,重量分别是1,3,9,27,81
则它们可以组合称出1到121之间任意整数重量(砝码允许放在左右两个盘中)。
本题目要求编程实现:对用户给定的重量,给出砝码组合方案。
例如:
用户输入:
5
程序输出:
9-3-1
用户输入:
19
程序输出:
27-9+1
要求程序输出的组合总是大数在前小数在后。
可以假设用户的输入的数字符合范围1~121。
Q:什么时候用大一点的数-小一点的数?
要表示的数>较大的数/2
【源代码】
【JAVA:于航】
// 天平称重,递归解法
public class A
{
// 把s中的符号取反(+变-,-变+)
static String reve(String s){
s = s.replace('-','#');//java中自带的replace方法实现替换符号的操作
s = s.replace('+','-');
s = s.replace('#','+');
return "-" + s;
}
static String f(int x){
int a = 1;
while(a<x) a *= 3;
if(a==x) return "" + a;
if(x<=a/2) return a/3 + "+" + f(x-a/3);
return a + reve(f(a-x));
}
public static void main(String[] args){
for(int i=1; i<100; i++){ System.out.println(i + ": " + f(i));
}
}
}
// 天平称重,进制解法
假如只放在左盘:1 2 4 8 。。。(二进制:0及1表示)
因此,这个问题是三进制的问题:0—》1 1—》1 2—》-1
所以 怎样把一个数转换为三进制? 长除法
3|__19____...........1 3|__19______...........1
3|_6___............0 调整商,将2的表示转换为-1,方便最后的输出> 3|_6______.............0
3|_2__............2 3|_2____..........-1
0 3|_1__..........1
0
public class B {
static String f(int n){
String s = "";
int q = 1; //位权重
while(n>0){
int sh = n/3; //商
if(n%3 == 1) s = "+" + q + s;//向前累加,大数在前,小数在后
if(n%3 == 2){
sh++;
s = "-" + q + s;
}
n = sh;
q *= 3;
}
return s.substring(1);
}
public static void main(String[] args){
for(int i=1; i<100; i++){
System.out.println(i + ": " + f(i));
}
}
}
有3堆硬币,分别是3,4,5
二人轮流取硬币。
每人每次只能从某一堆上取任意数量。
不能弃权。
取到最后一枚硬币的为赢家。
求先取硬币一方有无必胜的招法。
模二的加法(一旦达到二就对二取模)---》数论----》同余理论(常用)
结论:如果和为0,无论怎么拿,都会!=0
如果非0,总有办法=0 方法不唯一
【源代码】
【JAVA:于航】
// 尼姆堆的模2加(异或)解法
/*
10
101
1100
1110
--------
0101
方法:找到最高位的1,看其贡献者,可以改变贡献者自身,使结果为0;
关于异或操作的优秀特性:x^y^y=x
*/
public class A
{
static void f(int[] a){
int sum = 0;
for(int i=0; i<a.length; i++){
sum ^= a[i];
}
if(sum==0){
System.out.println("输了");
return;
}
for(int i=0; i<a.length; i++){
int x = sum ^ a[i];
if(x<a[i]) System.out.println(a[i] + " --> " + x);
}
}
public static void main(String[] args){
int[] a = {2,5,12,14};
f(a);
}
}
如果两个数很大,怎样求最大公约数,最小公倍数?
如果是n个数呢?比如1000个数的最小公倍数
【源代码】
【JAVA:于航】
/*
辗转相除法
欧几里得定理 gcd(A,B) = gcd(B,A%B)
关键是:算数的基本定理------->质因数分解的唯一性
n = p1^n1 * p2^n2 * ...
*/
public class A
{
static int gcd(int a, int b){
if(b==0) return a;
return gcd(b, a%b);
}
static int lcm(int a, int b){
return a * b / gcd(a,b);
}
public static void main(String[] args){
System.out.println(gcd(42,60));
System.out.println(lcm(42,60));
}
}
从昏迷中醒来,小明发现自己被关在X星球的废矿车里。
矿车停在平直的废弃的轨道上。
他的面前是两个按钮,分别写着“F”和“B”。
小明突然记起来,这两个按钮可以控制矿车在轨道上前进和后退。
按F,会前进97米。按B会后退127米。
透过昏暗的灯光,小明看到自己前方1米远正好有个监控探头。
他必须设法使得矿车正好停在摄像头的下方,才有机会争取同伴的援助。
或许,通过多次操作F和B可以办到。
矿车上的动力已经不太足,黄色的警示灯在默默闪烁…
每次进行 F 或 B 操作都会消耗一定的能量。
小明飞快地计算,至少要多少次操作,才能把矿车准确地停在前方1米远的地方。
请填写为了达成目标,最少需要操作的次数。
【源代码】
【JAVA:于航】
/*
解不定方程
97x + 127y = 1
欧几里得定理 ---- 辗转相除法 gcd
扩展欧几里得定理
Ax + By = gcd(A,B)
理论基础: gcd(A,B) == gcd(B,A%B)
求出特解后,通解很好表示
Ax + By = gcd(A,B)
Ax + By = gcd(B,A%B)
B(A/B x + y) + (A%B)x = gcd(B,A%B)
对比:
A/B x + y = 新x
x = 新y
*/
public class A
{
// 返回最大公约数
// xy: 顺便解出的xy
static int e_gcd(int A, int B, int[] xy)
{
if(B==0){
xy[0] = 1;
xy[1] = 0;
return A;
}
int ans = e_gcd(B, A%B, xy);
int t = xy[0];
xy[0] = xy[1];
xy[1] = t - A/B * xy[0];
return ans;
}
public static void main(String[] args)
{
int[] xy = new int[2];
int a = e_gcd(97,127,xy);
System.out.println(a);
System.out.println(xy[0] + " " + xy[1]);
}
}
而四则运算对于有理数是完全封闭的
语言中如果没有有理数类,可以自己写一个:
注意:
浮点数的计算会有舍入误差,导致精度有误差 会导致在判断浮点数相等的问题上有瑕疵
解决误差的方法有:①加大精度 ②尽量保持原貌,保持精度
【问题描述】
如果求 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + …. + 1/100 = ?
要求绝对精确,不能有误差。
A/B+C/D=(A*D+B*C)/B*D
【源代码】(貌似有点问题。。)
import java.math.BigInteger;
//自己写一个有理数类
public class Rati {
private BigInteger zi=BigInteger.ZERO;
private BigInteger mu=BigInteger.ONE;
public Rati(long x){
this(BigInteger.valueOf(x),BigInteger.ONE);
}
public Rati(long x,long y) {
this(BigInteger.valueOf(x),BigInteger.valueOf(y));
}
public Rati(BigInteger x){
this(x,BigInteger.ONE);
}
public Rati(BigInteger x,BigInteger y){
zi=x;
mu=y;
BigInteger g=gcd(zi,mu);
zi=zi.divide(g);
mu=mu.divide(g);
}
static BigInteger gcd(BigInteger a,BigInteger b){
if(b.equals(b.ZERO)) return a;
return gcd(b,a.mod(b));
}
public Rati add(Rati it){
return new Rati(zi.multiply(it.mu).add(mu.multiply(it.z
a746
i)));
}
public Rati mul(Rati it){
return new Rati(zi.multiply(it.zi),mu.multiply(it.mu));
}
public String toString(){
String s=zi.toString();
if(mu.equals(BigInteger.ONE)==false)
s+="/"+mu;
return s;
}
public static void main(String[] args) {
//System.out.println(new Rati(1,3).add(new Rati(1,6)));
}
}
第1个素数是2,第2个素数是3,…
求第100002(十万零二)个素数
【源代码】
【JAVA:于航】
/*
筛到第x个素数,需要数组准备多大?
素数分布定理:不大于n的素数的个数为:n / ln(n)
double t = 100;
while(t / Math.log(t) < x) t *= 1.1;
System.out.println(t);
*/
public class SuShu
{
public static void main(String[] args)
{
int N = 1500 * 1000;
int x = 100002;
byte[] a = new byte
;
for(int i=2; i<N/2; i++)
{
if(a[i]==1) continue; //越过已经找到的合数
for(int k=2; k<=N/i; k++)
{
if(i*k<N) a[i*k] = 1;
}
}
int m = 0;
for(int i=2; i<N; i++)
{
if(a[i]==0)
{
m++;
if(m==x) System.out.print(i + " ");
}
}
System.out.println("m=" + m);
}
}
①并非数学竞赛
②进制问题及其巧妙运用
③整数与整除问题
④欧几里得扩展定理
⑤有理数表示,大数问题
1. 奇怪的捐赠
地产大亨Q先生临终的遗愿是:拿出100万元给X社区的居民抽奖,以稍慰藉心中愧疚。麻烦的是,他有个很奇怪的要求:
100万元必须被正好分成若干份(不能剩余)。每份必须是7的若干次方元。比如:1元, 7元,49元,343元,…
相同金额的份数不能超过5份。
在满足上述要求的情况下,分成的份数越多越好!
【源代码】
【JAVA:于航】
public class A
{
public static void main(String[] args){
// 直接求一个数字的7进制表示
// 如果没有函数,可以自己用长除法取余数的方式
String s = Integer.toString(1000*1000,7);
int sum=0;
for(int i=0; i<s.length(); i++){
sum += s.charAt(i)-'0';
}
System.out.println(s);
System.out.println("result=" + sum);
}
}
2. 天平称重
【问题描述】用天平称重时,我们希望用尽可能少的砝码组合称出尽可能多的重量。
如果只有5个砝码,重量分别是1,3,9,27,81
则它们可以组合称出1到121之间任意整数重量(砝码允许放在左右两个盘中)。
本题目要求编程实现:对用户给定的重量,给出砝码组合方案。
例如:
用户输入:
5
程序输出:
9-3-1
用户输入:
19
程序输出:
27-9+1
要求程序输出的组合总是大数在前小数在后。
可以假设用户的输入的数字符合范围1~121。
Q:什么时候用大一点的数-小一点的数?
要表示的数>较大的数/2
【源代码】
【JAVA:于航】
// 天平称重,递归解法
public class A
{
// 把s中的符号取反(+变-,-变+)
static String reve(String s){
s = s.replace('-','#');//java中自带的replace方法实现替换符号的操作
s = s.replace('+','-');
s = s.replace('#','+');
return "-" + s;
}
static String f(int x){
int a = 1;
while(a<x) a *= 3;
if(a==x) return "" + a;
if(x<=a/2) return a/3 + "+" + f(x-a/3);
return a + reve(f(a-x));
}
public static void main(String[] args){
for(int i=1; i<100; i++){ System.out.println(i + ": " + f(i));
}
}
}
// 天平称重,进制解法
假如只放在左盘:1 2 4 8 。。。(二进制:0及1表示)
因此,这个问题是三进制的问题:0—》1 1—》1 2—》-1
所以 怎样把一个数转换为三进制? 长除法
3|__19____...........1 3|__19______...........1
3|_6___............0 调整商,将2的表示转换为-1,方便最后的输出> 3|_6______.............0
3|_2__............2 3|_2____..........-1
0 3|_1__..........1
0
public class B {
static String f(int n){
String s = "";
int q = 1; //位权重
while(n>0){
int sh = n/3; //商
if(n%3 == 1) s = "+" + q + s;//向前累加,大数在前,小数在后
if(n%3 == 2){
sh++;
s = "-" + q + s;
}
n = sh;
q *= 3;
}
return s.substring(1);
}
public static void main(String[] args){
for(int i=1; i<100; i++){
System.out.println(i + ": " + f(i));
}
}
}
3. 尼姆堆
【问题描述】有3堆硬币,分别是3,4,5
二人轮流取硬币。
每人每次只能从某一堆上取任意数量。
不能弃权。
取到最后一枚硬币的为赢家。
求先取硬币一方有无必胜的招法。
模二的加法(一旦达到二就对二取模)---》数论----》同余理论(常用)
结论:如果和为0,无论怎么拿,都会!=0
如果非0,总有办法=0 方法不唯一
【源代码】
【JAVA:于航】
// 尼姆堆的模2加(异或)解法
/*
10
101
1100
1110
--------
0101
方法:找到最高位的1,看其贡献者,可以改变贡献者自身,使结果为0;
关于异或操作的优秀特性:x^y^y=x
*/
public class A
{
static void f(int[] a){
int sum = 0;
for(int i=0; i<a.length; i++){
sum ^= a[i];
}
if(sum==0){
System.out.println("输了");
return;
}
for(int i=0; i<a.length; i++){
int x = sum ^ a[i];
if(x<a[i]) System.out.println(a[i] + " --> " + x);
}
}
public static void main(String[] args){
int[] a = {2,5,12,14};
f(a);
}
}
4. 公约公倍
【问题描述】如果两个数很大,怎样求最大公约数,最小公倍数?
如果是n个数呢?比如1000个数的最小公倍数
【源代码】
【JAVA:于航】
/*
辗转相除法
欧几里得定理 gcd(A,B) = gcd(B,A%B)
关键是:算数的基本定理------->质因数分解的唯一性
n = p1^n1 * p2^n2 * ...
*/
public class A
{
static int gcd(int a, int b){
if(b==0) return a;
return gcd(b, a%b);
}
static int lcm(int a, int b){
return a * b / gcd(a,b);
}
public static void main(String[] args){
System.out.println(gcd(42,60));
System.out.println(lcm(42,60));
}
}
5. 一步之遥
【问题描述】从昏迷中醒来,小明发现自己被关在X星球的废矿车里。
矿车停在平直的废弃的轨道上。
他的面前是两个按钮,分别写着“F”和“B”。
小明突然记起来,这两个按钮可以控制矿车在轨道上前进和后退。
按F,会前进97米。按B会后退127米。
透过昏暗的灯光,小明看到自己前方1米远正好有个监控探头。
他必须设法使得矿车正好停在摄像头的下方,才有机会争取同伴的援助。
或许,通过多次操作F和B可以办到。
矿车上的动力已经不太足,黄色的警示灯在默默闪烁…
每次进行 F 或 B 操作都会消耗一定的能量。
小明飞快地计算,至少要多少次操作,才能把矿车准确地停在前方1米远的地方。
请填写为了达成目标,最少需要操作的次数。
【源代码】
【JAVA:于航】
/*
解不定方程
97x + 127y = 1
欧几里得定理 ---- 辗转相除法 gcd
扩展欧几里得定理
Ax + By = gcd(A,B)
理论基础: gcd(A,B) == gcd(B,A%B)
求出特解后,通解很好表示
Ax + By = gcd(A,B)
Ax + By = gcd(B,A%B)
B(A/B x + y) + (A%B)x = gcd(B,A%B)
对比:
A/B x + y = 新x
x = 新y
*/
public class A
{
// 返回最大公约数
// xy: 顺便解出的xy
static int e_gcd(int A, int B, int[] xy)
{
if(B==0){
xy[0] = 1;
xy[1] = 0;
return A;
}
int ans = e_gcd(B, A%B, xy);
int t = xy[0];
xy[0] = xy[1];
xy[1] = t - A/B * xy[0];
return ans;
}
public static void main(String[] args)
{
int[] xy = new int[2];
int a = e_gcd(97,127,xy);
System.out.println(a);
System.out.println(xy[0] + " " + xy[1]);
}
}
6. 有理数------大整数运算
如何避免浮点误差? BigDicimal() / BigInteger()而四则运算对于有理数是完全封闭的
语言中如果没有有理数类,可以自己写一个:
注意:
浮点数的计算会有舍入误差,导致精度有误差 会导致在判断浮点数相等的问题上有瑕疵
解决误差的方法有:①加大精度 ②尽量保持原貌,保持精度
【问题描述】
如果求 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + …. + 1/100 = ?
要求绝对精确,不能有误差。
A/B+C/D=(A*D+B*C)/B*D
【源代码】(貌似有点问题。。)
import java.math.BigInteger;
//自己写一个有理数类
public class Rati {
private BigInteger zi=BigInteger.ZERO;
private BigInteger mu=BigInteger.ONE;
public Rati(long x){
this(BigInteger.valueOf(x),BigInteger.ONE);
}
public Rati(long x,long y) {
this(BigInteger.valueOf(x),BigInteger.valueOf(y));
}
public Rati(BigInteger x){
this(x,BigInteger.ONE);
}
public Rati(BigInteger x,BigInteger y){
zi=x;
mu=y;
BigInteger g=gcd(zi,mu);
zi=zi.divide(g);
mu=mu.divide(g);
}
static BigInteger gcd(BigInteger a,BigInteger b){
if(b.equals(b.ZERO)) return a;
return gcd(b,a.mod(b));
}
public Rati add(Rati it){
return new Rati(zi.multiply(it.mu).add(mu.multiply(it.z
a746
i)));
}
public Rati mul(Rati it){
return new Rati(zi.multiply(it.zi),mu.multiply(it.mu));
}
public String toString(){
String s=zi.toString();
if(mu.equals(BigInteger.ONE)==false)
s+="/"+mu;
return s;
}
public static void main(String[] args) {
//System.out.println(new Rati(1,3).add(new Rati(1,6)));
}
}
7. 素数
【问题描述】第1个素数是2,第2个素数是3,…
求第100002(十万零二)个素数
【源代码】
【JAVA:于航】
/*
筛到第x个素数,需要数组准备多大?
素数分布定理:不大于n的素数的个数为:n / ln(n)
double t = 100;
while(t / Math.log(t) < x) t *= 1.1;
System.out.println(t);
*/
public class SuShu
{
public static void main(String[] args)
{
int N = 1500 * 1000;
int x = 100002;
byte[] a = new byte
;
for(int i=2; i<N/2; i++)
{
if(a[i]==1) continue; //越过已经找到的合数
for(int k=2; k<=N/i; k++)
{
if(i*k<N) a[i*k] = 1;
}
}
int m = 0;
for(int i=2; i<N; i++)
{
if(a[i]==0)
{
m++;
if(m==x) System.out.print(i + " ");
}
}
System.out.println("m=" + m);
}
}
相关文章推荐
- 数学知识:和式的处理及二项式系数的运用
- 蓝桥杯C语言培训4 数学知识的运用 例题2 天平称重
- 数学知识在游戏中的运用
- 蓝桥杯C语言培训4 数学知识的运用 例题1 奇怪的捐赠(进制转换)
- 蓝桥杯C语言培训4 数学知识的运用 作业 求素数(素数的筛法)
- 蓝桥第四课《数学知识的运用》
- 蓝桥杯C语言培训4 数学知识的运用 例题4 公约公倍
- 蓝桥杯C语言培训4 数学知识的运用 例题5 一步之遥
- VC绘图/游戏简易教程--5:数学知识在绘图中的运用
- 蓝桥杯C语言培训4 数学知识的运用 例题3 尼姆堆
- 数学知识的运用(奇怪的捐赠)
- 数学基础知识积累——范数
- 股票入门基础知识29:如何运用日本蜡烛图进行技术分析
- [jbb0523整理]压缩感知中的数学知识:线性方程组的解
- 股票入门基础知识38:运用图表分析市场行情
- python基础知识和运用
- 相关数学知识
- Python基础知识——python中的if __name__ == "__main__"运用
- 3D数学 ---- 矩阵的更多知识
- word2vec 中的数学原理详解(二)预备知识