for (int i = 0;i <= 200000;i++){ // 如果i != 0;输出0 if (i != 0){ // 把i的平方转成String型(因为String.valueOf()转的是整型,但是参数是这个整型数的平方,此时超过了整型数范围,赋值给字符串值会为0,所以前面值定义为long) a = String.valueOf(i*i); // 截取a尾数并转成整型 b = Integer.valueOf(a.substring(a.length()-weiShu(i))); // 如果i的平方的尾数等于i;就输出i if (b == i){ System.out.println(i); } // 如果i = 0;输出0 }else System.out.println(0); }
时间: 2024-02-14 17:23:20 浏览: 25
这段代码的功能是输出所有平方值的尾数等于其本身的数字,范围在 0 到 200000 之间。具体实现过程为:
1. 从0到200000循环遍历每个数字,存储在变量 i 中。
2. 判断当前数字是否为0,如果不为0则继续执行,否则直接输出0。
3. 将当前数字 i 的平方计算出来,并将其转换成字符串类型,存储在变量 a 中。
4. 通过 weiShu(i) 方法获取当前数字 i 的位数,然后截取字符串 a 的尾数,并将其转换成整型,存储在变量 b 中。
5. 判断当前数字 i 是否等于变量 b 的值,如果相等则输出当前数字 i。
这段代码的实现思路比较简单,主要是通过字符串截取和转换实现了对数字尾数的提取和比较。需要注意的是,在使用 String.valueOf() 方法转换数字时,需要注意数据类型的范围,否则可能会出现数据溢出的情况。
相关问题
#include <bits/stdc++.h> using namespace std; int bucket[1005]={0},a,cnt,n; int main() { cin>>n; for(int i=1;i<=n;i++) { cin>>a; bucket[a]=1; } for(int i=0;i<=1000;i++) if(bucket[i]==1) cnt++; cout<<cnt<<endl; for(int i=0;i<=1000;i++) if(bucket[i]==1) cout<<i<<' '; cout<<endl; return 0; }给每一行代码加注释
```c++
#include <bits/stdc++.h> // 引入所有常用标准库头文件
using namespace std;
int bucket[1005]={0},a,cnt,n; // 定义桶数组,a 为输入的数,cnt 为不同数的个数,n 为输入的数的个数
int main() {
cin>>n; // 输入数的个数
for(int i=1;i<=n;i++) // 循环读入每个数,并统计到桶数组中
{
cin>>a;
bucket[a]=1;
}
for(int i=0;i<=1000;i++) // 遍历桶数组,统计不同数的个数
if(bucket[i]==1)
cnt++;
cout<<cnt<<endl; // 输出不同数的个数
for(int i=0;i<=1000;i++) // 遍历桶数组,输出不同的数
if(bucket[i]==1)
cout<<i<<' ';
cout<<endl; // 输出换行
return 0;
}
```
#include <iostream> #include <cstring> #include <algorithm> #include <vector> using namespace std; const int N = 10; int n, m; int a[N]; int main() { cout << "请输入结点数:" << endl; while (cin >> n >> m) //输入图1、2的结点数 { if (m != n) { cout << "两图不同构!!!" << endl; continue; } //根据结点数n、m,动态生成两个二维数组来存图(使用邻接矩阵的方式) vector<int> w1(n + 1, 0); vector<vector<int> > g1(n + 1, w1); vector<int> w2(m + 1, 0); vector<vector<int> > g2(m + 1, w2); for (int i = 1; i <= n; i++) a[i] = i; //初始化a数组 for (int i = 1; i <= n; i++) //输入图1的邻接矩阵 for (int j = 1; j <= n; j++) cin >> g1[i][j]; for (int i = 1; i <= m; i++) //输入图2的邻接矩阵 for (int j = 1; j <= m; j++) cin >> g2[i][j]; //全排列 do { bool f = 0; int temp[N][N]; memset(temp, 0, sizeof temp); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { temp[i][j] = g1[a[i]][a[j]]; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (temp[i][j] != g2[i][j]) { f = 1; break; } } if (f == 1) break; } if (f == 0) cout << "两图同构^-^" << endl; } while (next_permutation(a + 1, a + 1 + n)); cout << "两图不同构!!!" << endl; } return 0; }简化的完整代码
这是一个判断两个图是否同构的程序,输入两个图的邻接矩阵,通过枚举两个图的节点的排列顺序,判断它们是否同构。如果是同构的,则输出"两图同构^-^",否则输出"两图不同构!!!"。
```cpp
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 10;
int n, m;
int a[N];
int main() {
cout << "请输入结点数:" << endl;
while (cin >> n >> m) { //输入图1、2的结点数
if (m != n) {
cout << "两图不同构!!!" << endl;
continue;
}
//根据结点数n、m,动态生成两个二维数组来存图(使用邻接矩阵的方式)
vector<int> w1(n + 1, 0);
vector<vector<int> > g1(n + 1, w1);
vector<int> w2(m + 1, 0);
vector<vector<int> > g2(m + 1, w2);
for (int i = 1; i <= n; i++) a[i] = i; //初始化a数组
for (int i = 1; i <= n; i++) //输入图1的邻接矩阵
for (int j = 1; j <= n; j++)
cin >> g1[i][j];
for (int i = 1; i <= m; i++) //输入图2的邻接矩阵
for (int j = 1; j <= m; j++)
cin >> g2[i][j];
//全排列
do {
bool f = 0;
int temp[N][N];
memset(temp, 0, sizeof temp);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
temp[i][j] = g1[a[i]][a[j]];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (temp[i][j] != g2[i][j]) {
f = 1;
break;
}
}
if (f == 1) break;
}
if (f == 0) cout << "两图同构^-^" << endl;
} while (next_permutation(a + 1, a + 1 + n));
cout << "两图不同构!!!" << endl;
}
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)