这个程序的时间复杂度是多少?
问题描述:
我是新算法中的时间复杂性。 这是计算文本文件中字数的代码。 我的问题是,每次我的程序打印一个话多的文件中的实际计数,就像如果我在我的文件11个字它打印12这个程序的时间复杂度是多少?
#include<fstream>
#include<iostream>
#include<string>
using namespace std;
/* main function */
void main()
{
ifstream inFile; //file file name
string fileName;
string word;
int count = 0;
inFile.open("example.txt");
while(!inFile.eof())
{
inFile >> word;
++count;
}
cout << "Number of words in file is " << count<<endl;
inFile.close();
}
//this file is for counting the number of words in a text file**
答
第一件事,第一:Why is iostream::eof inside a loop condition considered wrong?这将回答你的额外计数问题。
然后,来的复杂性,因为直到它到达文件结尾它会去,虽然每N个字,它将在O(N)时间
也做,void main()
不法律 C++,主要应该返回int