题解
2024-12-21 20:03:39
发布于:浙江
我的代码嘛,有些复杂:
#include <iostream>
#include <cstring>
using namespace std;
int main(){
string n; //建立字符串
bool flag = true; //判定是否是美丽数
cin >> n;
int a[12] = {}; //用『桶』来做
for(int i = 0;i < n.size();i++){
int x;
if(n[i] == '1') x = 1; //将字符转为数字
else if(n[i] == '2') x = 2;
else if(n[i] == '3') x = 3;
else if(n[i] == '4') x = 4;
else if(n[i] == '5') x = 5;
else if(n[i] == '6') x = 6;
else if(n[i] == '7') x = 7;
else if(n[i] == '8') x = 8;
else if(n[i] == '9') x = 9;
else if(n[i] == '0') x = 0;
a[x] += 1; //将每个数字对应的下标+1
}
for(int i = 0;i <= 9;i++){
if(a[i] != 0){ //判定次数与数字是否相等
if(i == a[i]) flag = true; //(此次判断为1-9)
else{
flag = false; //如果有,flag为true,否则为false
break; //并立即结束循环
}
}
else if(i == 0){ //此次判断为0
if(i == a[i]) flag = true;
else{
flag = false;
break;
}
}
}
if(flag == true) cout << "Yes"; //判断并输出
else cout << "No";
return 0;
}
这里空空如也
有帮助,赞一个