如何从文件中输出第一个和最后一个完全平方之间的数字?

问题描述

我有一个文件(“bac.txt),其中包含数字 11 245 36 67 8 576 11 16 41 144 67 89 1011(这是未知数量的数字)。 第一个完全正方形是 36,最后一个是 144,我必须输出这 2 个之间的数字,所以输出必须是 36 67 8 576 11 16 41 144。我设法弄到了最后一个,但不知道如何记住第一个

#include <iostream>
#include <fstream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    ifstream f("bac.txt");
    int n,rp,x;
    int first = INT_MAX,last = INT_MAX;
    int cnt = 0,cnt2 = 0;

    while(f >> n){
        rp = sqrt(n);
        if(rp * rp == n)
            last = n;
        if((rp * rp == n) && n < first)
            first = n;
    }
    cout << first << " " << last;
    return 0;
}

这个只输出最小的一个和最后一个(我没有尝试输出间的)。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)