今天查到了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分。
1/**//*包含头文件*/
2#include <stdio.h>
3#include <string.h>
4
5/**//*函数原型*/
6int getRule(int n);
7void getArray(int* num,int length,int rule);
8void req1(int n);
9void req2(int n);
10void req12(int n);
11int getIndex(int n,int rule);
12void query(int n);
13void doit(char* s,int num);
14void disp(int rule);
15
16/**//*
17函数原型:void main()
18功能:主函数
19参数:无
20返回值:无
21*/
22void main(){
23 /**//*clrscr();*/
24 int num;
25 char space[20],*s=space;
26 fflush(stdin);
27 scanf("%s %d",s,&num);
28 doit(s,num);
29}
30
31/**//*
32函数原型:int getRule(int n)
33功能:取得某数字符合的规则。
34参数:要判断的数字n
35返回值:整型变量,取值范围为2bit,高位为规则1,低位为规则2。
36*/
37
38int getRule(int n){
39 int r1=0,r2=0,q=0,test[10]={0,0,0,0,0,0,0,0,0,0};
40 if(n%7==0){
41 r1=1;
42 }
43 do{
44 q=n%10;
45 n=n/10;
46 if(q==7)r1=1;
47 /**//*printf("\tq=%d,test[q]=%d\n",q,test[q]);*/
48 if(test[q]==1)r2=1;
49 test[q]=1;
50 }while(n>0);
51 return ((r1<<1)+r2);
52}
53
54/**//*
55函数原型:void req1(int n)
56功能:执行req1命令
57参数:要判断的数n
58返回值:无
59*/
60
61void req1(int n){
62 int i,count=0;
63 for(i=1;count<n;i++){
64 if(getRule(i)!=2)count++;
65 }
66 printf("%d\n",--i);
67}
68/**//*
69函数原型:void req2(int n)
70功能:执行req2命令
71参数:要判断的数n
72返回值:无
73*/
74
75
76void req2(int n){
77 int i,count=0;
78 for(i=1;count<n;i++){
79 if(getRule(i)!=1)count++;
80 }
81 printf("%d\n",--i);
82}
83/**//*
84函数原型:void req12(int n)
85功能:执行req12命令
86参数:要判断的数n
87返回值:无
88*/
89
90
91void req12(int n){
92 int i,retrule,count=0;
93 for(i=1;count<n;i++){
94 retrule=getRule(i);
95 if(retrule!=2&&retrule!=1)count++;
96 }
97 printf("%d\n",--i);
98}
99/**//*
100函数原型:int getIndex(int n,int rule)
101功能:取得数字n是符合规则rule的第几个元素,如不在数列中返回-1
102参数:要计算的数字n,规则rule
103返回值:元素索引,或-1
104*/
105
106int getIndex(int n,int rule){
107 int i,count=0;
108 if(getRule(n)==rule)
109 {
110 count=-1;
111 }
112 else{
113 for(i=1;i<=n;i++){
114 if(getRule(i)!=rule)count++;
115 }
116 }
117 return count;
118
119}
120/**//*
121函数原型:void query(int n)
122功能:执行query语句
123参数:要查找的数n
124返回值:无
125*/
126
127void query(int n){
128 printf("%d %d\n",getIndex(n,2),getIndex(n,1));
129}
130/**//*
131函数原型:void doit(char* s,int num)
132功能:执行各种命令
133参数:命令s与参数num
134返回值:无
135*/
136
137void doit(char* s,int num){
138 if(strcmp(s,"req1")==0){
139 req1(num);
140 return;
141 }
142 if(strcmp(s,"req2")==0){
143 req2(num);
144 return;
145 }
146 if(strcmp(s,"req12")==0){
147 req12(num);
148 return;
149 }
150 if(strcmp(s,"query")==0){
151 query(num);
152 return;
153 }
154}
155