持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第13天,点击查看活动详情
#include <iostream>
#include <string>
using namespace std;
bool isnot(int i)
{
if(i==0)
return true;
int a = i*i;
int sum= 0;
int j=1;
while(a!=0)
{
if(sum == i)
return true;
else
{
int tem =a%10;
for(int k=1;k<j;k++)
tem*=10;
j++;
sum+=tem;
a=a/10;
}
}
if(sum== i)
return true;
else
return false;
}
int main(){
int n;
cin>> n;
int count=0;
for(int i=0;i<=n;i++)
{
if(isnot(i))
count++;
}
cout<<count;
}
#include <iostream>
#include <string>
#include<math.h>
using namespace std;
bool isnot(int a)
{
for(int i=2;i<=sqrt(a);i++)
{
if(a%i==0)
return false;
}
return true;
}
int main(){
int n;
cin>> n;
int count=0;
for(int i=2;i<=n;i++)
{
if(isnot(i))
count++;
}
cout<<count;
}
注意题目要求,所以我用的是 for(int i=2;i<=sqrt(a);i++) 这个循环来判断
class Solution {
public:
int FirstNotRepeatingChar(string str) {
map<char,int> m;
for(int i=0;i<str.size();i++)
{
m[str[i]]++;
}
for(int i=0;i<str.size();i++)
{
if(m[str[i]]==1)
{
return i;
}
}
return -1;
}
};
这个题,主要就是用了map的特性,关于map的特性,可以看下我另外一篇文章C++精通之路:map和set
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有