day 05
1 数的分解
拆分成3个数相加得到该数
然后采用了一种巨愚蠢的办法:
int main()
{
int count = 0;
int a = 2;
int b = 0;
int c = 1;
int d = 9;
int a1, a2, a3;
int c1, c2, c3;
int d1, d2, d3;
for (a1 = 0; a1 < 2; a1++)
{
for (a2 = 0; a2 < 2; a2++)
{
for (a3 = 0; a3 < 2; a3++)
{
if (a1 + a2 + a3 == 2)
{
for (c1 = 0; c1 <= 1; c1++)
{
for (c2 = 0; c2 <= 1; c2++)
{
for (c3 = 0; c3 <= 1; c3++)
{
if (c1 + c2 + c3 == 1)
{
for (d1 = 0; d1 <= 9; d1++)
{
for (d2 = 0; d2 <= 9; d2++)
{
for (d3 = 0; d3 <= 9; d3++)
{
if (d1 + d2 + d3 == 9
&& d1 != 2 && d1 != 4
&& d2 != 2 && d2 != 4
&& d3 != 2 && d3 != 4)
{
if (a1 * 1000 + 0 * 100 + c1 * 10 + d1 != a2 * 1000 + 0 * 100 + c2 * 10 + d2
&& a2 * 1000 + 0 * 100 + c2 * 10 + d2 != a3 * 1000 + 0 * 100 + c3 * 10 + d3
&& a1 * 1000 + 0 * 100 + c1 * 10 + d1 != a3 * 1000 + 0 * 100 + c3 * 10 + d3
&& a1 * 1000 + 0 * 100 + c1 * 10 + d1 != 0)
{
count++;
}
}
}
}
}
}
}
}
}
}
}
}
}
printf("%d", count);
return 0;
}
为什么说这种方法愚蠢呢?
我将2019 拆分成2 、0、1、9四个数分别计算(个位十位百位千位)
2 只有1 1 0 这种情况
0 只有0 0 0 这种情况
1 只有0 0 1 这种情况
然后排列组合 判断几数相加是否等于2019
但是我没有思考到1个问题:进位,因为可以通过进位的方式获得更大的值,三数相加任然可以等于2019,我只是单纯的考虑到每一位对应的值
所以当看到正确结果完全傻眼了,思考方向错了!!!
那么正确方法应该如何处理呢?
2 猜生日
#include<stdio.h>
int main(void)
{
int b=19000600,y,d,x;
for(y=0;;y++)
{
for(d=1;d<=30;d++)
{
x=b+y*10000+d;
if(x%2012==0&&x%3==0&&x%12==0)
{
printf("%d",x);
return 1;
}
}
}
}
day06
1 星期计算
//巧法妙解,非暴力算法
using namespace std;
int main()
{
int s=7;
//每20天余6天,两个20天就余6*2,20个20天就余6*20;再把这个20当作一个20天,20的3次方就是6的三次方
//以此类推,20的22次方天就余6的22次方天;
//每六天就少一天,两个六天就少-1*2,6个6天就少-1*6;再把6当作-1天,如此类推就是-1的22次方,就是1,结果就是6+1天
cout<<s<<endl;
return 0;
}
2 考勤打卡
#include <bits/stdc++.h>
using namespace std;
int a[10001];
int main()
{
int n;
cin >> n;
string s;
for(int i = 0; i < n; i++)
{
cin >> s;
cin >> a[i];
}
sort(a, a + n);
for(int i = 0; i < n; i++)
{
if(i > 0 && a[i] == a[i - 1])
{
continue;
}
cout << a[i] << endl;
}
return 0;
}
day07
1 三角回文数
#include<bits/stdc++.h>
using namespace std;
int main()
{
int x = 0;
for(int i=0;;i++){
x+=i;
if(x>20220514){
string s = to_string(x);
string s1 = s;
reverse(s.begin(),s.end());
if(s1==s)
{
cout<<x<<endl;
return 0;
}
}
}
return 0;
}
用for循环和变量i,设结果变量是x,x+=i得出的结果一定是三角数,然后再判断它是不是大于题目给出的数值20220514,当x累加x大于20220514后就可以开始判断其是不是回文数了,如果是回文数,就可以输出当作结果了,然后结束程序。
2 数数
#include<iostream>
using namespace std;
bool check_prime(int x)
{
for(int i = 2;i *i <= x;i++)
{
if(x % i == 0) return false;
}
return true;
}
bool f(int x)
{
int s=0;
for(int i=2;i*i<=x;i++)
{
// 如果可以除尽的话,一直除下去
if(x%i==0)
{
while(x%i==0)
{
x/=i;
s++;
}
}
}
// 如果最后不是一的话,则最后一个约数就是一个质数
if(x!=1)s++;
// 如果是十二个质数相乘的话,则满足题意
if(s==12)return true;
else return false;
}
int main()
{
// int ans=0;
// for(int i=2333333;i<=23333333;i++)
// {
// if(f(i))ans++;
// }
// cout<< ans << endl;
cout << 25606 << endl;
return 0;
}
day08
1 分数
#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;
int main()
{
int k=0,p;
for(int i=0;i<20;i++){
k=k+pow(2,i);
}
p=pow(2,19);
printf("%d/%d",k,p);
return 0;
}
2. 回文日期
#include<bits/stdc++.h>
using namespace std;
int rw(int x)
{
int temp = 0;
for(int i = 0;i < 4;i++)
{
temp *= 10;
temp += x % 10;
x /= 10;
}
return temp;
}
bool isValidate(int x)
{
int dayInMonth[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int year = x / 10000;
int md = x % 10000;
int month = md / 100;
int day = md % 100;
if(month > 12 || month < 1)
return false;
if(year % 4 == 0 && !(year % 100 == 0))
dayInMonth[2]++;
if(day > dayInMonth[month] || day <= 0)
return false;
return true;
}
int main()
{
int num;
cin>>num;
int a = num+1;
int b;
for(;a < 100000000;a++)
{
if(a / 10000 == rw(a % 10000) && isValidate(a))
{
printf("%08d\n",a);
break;
}
}
b = a / 10000;
for(;b < 10000;b++)
{
if(b / 100 == b % 100 && isValidate(b * 10000 + rw(b)))
{
printf("%04d%04d",b,rw(b));
break;
}
}
}