百度之星程序設計大賽
試題
第一題(共四題100分):連續正整數(10分)
題目描述:一個正整數有可能可以被表示為n(n>=2)個連續正整數之和,如:
15=1+2+3+4+5
15=4+5+615=7+8
請編寫程序,根據輸入的任何一個正整數,找出符合這種要求的所有連續正整數序列。
輸入數據:一個正整數,以命令行參數的形式提供給程序。
輸出數據:在標準輸出上打印出符合題目描述的全部正整數序列,每行一個序列,每個序列都從該序列的最小正整數開始、以從小到大的順序打印。如果結果有多個序列,按各序列的最小正整數的大小從小到大打印各序列。此外,序列不允許重復,序列內的整數用一個空格分隔。如果沒有符合要求的序列,輸出“NONE”。
例如,對于15,其輸出結果是:
1 2 3 4 5
4 5 6
7 8
對于16,其輸出結果是:
NONE
評分標準:程序輸出結果是否正確。
我的程序:
#include <stdio.h>
#include <math.h>
void show(int a1, int n)
{
int i;
printf("%d", a1);
for (i = 1; i < n; i++)
{
printf(" %d", a1 + i);
}
printf("\n");
}
int main(int argc, char** argv)
{
int x, n, a1;
int showed = 0;
if (argc != 2)
return -1;
if (sscanf(argv[1], "%d", &x)!=1)
return -1;
x = x*2;
// n(1+n)/2 = x
n = (int)sqrt(x); // a*b = c^2 then a <= c <= b, so n <= sqrt(2x) <= n+1
for(; n >= 2; n--)
{
if (x % n)
continue;
a1 = x / n - n + 1;
if (a1 % 2 == 0)
{
a1 /= 2;
show(a1, n);
showed = 1;
}
}
if (showed == 0)
printf("NONE\n");
return 0;
}
//
第二題(共四題100分):重疊區間大小(20分)
題目描述:請編寫程序,找出下面“輸入數據及格式”中所描述的輸入數據文件中最大重疊區間的大小。
對一個正整數n,如果n在數據文件中某行的兩個正整數(假設為A和B)之間,即A<=n<=B或A>=n>=B,則n屬于該行;如果n同時屬于行i和j,則i和j有重疊區間;重疊區間的大小是同時屬于行i和j的整數個數。
例如,行(10 20)和(12 25)的重疊區間為[12 20],其大小為9;行(20 10)和(12 18)的重疊區間為[10 12],其大小為3;行(20 10)和(20 30)的重疊區間大小為1。
輸入數據:程序讀入已被命名為input.txt的輸入數據文本文件,該文件的行數在1到1,000,000之間,每行有用一個空格分隔的2個正整數,這2個正整數的大小次序隨機,每個數都在1和2^32-1之間。(為便于調試,您可下載測試input.txt文件,實際運行時我們會使用不同內容的輸入文件。)
輸出數據:在標準輸出上打印出輸入數據文件中最大重疊區間的大小,如果所有行都沒有重疊區間,則輸出0。
評分標準:程序輸出結果必須正確,內存使用必須不超過256MB,程序的執行時間越快越好。
我的程序:
#include <stdio.h>
#include <stdlib.h>
#ifndef MIN
#define MIN(a,b) ((a)<(b)?(a):(b))
#endif
int map[1000000][2];
int cmp(const void *a, const void *b)
{
return *(int*)a > *(int*)b;
}
inline int cover(int n[2], int e)
{
int left = n[0];
int right = MIN(n[1], e);
if (left <= right)
return right - left + 1;
return 0;
}
int main()
{
FILE *fp = fopen("input.txt", "rt");
int i = 0, n, a, b;
int end;
int maxcover = 0, c;
while(fscanf(fp, "%d%d", &a, &b) == 2)
{
if (a < b)
{
map[i][0] = a;
map[i][1] = b;
}
else
{
map[i][0] = b;
map[i][1] = a;
}
i++;
}
fclose(fp);
n = i;
qsort(map, n, sizeof(int)*2, cmp);
end = map[0][1];
for (i = 1; i < n; i++)
{
c = cover(map[i], end);
if (c > maxcover)
maxcover = c;
if (map[i][1] > end)
{
end = map[i][1];
}
}
printf("%d\n", maxcover);
return 0;
}
//第三題(共四題100分):字符串替換(30分)
題目描述:請編寫程序,根據指定的對應關系,把一個文本中的字符串替換成另外的字符串。
輸入數據:程序讀入已被命名為text.txt和dict.txt的兩個輸入數據文本文件,text.txt為一個包含大量字符串(含中文)的文本,以whitespace為分隔符;dict.txt為表示字符串(s1)與字符串(s2)的對應關系的另一個文本(含中文),大約在1萬行左右,每行兩個字符串(即s1和s2),用一個\t或空格分隔。dict.txt中各行的s1沒有排序,并有可能有重復,這時以最后出現的那次s1所對應的s2為準。text.txt和dict.txt中的每個字符串都可能包含除whitespace之外的任何字符。text.txt中的字符串必須和dict.txt中的某s1完全匹配才能被替換。(為便于調試,您可下載測試text.txt和dict.txt文件,實際運行時我們會使用不同內容的輸入文件。)
輸出數據:在標準輸出上打印text.txt被dict.txt替換后了的整個文本。
評分標準:程序輸出結果必須正確,內存使用越少越好,程序的執行時間越快越好。
我的程序:
#pragma warning(disable:4786)
#include <string>
#include <map>
#include <fstream>
#include <cassert>
#include <cstdio>
using namespace std;
map< string, string > dict;
void loadDict(const char *filename)
{
string a, b;
ifstream dic;
assert(filename != NULL);
dic.open(filename);
while(dic>>a>>b)
{
dict[a] = b;
}
dic.close();
}
const char *replaceWord(const string &word)
{
map< string, string >::iterator word2 = dict.find(word);
if (word2 == dict.end())
{
return word.c_str();
}
else
{
return word2->second.c_str();
}
}
int main()
{
bool isChinese = false;
char c;
string word;
loadDict("dict.txt");
FILE *fp = fopen("text.txt", "rt");
while(!feof(fp))
{
c = fgetc(fp);
if (isChinese)
{
word += c;
isChinese = false;
}
else
{
if ((c & 0x80) == 0 && isspace(c))
{
if (!word.empty())
{
printf("%s", replaceWord(word));
word = "";
}
printf("%c", c);
}
else
{
if (c & 0x80)
{
isChinese = true;
}
word += c;
}
}
}
fclose(fp);
if (!word.empty())
{
printf("%s", replaceWord(word));
}
return 0;
}
第四題(共四題100分):低頻詞過濾(40分)
題目描述:請編寫程序,從包含大量單詞的文本中刪除出現次數最少的單詞。如果有多個單詞都出現最少的次數,則將這些單詞都刪除。
輸入數據:程序讀入已被命名為corpus.txt的一個大數據量的文本文件,該文件包含英文單詞和中文單詞,詞與詞之間以一個或多個whitespace分隔。(為便于調試,您可下載測試corpus.txt文件,實際運行時我們會使用不同內容的輸入文件。)
輸出數據:在標準輸出上打印刪除了corpus.txt中出現次數最少的單詞之后的文本(詞與詞保持原來的順序,仍以空格分隔)。
評分標準:程序輸出結果必須正確,內存使用越少越好,程序的執行時間越快越好。
我的程序:
#pragma warning(disable: 4786)
#include <map>
#include <string>
#include <vector>
#include <cstdio>
#include <fstream>
#include <algorithm>
using namespace std;
struct Pair
{
Pair(){}
Pair(const string& w, int c):word(w), count(c)
{
}
string word;
int count;
};
map< string, int > wordmap;
vector< Pair > wordcount;
vector< int > wordindex;
int main()
{
// load the file, count word, build index
string word;
fstream file("corpus.txt");
while(file >> word)
{
int id;
map< string, int >::iterator itw = wordmap.find(word);
if (itw == wordmap.end()) // new word
{
id = wordcount.size();
wordmap[word] = id;
wordcount.push_back(Pair(word, id));
}
else // found it
{
wordcount[itw->second].count++;
id = itw->second;
}
wordindex.push_back(id);
}
file.close();
if (wordcount.empty())
return 0;
// find the min count
vector< Pair >::iterator itc = wordcount.begin();
int mincount = itc->count;
for (++itc; itc != wordcount.end(); ++itc)
{
if (mincount > itc->count)
mincount = itc->count;
}
// show all filtered words
if (wordindex.empty())
return 0;
// skip leading filtered words
vector< int >::iterator w = wordindex.begin();
while(w != wordindex.end() && wordcount[*w].count == mincount)
++w;
if (w == wordindex.end())
return 0;
/**//* debug use
for (vector< Pair >::iterator iw = wordcount.begin(); iw != wordcount.end(); ++iw)
printf("%s %d\n", iw->word.c_str(), iw->count);
printf("mincount = %d\n", mincount);
/*/
// show first word, with no space after
printf("%s", wordcount[*w].word.c_str());
for (++w; w != wordindex.end(); ++w)
{
if (wordcount[*w].count != mincount)
{
printf(" %s", wordcount[*w].word.c_str());
}
}
printf("\n");
//*/
return 0;
}