7-1 猜数字-交互版
你需要编写一个“猜数字”的程序。跟你做过的大部分题目不一样,你需要通过不断地询问另外一个程序(以下称之为“交互程序”)来猜到最终的数字。
在你的程序刚运行时,交互程序会通过标准输入给你提供一个数字 N,表示你需要猜的数字在 1 到 N 范围里。
接下来你可以开始发起你的询问。你可以直接通过输出到标准输出来询问;每次询问为一个数字,表示你猜测的结果。每次询问后,你需要刷新输出流,否则可能会有输出内容停留在缓冲区不被输出。例如 C++ 你可以使用 fflush(stdout),Java 你可以使用 System.out.flush() ,Pascal 你可以使用 flush(output) ,Python 则可以使用 stdout.flush()。
交互程序会根据你的询问返回你的猜测与正确答案的比较情况。具体而言:
如果正确答案小于你的猜测,则会返回 <;
如果正确答案大于等于你的猜测,则会返回 >=
一旦你确定你找到了正确的答案,则输出 ! x(x 是你猜测的数字,注意与感叹号用空格隔开),并立刻结束你的程序(否则判题系统可能不能返回正确的判题结果)。
最后,总的询问次数不能大于等于 25 次。
请你尝试猜对正确的数字吧!
输入样例:
20
>=
<
>=
<
>=
<
输出样例:
5
18
10
13
12
11
! 12
样例程序
以下程序用于你理解如何与交互程序进行交互,并不是正确的程序。
#include <cstdio>
int main() {
int n;
scanf("%d", &n);
printf("%d\n", n / 2);
fflush(stdout);
char ans[10];
scanf("%s", ans);
if(ans[0] == '>') {
printf("! %d\n", n / 2 + 1);
fflush(stdout);
} else {
printf("! %d\n", n / 2 - 1);
fflush(stdout);
}
}
解题思路:
我们知道目标数字在 1 到 N 之间,所以首先设置 low = 1 和 high = N。计算中间值 mid,即 mid = (low + high) // 2。向交互程序发送 mid 作为猜测。
根据交互程序的响应:
如果响应是 <,则目标数字在 mid 右边,因此更新 low = mid + 1。
如果响应是 >=,则目标数字在 mid 左边或正好是 mid,因此更新 high = mid。
继续这个过程直到 low 等于 high,此时 low 即为目标数字。
当确定目标数字时,输出 ! x,其中 x 是猜测的数字,并结束程序。
答案:
#include<bits/stdc++.h>
using namespace std;
int main(){
int last=1,maxnumber;
char c[3];
cin>>maxnumber;
while(last != maxnumber){
int midnumber=(last + maxnumber + 1) >> 1;//随机数
printf("%d\n", midnumber);
fflush(stdout);
scanf("%s",c);
if(strcmp(c,"<")) last=midnumber;
else maxnumber= midnumber - 1;
}
printf("! %d\n", last);
fflush(stdout);
return 0;
}
7-2 新浪微博热门话题
新浪微博可以在发言中嵌入“话题”,即将发言中的话题文字写在一对“#”之间,就可以生成话题链接,点击链接可以看到有多少人在跟自己讨论相同或者相似的话题。新浪微博还会随时更新热门话题列表,并将最热门的话题放在醒目的位置推荐大家关注。
本题目要求实现一个简化的热门话题推荐功能,从大量英文(因为中文分词处理比较麻烦)微博中解析出话题,找出被最多条微博提到的话题。
输入格式:
输入说明:输入首先给出一个正整数N(≤10
5
),随后N行,每行给出一条英文微博,其长度不超过140个字符。任何包含在一对最近的#中的内容均被认为是一个话题,输入保证#成对出现。
输出格式:
第一行输出被最多条微博提到的话题,第二行输出其被提到的微博条数。如果这样的话题不唯一,则输出按字母序最小的话题,并在第三行输出And k more ...,其中k是另外几条热门话题的条数。输入保证至少存在一条话题。
注意:两条话题被认为是相同的,如果在去掉所有非英文字母和数字的符号、并忽略大小写区别后,它们是相同的字符串;同时它们有完全相同的分词。输出时除首字母大写外,只保留小写英文字母和数字,并用一个空格分隔原文中的单词。
输入样例:
4
This is a #test of topic#.
Another #Test of topic.#
This is a #Hot# #Hot# topic
Another #hot!# #Hot# topic
输出样例:
Hot
2
And 1 more ...
解题思路:
使用一个哈希表(或字典)来存储每个话题及其出现次数。哈希表的键是话题,值是出现的次数。遍历每条微博,从中提取所有用 # 包围的词汇。可以通过正则表达式(例如 #(\w+)#)来匹配这些话题。对于每个匹配到的话题,如果话题不在哈希表中,将其添加进去,初始计数为 1。如果话题已经存在,增加其计数。遍历哈希表,找到出现次数最多的话题。可以通过一个额外的变量来跟踪当前最大值和相应的话题。最后根据需求输出最热门的话题和它们的出现次数。如果有多个话题出现次数相同,可以输出所有这些话题。
答案:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXCHAR 140
typedef enum{empty, exist} EntryType;
typedef char EleType[MAXCHAR+1];
typedef struct HashEntry Cell;
struct HashEntry
{
EleType Data;
int Number;
EntryType Info;
int Collected;
};
typedef struct TblNode *HashTable;
struct TblNode
{
int TableSize;
Cell *Cells;
};
HashTable CreateTable(int TableSize)
{
HashTable H = (HashTable)malloc(sizeof(struct TblNode));
H->TableSize = 4*TableSize;
H->Cells = (Cell*)malloc(H->TableSize*sizeof(struct HashEntry));
for(int i=0; i<H->TableSize; i++)
{
H->Cells[i].Info = empty;
H->Cells[i].Number = 0;
H->Cells[i].Collected = 0;
}
return H;
}
int Hash(HashTable H, EleType key)
{
int hkey,count,ans;
hkey = count = 0;
while(key[count]!='\0' && count < 5)
{
hkey = hkey*3 + key[count];
count++;
}
return hkey % H->TableSize;
}
int CompareIgnoreCap(EleType a, EleType b)
{
int count = 0;
int flag = 1;
while(a[count] != '\0' && b[count] != '\0')
{
if( (a[count]-b[count]) != 0 && (a[count]-b[count]) != 'A'-'a' && (a[count]-b[count]) != 'a'-'A')
{
flag = 0;
break;
}
count++;
}
if(a[count] != '\0' || b[count] != '\0')
flag = 0;
return flag;
}
int LinearFind(HashTable H, EleType key)
{
int CurPos, NewPos;
int count = 0;
CurPos = NewPos = Hash(H, key);
while(H->Cells[NewPos].Info != empty && !CompareIgnoreCap(H->Cells[NewPos].Data, key))
{
count++;
NewPos = CurPos + count;
if(NewPos >= H->TableSize)
NewPos = NewPos%H->TableSize;
}
return NewPos;
}
void Insert(HashTable H, EleType key, int i)
{
int Pos = LinearFind(H, key);
if(H->Cells[Pos].Info == empty)
{
strcpy(H->Cells[Pos].Data, key);
H->Cells[Pos].Number = 1;
H->Cells[Pos].Info = exist;
H->Cells[Pos].Collected = i;
}
else if(H->Cells[Pos].Collected != i)
{
H->Cells[Pos].Number++;
H->Cells[Pos].Collected = i;
}
}
int IsALetter(char tmp)
{
int flag = 0;
if(tmp>='A' && tmp<='Z')
flag = 1;
if(tmp>='a' && tmp<='z')
flag = 2;
if(tmp == ' ')
flag = 3;
if(tmp>='0' && tmp<='9')
flag = 4;
if(tmp == '@')
flag = 5;
return flag;
}
char BToS(char X)
{
return X-'A'+'a';
}
char SToB(char X)
{
return X-'a'+'A';
}
void ReadTopics(HashTable H, int N)
{
int i, Pos,countT;
char tmp = 'k'; //select randomly
EleType key;
int count,tag;
for(i=0; i<N+1; i++)
{
scanf("%c", &tmp);
countT = 0;
while(tmp != '\n' && countT<1000)
{
if(tmp == '#')
{
count = 0;
scanf("%c", &tmp);
while(tmp != '#')
{
tag = IsALetter(tmp);
if(tag)
{
if(count == 0 && tag == 2)
tmp = SToB(tmp);
if(count > 0 && tag == 1)
tmp = BToS(tmp);
if(count > 0 && tag == 5)
tmp = ' ';
key[count++] = tmp;
}
scanf("%c", &tmp);
}
key[count] = '\0';
//Insert key
Insert(H, key, i);
}
scanf("%c", &tmp);
countT++;
}
}
}
void PrintResult(HashTable H)
{
int i,MaxNumber, MaxCount;
EleType output;
MaxCount = MaxNumber = 0;
for(i=0; i<H->TableSize; i++)
{
if(H->Cells[i].Number > MaxNumber)
MaxNumber = H->Cells[i].Number;
}
for(i=0; i<H->TableSize; i++)
{
if(H->Cells[i].Number == MaxNumber)
{
MaxCount++;
if(MaxCount == 1)
strcpy(output, H->Cells[i].Data);
else if(strcmp(output, H->Cells[i].Data)>0)
strcpy(output, H->Cells[i].Data);
}
}
printf("%s\n", output);
printf("%d\n", MaxNumber);
if(MaxCount-1>0)
printf("And %d more ...", MaxCount-1);
}
int main()
{
int N;
scanf("%d", &N);
HashTable H = CreateTable(N);
ReadTopics(H, N);
PrintResult(H);
return 0;
}