自守数
来源:互联网 发布:java等级证书 编辑:程序博客网 时间:2024/04/30 02:40
自守数是指一个数的平方的尾数等于该数自身的自然数。例如:252 = 625,762 = 5776,93762 = 87909376。请求出n以内的自守数的个数
前者用字符串的思维进行处理
#include<iostream>#include<string>#include<stdlib.h>using namespace std;int main(){ int n, b; int k = 0; cin>>n; for(int i = 0; i < n; i++){ int sum = i*i; char schar[32]; char ichar[32]; itoa(sum, schar, 10); // 将整型值转换为字符串 itoa(i, ichar,10); string str(schar); // 创建字符串str string istr(ichar); //cout<<"str = "<<str<<" "; //cout<<"str.length() = "<<str.length()<<endl; int len = istr.length(); if(str.substr(str.length() - len) == istr) { //cout<<"i = "<<i<<endl; k++; } } cout<<k<<endl;}
<pre style="word-wrap: break-word; white-space: pre-wrap;">#include <iostream>using namespace std;int CalcAutomorphicNumbers( int n){int count=0;for(int i=0;i<=n;i++){int sum=i*i;if (!(i/10)) //一位数{if(sum%10==i)count++;}else if (!(i/100)) //两位数{if(sum%100==i)count++;}else if (!(i/1000)) //三位数{if(sum%1000==i)count++;}else if (!(i/10000)) //四位数{if(sum%10000==i)count++;}}return count;}int main(){int num;cin>>num;cout<<CalcAutomorphicNumbers(num);}
0 0
- 自守数
- 自守数
- 自守数
- 自守数
- 自守数
- 自守数!
- 自守数
- 自守数
- 自守数
- 自守数
- 自守数
- 自守数
- 自守数
- 自守数
- 自守数
- 自守数
- 自守数
- 自守数
- NFS服务的配置
- HDOJ 题目4408 Minimum Spanning Tree(Kruskal+Matrix_Tree)
- 《数据结构与算法分析》二项队列详解
- 基于浏览器自身的event-loop特性,初探在此基础上的promise模型
- Windows,C++中各种数据类型的转换---很有用!
- 自守数
- 165Compare Version Numbers
- Android Studio查看MD5与SHA1
- Poj 1258 Agri-Net【最小生成树】
- JavaScript基础学习之-JavaScript权威指南-第三章类型、值和变量(2)
- c语言复习
- 设置tableView的背景图片
- setObjectForKey: object cannot be nil (key: av)'
- Unity3D系列1 : foreach对于性能到底有没有影响