第一题代码:
#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;
}