1.神奇算式
#include <iostream>
#include <cstring>
using namespace std;
bool check(int a,int b){
int st[10];
memset(st,false,sizeof st);
int c=a*b;
//注意c必须为四位数
if(c>=9999||c<=1000)return false;
while(a){
if(st[a%10]!=0)return false;
else st[a%10]++;
a/=10;
}
//puts("1");
while(b){
if(st[b%10]!=0)return false;
else st[b%10]++;
b/=10;
}
// puts("2");
while(c){
if(st[c%10]!=1)return false;
else st[c%10]--;
c/=10;
}
// puts("3");
return true;
}
int main()
{
long long ans=0;
//可以保证i,j一定不相同且仅计算一次
for(int i=10;i<=99;i++){
for(int j=i+1;j<=99;j++){
if(check(i,j)){ans++;
//printf("%d",ans);
}
}
}
for(int i=100;i<=999;i++){
for(int j=1;j<=9;j++){
if(check(i,j)){ans++;
//printf("%d",ans);
}
}
}
printf("%lld",ans);
// printf("%d",check(27,81));
return 0;
}
2.缩位求和
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
string str;
cin>>str;
long long ans=0;
int len=str.length();
for(int i=0;i<len;i++){
ans+=str[i]-'0';
}
while(ans>10){
long long tmp=0;
while(ans){
tmp+=ans%10;
ans/=10;
}
ans=tmp;
}
printf("%lld",ans);
return 0;
}
3.积木大赛
贪心
每次移除最小的层
我们可以这样想
第一座楼房是两层,那么至少要移除两层
接下来是第二座楼房,由于它比第一座楼层高,第一座楼的两层移除时,它的下面两层也已经被移走了,只需要再移走最上面的一层,即与前一座楼的差值即可;
第三座楼也是这样;
而这时出现了第四座楼,它比第三座楼矮,这就是说,在移走前面的几层后,这个地方已经是空的了。
第四座楼有一层,也就是说,第一层移走后,这里就是空白,将大厦分为两个区间(如上图,去除最下面黄色的一层)。
因此,这座楼右面的区间和左边区间从第一层(也就是这座楼的层数)开始就不在一个系统,需要单独操作,相当于一座独立的大厦。所以我们在这时候将当前楼层更新成这座矮楼,之后的操作和之前一样。
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
const int N=1e5+10;
int h[N];
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&h[i]);
}
int ans=h[0];
for(int i=1;i<n;i++){
if(h[i]>h[i-1])ans+=h[i]-h[i-1];
}
printf("%d",ans);
return 0;
}