#include #include #include #include

using namespace std;

bool isPerfectNumber(int num) { int sum = 0; for (int i = 1; i <= num / 2; i++) { if (num % i == 0) { sum += i; } } if (sum == num) { return true; } return false; }

int main() { // 获取文件路径 string filePath = "D:/temp/"; // 打开源文件 ifstream sourceFile(filePath + "source.txt"); // 打开目标文件 ofstream destFile(filePath + "destination.txt");

// 输出学号姓名到目标文件第一行
destFile << "学号:xxx,姓名:xxx" << endl;

vector<int> nums;
int num;
while (sourceFile >> num) {
    nums.push_back(num);
}

// 查找完数并写入到目标文件中
for (int i = 0; i < nums.size(); i++) {
    if (isPerfectNumber(nums[i])) {
        destFile << nums[i] << endl;
    }
}

// 关闭文件
sourceFile.close();
destFile.close();

return 0;

}

C++ 编程实现完数查找并写入文件

原文地址: https://www.cveoy.top/t/topic/oJTD 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录