真题见如下链接
https://www.lanqiao.cn/courses/2786/learning/?id=280827
第一题
如果实在不会了直接char强制转换int也可以做啊,忽略了。
第二题
科普一下:1Mb = 1024Kb = 1024 * 1024b
可以通过sizeof(int)得到 int的字节数为4
所以这道题的答案为256*1024*1024/4=67108864
第三题
一道简单的模拟题,直接看代码。
//Problem C
#include <iostream>
using namespace std;
int n[10];//用于存储每一个数字卡片剩余张数
bool check(int a)
{
while (a > 0)
{
int tmp = a % 10;
a /= 10;
n[tmp]--;//剩余张数减一
if (n[tmp] == -1)//这个数字无法组合
{
return true;
}
}
return false;
}
int main()
{
for (int i = 0; i < 10; i++)
{
n[i] = 2021;//初始化,每个数字都有2021张
}
int i = 1;
for (i;; i++)
{
bool f = check(i);
if (f)
break;
}
cout << i - 1 << endl;
return 0;
}
第四题
略,等学完图论补充。
第五题
#include <iostream>
#include <cstdio>
#define LL long long
using namespace std;
int main()
{
LL t;
scanf("%lld", &t);
t /= 1000;//毫秒转秒
t %= 86400;//精确到一天的秒数
int hh = t / 3600;
t %= 3600;
int mm = t / 60;
t %= 60;
int ss = t;
printf("%0.2d:%0.2d:%0.2d\n", hh, mm, ss);
return 0;
}
第六题
很妙啊,参考https://zhuanlan.zhihu.com/p/37895166
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int index = 1, count = 1, weight = 1;
while (weight < n) {
count++;
index *= 3;
weight += index;
}
cout << count << endl;
return 0;
}
第七题
杨辉三角形
参考https://blog.csdn.net/weixin_44091134/article/details/116748883
这是我的注解
#include <iostream>
#include <cmath>
#define LL long long
using namespace std;
int N;
LL c(int a, int b)
{
LL res = 1;
for (int i = a, j = 1; j <= b; i--, j++)
{
res = res * i / j;
if (res > N)//大于N没有意义
return res;
}
return res;
}
bool check(int k)//二分查找
{
int l = 2 * k;//左边界
int r = max(l, N);//右边界
while (l < r)
{
int mid = (l + r) / 2;
if (c(mid, k) >= N)
{
r = mid;
}
else
{
l = mid + 1;
}
}
if (c(r, k) != N)
{
//cout << k << " not found" << endl;
return false;//没有找到
}
cout << 1ll * (r + 1) * r / 2 + k + 1 << endl;
return true;
}
int main()
{
cin >> N;
for (int i = 16; i >= 0; i--)//从大到小保证找到的数出现的最早
{
if (check(i))
break;
}
return 0;
}
第八题
看见数据结构就头疼,等学完了再说。
第九题
还在思考中。