百度之星程序设计大赛试题答案 - 兰若书生的小屋 | 搜狐博客登录|注册|撰写新日志帮助 撰写新日志loading...
初赛第1题参考答案:
#include<stdio.h>
#include<math.h>
void All_Solution(long n) /*求所有解*/
{
int flag,have_slu=0; /*依次为每一轮是否有解的标志符和有解否的最终标志*/
long i,j,k,sum; /*每一轮的累加和都放在sum中*/
for(i=1;i<n;i++){
sum=i;
for(j=i+1;j<n;j++){
sum+=j;
flag=0;
if(sum==n){ /*找到解则置标志为1,同时记录最后一个数*/
flag=1;
k=j;
have_slu=1;
break;
}
else if(sum>n)break; /*如果sum大于n了则没必要继续累加了*/
}
if(flag){
for(j=i;j<=k;j++)
printf("%d ",j);
printf("\n");
}
}
if(have_slu==0)
printf("NONE!\n");
}
void main(int argc,char *argv[])
{
int len,i;
long n=0;
if(argc!=2){ /*命令行参数只能有两个,一个为程序名,一个为输入的数*/
printf("Enter Error!\n");
getch();
return;
}
len=strlen(argv[1]);
for(i=0;i<len;i++) /*检测输入的有效性,即只能输入数字*/
if(*(argv[1]+i)<48||*(argv[1]+i)>57){
printf("Must enter numerical character!\n");
getch();
return;
}
else /*将输入的数字字符串转换成数字*/
n+=pow(10,len-i-1)*(int)(*(argv[1]+i)-48);
All_Solution(n);
getch();
}
陈世熹的比赛答题源码 - 初赛第1题
#include <iostream>
#include <cstdio>
#include <utility>
#include <algorithm>
#include <cmath>
using namespace std;
int N, M;
pair<int, int> List[65536];
int main(int argc, char* args[])
{
int I, J, Max, A, B;
if (argc <= 1) return 0;
sscanf(args[1], "%d", &N);
M = 0;
Max = (int)sqrt((double)N) * 2;
for (I = 2; I <= Max; I++)
if (I & 1)
{
if (N % I == 0)
{
J = N / I;
A = J - I / 2;
B = J + I / 2;
if (A >= 1) List[M++] = make_pair(A, B);
}
}
else
{
if (N % I != 0 && N % (I / 2) == 0)
{
J = N / I;
A = J - I / 2 + 1;
B = J + I / 2;
if (A >= 1) List[M++] = make_pair(A, B);
}
}
if (M == 0)
printf("NONE\n");
else
{
sort(List, List + M);
for (I = 0; I < M; I++)
{
printf("%d", List[I].first);
for (J = List[I].first + 1; J <= List[I].second; J++)
printf(" %d", J);
printf("\n");
}
}
return 0;
}
3 回复:陈世熹的比赛答题源码 - 初赛第2题
#include <iostream>
#include <cstdio>
#include <utility>
#include <algorithm>
using namespace std;
struct pair_less
{
bool operator()(const pair<int, int>& A, const pair<int, int>& B)
const
{
return A.first < B.first;
}
};
int N;
pair<int, int> List[2000000];
int MaxResult;
int main()
{
int I, J, Last, A, B;
freopen("input.txt", "r", stdin);
N = 0;
while (scanf("%d%d", &A, &B) > 0)
{
if (A > B) I = A, A = B, B = I;
List[N] = make_pair(A, B);
N++;
}
sort(List, List + N, pair_less());
Last = 0x80000000;
MaxResult = 0;
for (I = 0; I < N; I++)
{
if (List[I].first <= Last)
{
if (List[I].second <= Last)
J = List[I].second - List[I].first + 1;
else
J = Last - List[I].first + 1;
if (J > MaxResult) MaxResult = J;
}
if (List[I].second > Last) Last = List[I].second;
}
printf("%d\n", MaxResult);
return 0;
}
4 回复:陈世熹的比赛答题源码 - 初赛第3题
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
struct word_less
{
bool operator()(const pair<char*, char*>& A, const pair<char*,
char*>& B) const
{
return strcmp(A.first, B.first) < 0;
}
};
int N, M;
pair<char*, char*> List[100000];
char Word[100001];
char* get_word()
{
char* P;
int Len;
Len = (int)strlen(Word);
P = new char[Len + 1];
strcpy(P, Word);
return P;
}
int main()
{
freopen("dict.txt", "r", stdin);
N = 0;
while (scanf("%s", Word) > 0)
{
List[N].first = get_word();
scanf("%s", Word);
List[N].second = get_word();
N++;
}
stable_sort(List, List + N, word_less());
freopen("text.txt", "r", stdin);
M = 0;
while (scanf("%s", Word) > 0)
{
if (M > 0) printf(" ");
M++;
pair<char*, char*>* It = upper_bound(List, List + N,
make_pair((char*)Word,(char*) 0), word_less());
if (It > List && strcmp(It[-1].first, Word) == 0)
printf("%s", It[-1].second);
else
printf("%s", Word);
}
printf("\n");
return 0;
}
作者: 61.135.146.* 2005-10-18 12:07 回复此发言
5 回复:陈世熹的比赛答题源码 - 初赛第4题
#include <iostream>
#include <cstdio>
#include <cstring>
#include <utility>
#include <map>
using namespace std;
struct word_less
{
bool operator()(char* A, char* B) const
{
return strcmp(A, B) < 0;
}
};
typedef map<char*, int, word_less> my_map;
my_map Map;
int N, Min;
char Word[100001];
char* get_word()
{
char* P;
int Len;
Len = (int)strlen(Word);
P = new char[Len + 1];
strcpy(P, Word);
return P;
}
int main()
{
my_map::iterator It;
freopen("corpus.txt", "r", stdin);
Map.clear();
while (scanf("%s", Word) > 0)
{
It = Map.find(Word);
if (It == Map.end())
Map.insert(make_pair(get_word(), 1));
else
It->second++;
}
Min = 0x7fffffff;
for (It = Map.begin(); It != Map.end(); ++It) if (It->second < Min)
Min = It->second;
N = 0;
freopen("corpus.txt", "r", stdin);
while (scanf("%s", Word) > 0)
if (Map[Word] > Min)
{
if (N > 0) printf(" ");
N++;
printf("%s", Word);
百度之星程序设计大赛试题和答案
4星 · 超过85%的资源 需积分: 50 107 浏览量
2008-11-13
22:55:40
上传
评论
收藏 10KB RAR 举报
liuyijesn
- 粉丝: 1
- 资源: 6
最新资源
- 基于MATLAB的PCA算法人脸识别项目源码+GUI界面+说明文档.zip
- 基于STM32的测量温度与压力的数据处理设计源码+论文(毕业设计).zip
- 基于C51带字库LCD12864(ST7920)的keil工程源码,只支持8位并口通讯(不支持串口),可显示中文.zip
- 基于SI4463射频模块433MD-SMA无线模块软硬件技术资料及(SI4463)IC技术资料文档.zip
- (GPS+北斗+GSM)HLK-GS2503模块软硬件开发资料包硬件参考设计(原理图PCB)+技术文档资料.zip
- 基于BERT+Biaffine结构的关系抽取模型源码+文档说明.zip
- 利用c语言编写的冒泡排序代码
- 基于Ansoft-HFSS知识总结hfss中文教程HFSS培训教材等技术资料合集(50个).zip
- 基于Python+OpenCV的材料缺陷检测程序项目源码课程设计.zip
- 基于c语言实现的二叉树代码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈