水仙花数的判断函数如下,然后逐行读文件判断就是了。一会给你补全
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//判断是否是水仙花
int isSxh(int n)
{
int s = 0;
int a = n;
int d = 0;
while(a>0)
{
int d = a%10;
s += (d*d*d);
a = a/10;
}
if(s == n)
return 1;
else
return 0;
}
int main()
{
ifstream fin("in.txt");
string line;
if (fin)
{
while(getline(fin,line))
{
int n = atoi(line.c_str());
if(n<0) continue;
//cout << n << " ";
if(isSxh(n))
cout << "1"<<endl;
else
cout << "0"<<endl;
}
}
return 0;
}
分解出个位,十位和百位,立方求和
fopen打开文件,fgets逐行读取,fscanf分解出整数