2005百度之星初赛试题及程序整理

第一题(共四题 100 分):连续正整数( 10 分)

题目描述:一个正整数有可能可以被表示为 n(n>=2) 个连续正整数之和,如:

15=1+2+3+4+5
15=4+5+6
15=7+8

请编写程序,根据输入的任何一个正整数,找出符合这种要求的所有连续正整数序列。

输入数据:一个正整数,以命令行参数的形式提供给程序。

输出数据:在标准输出上打印出符合题目描述的全部正整数序列,每行一个序列,每个序列都从该序列的最小正整数开始、以从小到大的顺序打印。如果结果有多个序列,按各序列的最小正整数的大小从小到大打印各序列。此外,序列不允许重复,序列内的整数用一个空格分隔。如果没有符合要求的序列,输出 “NONE” 。

例如,对于 15 ,其输出结果是:
1 2 3 4 5
4 5 6
7 8
对于 16 ,其输出结果是:
NONE

评分标准:程序输出结果是否正确。

#include<iostream>
using namespace std;
int main()
{
	bool y=0;
	int n;
	cin>>n;
	int a=n;
	for(int i=1;i<=n/2;i++)
	{
		for(int j=i;j<=n;j++)
		{
			a=a-j;
			if(a==0)
			{
				//cout<<i<<endl;
			    //cout<<j<<endl;
				//cout<<n<<endl;
				y=1;
				for(int x=i;x<=j;x++)
				{
					cout<<x<<' ';
				}
				cout<<endl;	
			}		
		}
		a=n;
	}
    if(y==10)
	{
    	cout<<"NONE";
	}
	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 <iostream>
#include <fstream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
bool cmp(pair<int,int> a,pair<int,int> b)
{
    return a.first<b.first;
}
int main(int argc, char* argv[])
{
    int a,b,x,i;
    vector<pair<int,int> > interval;
    ifstream in("input.txt");
    while(in>>a>>b)
    {
        if(a<b)
            interval.push_back(make_pair(a,b));
        else
            interval.push_back(make_pair(b,a));
    }
    in.close();
    sort(interval.begin(),interval.end(),cmp);
    int maxcover = 0;
    int end = interval[0].second;
    int right,cover;
    for(i=1;i<interval.size();i++)
    {
        right = end>interval[i].second?interval[i].second:end;
        cover = right-interval[i].first>=0?right-interval[i].first+1:0;
        if(cover>maxcover)
            maxcover = cover;
        if(interval[i].second>end)
            end = interval[i].second;
    }
    cout<<maxcover<<endl;
    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 中出现次数最少的单词之后的文本(
词与词保持原来的顺序,仍以空格分隔)。

评分标准:程序输出结果必须正确,内存使用越少越好,程序的执行时间越快越好。  

#include <vector>
#include <map>
#include <string>
#include <stdio.h>
using namespace std;
/******************************************************************************************************
Description     : 对某一输入的字符串删除出现次数最少的单词,输出删除单词后的字符串 
Prototype       : void GetFilteredString(char* pInString,char** ppOutString)
Input Param     : char* pInString 输入字符串
Output Param    : char** ppOutString 输出字符串,内存空间由函数实现者申请,使用者释放
Return Value    : 无

********************************************************************************************************/
void GetFilteredString(char* pInString,char** ppOutString)
{
	map<string, int> my_map;
	vector<string> my_vec;
	map<string, int>::iterator key;
	vector<string>::iterator key_vec;
	int i = -1;
	int start_index = 0;
	int min = 0x10000;
	string temp = "";
	int length = 0;

	if (pInString == 0 || ppOutString == 0)
	{
		return;
	}	
	do
	{
		i++;
		if (pInString[i] == 0 || pInString[i] == ' ')

		{
			string s(pInString, start_index, i - start_index);
			my_map[s]++;
			my_vec.push_back(s);
			start_index = i + 1;
		}
	} while (pInString[i] != 0);

	for (key = my_map.begin(); key != my_map.end(); key++)
	{
		if (key->second<min)
		{
			min = key->second;
		}
	}
	
	for (key = my_map.begin(); key != my_map.end(); key++)
	{
		if (key->second != min)
		
			length += ((int)((key->first).length()) + 1)*key->second;		
	}

	*ppOutString = (char *)malloc((length + 1) * sizeof(char));

	if (*ppOutString == 0)
	{
		return;
	}		
	for (key_vec = my_vec.begin(); key_vec != my_vec.end(); key_vec++)
	{
		if (my_map[*key_vec] != min)
		{
			temp += *key_vec;
			temp += " ";
		}
	}
	if (temp != "")
	{
		temp.erase(temp.length() - 1, 1);//删除最后一个空格
	}
	strcpy(*ppOutString, temp.c_str());
	return;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值