Astar2007初赛第一场的题目2

 今天查到了Astar2007的初赛题目,很感兴趣,便拿来做了一下,这是第二题。


2.大话西游与数字游戏

“叉烧鸡翅膀,我呀最爱吃!……”,百度spider组的“黑龙潭之行”在烤着鸡翅,唱着星爷的经典时达到高潮。大家在篝火旁围成一圈,开始玩“数7”加强版游戏,规则如下:
规则1:遇7的倍数或含7的数时pass。
规则2:遇有包含相同数字的数时pass。注意相同数字不必相邻。例如121。

数错的惩罚很残酷——吞食烤全羊。为避免惩罚,百度工程师们需要你——史上最强程序员的帮助。百度工程师想知道:
req1 x:符合规则1的第x个数是什么?
req2 y:符合规则2的第y个数是什么?
req12 z:同时符合规则1、2的第z个数是什么?
query n:数n是规则1中的第几个数,是规则2中的第几个数?

输入格式
输入的每一行为一个查询,由一个查询词和一个无符号整型数组成。共有四种查询,查询词分别为req1、req2、req12、query(区分大小写)。
输出格式
前三种查询输出一个无符号整型的解。对于“query n”的查询,若n是规则中的数则输出相应的解,否则输出-1。
输入样例 例
req1 10
req2 10
req12 10
query 14
输出样例 例
11
10
12
-1 13
评分规则
程序将运行在一台Linux机器上(内存使用不作严格限制),在每一测试用例上运行不能超过1秒,否则该用例不得分;

要求程序能按照输入样例的格式读取标准输入数据,按照输出样例的格式将运行结果输出到标准输出上。如果不能正确读入数据和输出数据,该题将不得分;

该题目共有10个测试数据集,其中数据1~5主要考查正确性,满足x,y,z,n
该题目20分。

ContractedBlock.gif ExpandedBlockStart.gif 我写的第2题C源码
  1ExpandedBlockStart.gifContractedBlock.gif/**//*包含头文件*/
  2None.gif#include <stdio.h>
  3None.gif#include <string.h>
  4None.gif
  5ExpandedBlockStart.gifContractedBlock.gif/**//*函数原型*/
  6None.gifint getRule(int n);
  7None.gifvoid getArray(int* num,int length,int rule);
  8None.gifvoid req1(int n);
  9None.gifvoid req2(int n);
 10None.gifvoid req12(int n);
 11None.gifint getIndex(int n,int rule);
 12None.gifvoid query(int n);
 13None.gifvoid doit(char* s,int num);
 14None.gifvoid disp(int rule);
 15None.gif
 16ExpandedBlockStart.gifContractedBlock.gif/**//*
 17InBlock.gif函数原型:void main()
 18InBlock.gif功能:主函数
 19InBlock.gif参数:无 
 20InBlock.gif返回值:无
 21ExpandedBlockEnd.gif*/

 22ExpandedBlockStart.gifContractedBlock.gifvoid main()dot.gif{
 23ExpandedSubBlockStart.gifContractedSubBlock.gif /**//*clrscr();*/
 24InBlock.gif int num;
 25InBlock.gif char space[20],*s=space;
 26InBlock.gif fflush(stdin);
 27InBlock.gif scanf("%s %d",s,&num);
 28InBlock.gif doit(s,num);
 29ExpandedBlockEnd.gif}

 30None.gif
 31ExpandedBlockStart.gifContractedBlock.gif/**//*
 32InBlock.gif函数原型:int getRule(int n)
 33InBlock.gif功能:取得某数字符合的规则。
 34InBlock.gif参数:要判断的数字n
 35InBlock.gif返回值:整型变量,取值范围为2bit,高位为规则1,低位为规则2。
 36ExpandedBlockEnd.gif*/

 37None.gif
 38ExpandedBlockStart.gifContractedBlock.gifint getRule(int n)dot.gif{
 39ExpandedSubBlockStart.gifContractedSubBlock.gif int r1=0,r2=0,q=0,test[10]=dot.gif{0,0,0,0,0,0,0,0,0,0};
 40ExpandedSubBlockStart.gifContractedSubBlock.gif if(n%7==0)dot.gif{
 41InBlock.gif  r1=1;
 42ExpandedSubBlockEnd.gif }

 43ExpandedSubBlockStart.gifContractedSubBlock.gif dodot.gif{
 44InBlock.gif  q=n%10;
 45InBlock.gif  n=n/10;
 46InBlock.gif  if(q==7)r1=1;
 47ExpandedSubBlockStart.gifContractedSubBlock.gif  /**//*printf("\tq=%d,test[q]=%d\n",q,test[q]);*/
 48InBlock.gif  if(test[q]==1)r2=1;
 49InBlock.gif  test[q]=1;
 50ExpandedSubBlockEnd.gif }
while(n>0);
 51InBlock.gif return ((r1<<1)+r2);
 52ExpandedBlockEnd.gif}

 53None.gif
 54ExpandedBlockStart.gifContractedBlock.gif/**//*
 55InBlock.gif函数原型:void req1(int n)
 56InBlock.gif功能:执行req1命令
 57InBlock.gif参数:要判断的数n
 58InBlock.gif返回值:无
 59ExpandedBlockEnd.gif*/

 60None.gif
 61ExpandedBlockStart.gifContractedBlock.gifvoid req1(int n)dot.gif{
 62InBlock.gif int i,count=0;
 63ExpandedSubBlockStart.gifContractedSubBlock.gif for(i=1;count<n;i++)dot.gif{
 64InBlock.gif  if(getRule(i)!=2)count++;
 65ExpandedSubBlockEnd.gif }

 66InBlock.gif printf("%d\n",--i);
 67ExpandedBlockEnd.gif}

 68ExpandedBlockStart.gifContractedBlock.gif/**//*
 69InBlock.gif函数原型:void req2(int n)
 70InBlock.gif功能:执行req2命令
 71InBlock.gif参数:要判断的数n
 72InBlock.gif返回值:无
 73ExpandedBlockEnd.gif*/

 74None.gif
 75None.gif
 76ExpandedBlockStart.gifContractedBlock.gifvoid req2(int n)dot.gif{
 77InBlock.gif int i,count=0;
 78ExpandedSubBlockStart.gifContractedSubBlock.gif for(i=1;count<n;i++)dot.gif{
 79InBlock.gif  if(getRule(i)!=1)count++;
 80ExpandedSubBlockEnd.gif }

 81InBlock.gif printf("%d\n",--i);
 82ExpandedBlockEnd.gif}

 83ExpandedBlockStart.gifContractedBlock.gif/**//*
 84InBlock.gif函数原型:void req12(int n)
 85InBlock.gif功能:执行req12命令
 86InBlock.gif参数:要判断的数n
 87InBlock.gif返回值:无
 88ExpandedBlockEnd.gif*/

 89None.gif
 90None.gif
 91ExpandedBlockStart.gifContractedBlock.gifvoid req12(int n)dot.gif{
 92InBlock.gif int i,retrule,count=0;
 93ExpandedSubBlockStart.gifContractedSubBlock.gif for(i=1;count<n;i++)dot.gif{
 94InBlock.gif  retrule=getRule(i);
 95InBlock.gif  if(retrule!=2&&retrule!=1)count++;
 96ExpandedSubBlockEnd.gif }

 97InBlock.gif printf("%d\n",--i);
 98ExpandedBlockEnd.gif}

 99ExpandedBlockStart.gifContractedBlock.gif/**//*
100InBlock.gif函数原型:int getIndex(int n,int rule)
101InBlock.gif功能:取得数字n是符合规则rule的第几个元素,如不在数列中返回-1
102InBlock.gif参数:要计算的数字n,规则rule
103InBlock.gif返回值:元素索引,或-1
104ExpandedBlockEnd.gif*/

105None.gif
106ExpandedBlockStart.gifContractedBlock.gifint getIndex(int n,int rule)dot.gif{
107InBlock.gif int i,count=0;
108InBlock.gif if(getRule(n)==rule)
109ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
110InBlock.gif  count=-1;
111ExpandedSubBlockEnd.gif }

112ExpandedSubBlockStart.gifContractedSubBlock.gif elsedot.gif{
113ExpandedSubBlockStart.gifContractedSubBlock.gif  for(i=1;i<=n;i++)dot.gif{
114InBlock.gif    if(getRule(i)!=rule)count++;
115ExpandedSubBlockEnd.gif  }

116ExpandedSubBlockEnd.gif }

117InBlock.gif return count;
118InBlock.gif
119ExpandedBlockEnd.gif}

120ExpandedBlockStart.gifContractedBlock.gif/**//*
121InBlock.gif函数原型:void query(int n)
122InBlock.gif功能:执行query语句
123InBlock.gif参数:要查找的数n
124InBlock.gif返回值:无
125ExpandedBlockEnd.gif*/

126None.gif
127ExpandedBlockStart.gifContractedBlock.gifvoid query(int n)dot.gif{
128InBlock.gif printf("%d %d\n",getIndex(n,2),getIndex(n,1));
129ExpandedBlockEnd.gif}

130ExpandedBlockStart.gifContractedBlock.gif/**//*
131InBlock.gif函数原型:void doit(char* s,int num)
132InBlock.gif功能:执行各种命令 
133InBlock.gif参数:命令s与参数num
134InBlock.gif返回值:无
135ExpandedBlockEnd.gif*/

136None.gif
137ExpandedBlockStart.gifContractedBlock.gifvoid doit(char* s,int num)dot.gif{
138ExpandedSubBlockStart.gifContractedSubBlock.gif if(strcmp(s,"req1")==0)dot.gif{
139InBlock.gif  req1(num);
140InBlock.gif  return;
141ExpandedSubBlockEnd.gif }

142ExpandedSubBlockStart.gifContractedSubBlock.gif if(strcmp(s,"req2")==0)dot.gif{
143InBlock.gif  req2(num);
144InBlock.gif  return;
145ExpandedSubBlockEnd.gif }

146ExpandedSubBlockStart.gifContractedSubBlock.gif if(strcmp(s,"req12")==0)dot.gif{
147InBlock.gif  req12(num);
148InBlock.gif  return;
149ExpandedSubBlockEnd.gif }

150ExpandedSubBlockStart.gifContractedSubBlock.gif if(strcmp(s,"query")==0)dot.gif{
151InBlock.gif  query(num);
152InBlock.gif  return;
153ExpandedSubBlockEnd.gif }

154ExpandedBlockEnd.gif}

155None.gif

转载于:https://www.cnblogs.com/zxsoft/archive/2007/07/19/823272.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值