2目录
1、排他平方数
https://www.lanqiao.cn/problems/712/learning/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
bool p[10];//标记每个数字是否被使用
typedef long long ll;
bool f(ll x){//判断6位正整数中是否有重复数字
while(x){
ll s=x%10;
if(p[s]) return false;
else p[s]=1;
x/=10;
}
return true;
}
bool ff(ll x){//判断得到的新数每一位是否存在之前使用过的数字
while(x){
ll s=x%10;
if(p[s]) return false;
x/=10;
}
return true;
}
int main(){
ll i;
for( i=999999;i>=100000;i--){
memset(p,false,sizeof(p));
ll j=i*i;
if(!f(i)) continue;
if(!ff(j))continue;
cout<<i;
return 0;
}
return 0;
}
2、买不到的数目
https://www.lanqiao.cn/problems/213/learning/
样例:
输入
4 7
输出
17
这题想半天没思路,后来去看了y总的视频了解到可以用数论知识 裴蜀定理 巧妙解答
这里直接放上结论
如果 a,b 均是正整数且互质,那么由 ax+by,x≥0,y≥0 不能凑出的最大数是 ab−a−b。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int p, q,n;
cin >> p >> q;
n=(p-1)*(q-1)-1;
cout<<n;
return 0;
}
3、回文日期
https://www.lanqiao.cn/problems/498/learning/
样例:
输入
20200202
输出
20211202
21211212
这题从题意可看出需要
1.日期合法性判断
2.回文判断
3.ABABBABA型回文判断
这道题直接暴力枚举100000101~89991231再进行判断的话会超时
因此可以使用一种巧妙的做法,只枚举数字的前一半,再进行翻转,这样操作可直接省去回文的判断
stoi( ) 将string字符串转换为int类型 头文件为#include <string>
to_string( ) 将数值转换成字符串形式 (c++11特性) 头文件 为#include <string>
reverse()会将区间[beg,end)内的元素全部逆序
翻转字符串 reverse(a.begin(), a.end());
翻转字符数组 reverse(a,a+m);
日期合法性判断 直接放上我之前看到某个大佬写的模板(挺好用)
int months[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
bool check(int date)//判断日期合法性
{
int year = date / 10000; //年
int month = date % 10000 / 100; //月
int day = date % 100; //日
if(!month || month > 12 || !day ) return false;//如果月份大于12或者为零或者天数为零则该日期不合法
if(month != 2 && day > months[month]) return false;//在不是二月的情况下,该月实际天数大于该月最大天数,则该日期不合法
if(month == 2) //特判二月
{
if((year % 4 == 0&& year % 100 != 0) || (year % 400 == 0))//特判闰年
{
if(day > 29) return false;
}
else if( day > 28) return false;
}
return true;
}
详细代码:
#include<bits/stdc++.h>
using namespace std;
int months[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
bool check( string x)//判断日期合法性
{
int year = stoi(x.substr(0,4)); //年
int month =stoi(x.substr(4,2)); //月
int day = stoi(x.substr(6,2)); //日
if(!month || month > 12 || !day ) return false;//如果月份大于12或者为零或者天数为零则该日期不合法
if(month != 2 && day > months[month]) return false;//在不是二月的情况下,该月实际天数大于该月最大天数,则该日期不合法
if(month == 2) //特判二月
{
if((year % 4 == 0&& year % 100 != 0) || (year % 400 == 0))//特判闰年
{
if(day > 29) return false;
}
else if( day > 28) return false;
}
return true;
}
bool ok(string x){//判断是否为 ABABBABA回文串
if(x[0]==x[2]&&x[0]==x[5]&&x[0]==x[7]&&x[1]==x[3]&&x[1]==x[4]&&x[1]==x[6]&&x[0]!=x[1]) return true;
else return false;
}
int main(){
string s;
cin>>s;
int idx=0;
for(int i=stoi(s.substr(0,4));;i++){
string s1=to_string(i),s2=to_string(i);
reverse(s2.begin(),s2.end());
s1+=s2;
if(check(s1)&&s1>s&&idx==0){
cout<<s1<<endl;
idx++;
}
if(check(s1)&&s1>s&&ok(s1)){
cout<<s1;
break;
}
}
return 0;
}
4、约瑟夫环
https://www.lanqiao.cn/problems/231/learning/
输入
10 3
输出
4
很经典的题目,高中信息技术课学VB的时候就碰到过 ^_^,到现在完全忘记了
详细解释请见 约瑟夫环问题
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N=1e7+10;
int n,k;
int f(int n,int k){
int x=0;
for(int i=1;i<=n;i++){
x=(x+k)%i;//旧编号=(新编号+最大报数值)%旧一轮的人数取余
}
return x+1;
}
int main(){
cin>>n>>k;
cout<<f(n,k);
return 0;
}