九进制转10进制:
1.顺子日期 - 蓝桥云课 (lanqiao.cn) (独立完成)
面向结果编程,知道了答案最后才把代码写出来:
#include<bits/stdc++.h>
using namespace std;
const int months[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int ans;
vector<int>v;
int is_leap(int year)
{
if ((year % 4 == 0 && year % 100) || year % 400 == 0)return 1;
return 0;
}
bool check(string Time)
{
v.clear();
int cnt = 0;
int flag = 0;
//转化为数字,拆位,看前后差值是否为1
stringstream ss;
int t=0;
ss << Time;
ss >> t;
while (t)
{
v.push_back(t % 10);
t /= 10;
}
reverse(v.begin(),v.end());
//之所以从第四位开始是因为所有日期前面都是2022
for (int i = 4; i < v.size()-1; i++)
{
if (v[i - 1] + 1 == v[i] && v[i] + 1 == v[i + 1])return true;
}
return false;
}
void solve1()
{
int y = 2022, m = 1, d = 1;
string Time1;
while (Time1!="20230101")
{
d++;
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
{
if (d == 32)
{
m++;
d = 1;
}
}
if (m == 13)
{
y++;
m = 1;
}
if (m == 4 || m == 6 || m == 9 || m == 11)
{
if (d == 31)
{
m++;
d = 1;
}
}
if (m == 2 && is_leap(y))
{
if (d == 30)
{
m++;
d = 1;
}
}
else if (m == 2 && !is_leap(y))
{
if (d == 29)
{
m++;
d = 1;
}
}
//拼接
string year = to_string(y);
string month = to_string(m);
string day = to_string(d);
//补0
while (year.size() < 4)year = '0' + year;
if (month.size() < 2)month = '0' + month;
if (day.size() < 2)day = '0' + day;
string Time2 = year + month + day;
//遍历日期
if (check(Time2))ans++;
Time1 = Time2;
}
}
int main()
{
//我们枚举2022年的所有日期,然后再看每个日期是否为顺子日期
solve1();
cout << ans << endl;
return 0;
}
1.刷题统计 - 蓝桥云课 (lanqiao.cn) (独立完成)
纯模拟,开Longlong只过了60%:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a, b, n;
void sovel()
{
cin >> a >> b >> n;
int week = 1; //从周一开始 7天一循环
int sum = 0;
while (1)
{
//如果是工作日
if (week % 7 >= 1 && week % 7 <= 5)
{
sum += a;
}
else
{
sum += b;
}
//cout <<week<<" "<< sum << endl;
if (sum >= n)break;
week++;
}
cout << week << endl;
}
signed main()
{
cin.tie(nullptr)->sync_with_stdio(false);
int t = 1;
while(t--)
sovel();
return 0;
}
因为100%的数据范围是1e18,所以模拟枚举肯定会超,所以我就想,先算出第一周做了多少题,后面就不用枚举了,直接通过计算算出总共题数n够做几周余几天即可。于是写了如下代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a, b, n;
int cnt;
void sovel()
{
cin >> a >> b >> n;
int sum = 5 * a + 2 * b; //一周的做题
int t1 = n / sum; //够做几周
int t2 = n % sum; //剩多少题
////剩的这些题够写几天
//剩的这些题能不能在工作日内写完
//当剩的不是0题时进入
while (t2)
{
t2 /= a;
cnt++;
if (cnt == 5)break;
}
//剩的这些题能不能在休息日写完,必然可以在周6写完,不然就又是一周了
//如果t2不为0说明工作日没写完剩的题,那就周六写
if (t2)
{
while (t2)
{
t2 /= b;
cnt++;
}
}
cout << t1 * 7 + cnt << endl;
}
signed main()
{
cin.tie(nullptr)->sync_with_stdio(false);
int t = 1;
while(t--)
sovel();
return 0;
}
结果只通过了50%,比枚举过得还少,有WA和TLE多种错误:
后面看了别人的代码知道哪里有错误了:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a, b, n;
void sovel()
{
cin >> a >> b >> n;
int sum = 5 * a + 2 * b; //一周的
int t1 = n / sum; //够做几周
int t2 = n % sum; //剩多少题
int c[7] = { a,a,a,a,a,b,b };
int cnt = 0;
while (t2 > 0)
{
t2 -= c[cnt++];
}
cout << t1 * 7 + cnt << endl;
}
signed main()
{
cin.tie(nullptr)->sync_with_stdio(false);
int t = 1;
while (t--)
sovel();
return 0;
}
原因是在求剩下的题可以做多少天的时候应该用剩余的题数减去每天做的题数,而我用的是除法
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a, b, n;
int cnt;
void sovel()
{
cin >> a >> b >> n;
int sum = 5 * a + 2 * b; //一周的做题
int t1 = n / sum; //够做几周
int t2 = n % sum; //剩多少题
////剩的这些题够写几天
//剩的这些题能不能在工作日内写完
//当剩的不是0题时进入
while (t2>0)
{
t2 -= a;
cnt++;
if(cnt>5)
if (cnt == 5)break; //如果工作日没做完,就要留到休息日做了
}
//剩的这些题能不能在休息日写完,必然可以在周6写完,不然就又是一周了
//如果t2不为0说明工作日没写完剩的题,那就周六写
if (t2>0)
{
while (t2>0)
{
t2 -= b;
cnt++;
}
}
cout << t1 * 7 + cnt << endl;
}
signed main()
{
cin.tie(nullptr)->sync_with_stdio(false);
int t = 1;
while(t--)
sovel();
return 0;
}
一棵树在被修剪前它就是最高的状态。
我们假设A树在从右向左修建时被修建了一次, 那么第二次被修建前它就是最高的:
如图所示,树A最高就是6米: