2018年蓝桥杯真题解析分析及代码听课笔记

在这里插入图片描述

第一题代码:

#include<bits/stdc++.h>
using namespace std;
/**
*第一题 first way 快速幂运算
*/
//累乘
long pow_2(int b){
    long x=2;
    long res = 1;
    while(b>0){
        if(b&1)
            res *= x;
        b>>=1;
        x=x*x;
    }
    return res;
}
//计算最大公约数
int gcd(long a,long b){
    if(b==0)
        return a;
    return gcd(b,a%b);
}
int main(){
    cout<<gcd(pow_2(20)-1,pow_2(19))<<endl;
    cout<<pow_2(20)-1<<"/"<<pow_2(19)<<endl;
}

/**
*second way
*/
int main(){
    long a = (1<<20)-1;
    long b = (1<<19);
    cout<<a<<"/"<<b<<endl;
    return 0;
}
java方法实现
package LanQiaoTest;
import java.math.BigInteger;
public class demo {
	/**
	 * JAVA way
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		BigInteger two = BigInteger.valueOf(2);
		BigInteger a = two.pow(20).subtract(BigInteger.ONE);
		BigInteger b = two.pow(19);
		BigInteger gcd = a.gcd(b);//最大公约数
		System.out.println(gcd);//1
		System.out.println(a+"/"+b);
	}
}

第二题代码:

第二题只需要知道2000年是闰年,所以只要将每个月份的日子加起来即可!

第三题代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
int m = 1;
int total = m;
int day = 1;
while(total<108){
    day++;
    m +=2;
    total += m;
    cout<<day<<" "<<m<<" "<<total<<endl;
    }
    cout<<day<<endl;
return 0;
}

Java代码类似c++代码,此处不再详细写入!

第四题代码:

#include<bits/stdc++.h>
using namespace std;
//判断1901-2000那些年份是闰年
bool isLeap(int x){
    return x%400 == 0||(x%4==0&&x%100!=0);
}
int main(){
    int t = 0;
    for(int i = 1901; i<2001;i++){
        if(isLeap(i))
            t += 366;
        else
            t += 365;
    }
    t -= 6;
    int ans = 0;
    for(;t>0;t -=7){
        ans++;
    }
    cout<<ans<<endl;
    return  0;
}
java方法实现:
package LanQiaoTest;
public class demo {
	/**
	 * JAVA way
	 */
	public static void main(String[] args) {
		int t = 0;
		for (int i = 1901; i <= 2000; i++) {
			if(isLeap(i))
				t += 366;
			else
				t += 365;
		}
			t -= 6;
			
			int ans = 0;
			for (; t > 0; t -= 7) {
				ans++;
			}
			System.out.println(ans);
	}
	private static boolean isLeap(int x){
		return x % 400 == 0||(x % 4 == 0&& x % 100 != 0);
	}
}

第五题代码:

#include<bits/stdc++.h>
using namespace std;
//明码
void toBinaryStr(int i,string &ans){
    if(i >= 0){
        ans[0] = '-';
        for(int  j = 0; j < 7;++j){
            if(((i>>j)&1)==1)//二进制上位1
            {
                ans[8-j-1] = '1';
            }
        }
    }else{
        ans[0]='1';
        for(int j = 0; j<7;++j){
             if((((128+i)>>j)&1)==1)//二进制上位1
            {
                ans[8-j-1] = '1';
            }
        }
    }
}
int main(){
    for(int i = 0; i<10;++i){
        //下面的32个数字组成一个汉字
        for(int j = 0; j<16; ++j){
            int x,y;
            cin>> x>>y;
            string xx = "--------",yy = "--------";
            toBinaryStr(x,xx);
            toBinaryStr(y,yy);
            cout<<xx + yy <<endl;
        }
        cout<<endl<<"=============================="<<endl;
    }
    // printf("%s\n",toBinaryStr(-1));
        long long ans = 9;
        for(int k = 0; k < 8;k++){
            ans *= 9;
        }
        cout<<ans<<endl;
        return 0;
}

第六题代码:

#include<bits/stdc++.h>
using namespace std;
//猴子吃桃子
int main(){
    //枚举,筛选
    for(int i = 6; i < INT_MAX;++i){
        if((i-1)%5!=0)continue;
        int m = i;
        m -= 1;
        m = m/5*4;
        if(m<=0||(m-2)%5!=0)continue;
        m -= 2;
        m =m/5*4;
        if(m<=0||(m-3)%5!=0)continue;
        m -= 3;
        m =m/5*4;
        if(m<=0||(m-4)%5!=0)continue;
        m -= 4;
        m =m/5*4;
        if(m<=0||m%5!=0)continue;
        cout<<i<<endl;
        break;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NoSuchManException

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值