疯狂游戏 3.22笔试

总的来说还不算难,第一次ak,希望能给个面试机会。

第一题:给你一个乘法表达式,让你求值,例如1000*15*55555

这题思路不算难,唯一问题在于大数,由于c++没有成品大数,我直接上Python

1 a=input()
2 list=a.split("*")
3 ans=1
4 for i in list:
5     ans=ans*(int(i))
6 print(ans)

第二题:要求面值为n的最少硬币数,且只有1,4,5三种面值,问你最少多少个硬币,并且要求在最少硬币的情况下硬币面值尽量大。

思路直接详见代码,直接分类讨论即可,比较明了。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using  namespace std;
 5 typedef long long ll;
 6 int main()
 7 {
 8     ll n;
 9     while (scanf("%lld",&n)!=EOF)
10     {
11         if(n%5==0)
12             printf("0,0,%lld\n",n/5);
13         else if(n%5==1)
14         {
15             if(n>=16)
16             printf("0,4,%lld\n",(n/5)-3);
17             else if(n==11)
18                 printf("1,0,2\n");
19             else if(n==6)
20                 printf("1,0,1\n");
21             else
22                 printf("1,0,0\n");
23         }
24         else if(n%5==2)
25         {
26             if(n>=12)
27                 printf("0,3,%lld\n",(n/5)-2);
28             else if(n==2)
29             {
30                 printf("2,0,0\n");
31             }
32             else
33             {
34                 printf("2,0,1\n");
35             }
36         }
37         else if(n%5==3)
38         {
39             if(n>=8)
40                 printf("0,2,%lld\n",(n/5)-1);
41             else
42             {
43                 printf("3,0,0\n");
44             }
45         }
46         else if(n%5==4)
47         {
48             printf("0,1,%lld\n",n/5);
49         }
50     }
51 }

第三题:给你一个有向图,再给你源点集和终点集(即这些点不是唯一的),问你所有从源点到终点的路径上可以经过的点,由小到大输出。

除了处理输入之外,基本没有什么特别麻烦的。

思路:对源点做一遍bfs,打一下标记,即源点可以到达的点,然后反向建图(正反一起建比较方便),对终点进行一遍bfs,两者相交部分即可以经过的点。

  1 #include <iostream>
  2 #include <cstring>
  3 #include <cstdio>
  4 #include <queue>
  5 using namespace std;
  6 struct edge
  7 {
  8    int to;
  9    int next;
 10 }e[2][30005];
 11 int tol;
 12 int head[2][30005];
 13 bool vis[2][10005];
 14 inline void add(int x,int y)
 15 {
 16     e[0][tol].to=y;
 17     e[1][tol].to=x;
 18     e[0][tol].next=head[0][x];
 19     e[1][tol].next=head[1][y];
 20     head[0][x]=tol;
 21     head[1][y]=tol++;
 22 }
 23 int ss[10005];
 24 int ee[10005];
 25 int sscnt;
 26 int eecnt;
 27 int n,m;
 28 void init()
 29 {
 30     sscnt=eecnt=0;
 31     tol=0;
 32     memset(head,-1,sizeof(head));
 33     memset(vis,false, sizeof(vis));
 34 }
 35 char a[500005];
 36 int num[100005];
 37 void bfs(int x)
 38 {
 39     queue<int>que;
 40     int i=0;
 41     //printf("x=%d\n",x);
 42     if(x==0)
 43     {
 44         for(i=0;i<sscnt;i++)
 45         {
 46             que.push(ss[i]);
 47             vis[x][ss[i]]=true;
 48         }
 49     }
 50     else
 51     {
 52         for(i=0;i<eecnt;i++)
 53         {
 54             que.push(ee[i]);
 55             vis[x][ee[i]]=true;
 56         }
 57     }
 58     while (!que.empty())
 59     {
 60         int u=que.front();
 61         que.pop();
 62    //     printf("u=%d\n",u);
 63         for(int i=head[x][u];i!=-1;i=e[x][i].next)
 64         {
 65             int v=e[x][i].to;
 66             if(!vis[x][v])
 67             {
 68                 vis[x][v]=true;
 69                 que.push(v);
 70             }
 71         }
 72     }
 73 }
 74 int main()
 75 {
 76     int cnt=0;
 77     int x,y;
 78     int len;
 79     int n=0;
 80     int i,j;
 81     int xx;
 82    while (scanf("%s",a)!=EOF)
 83    {
 84        cnt=0;
 85        len=strlen(a);
 86        i=0;
 87        init();
 88        while (i<len&&a[i]!='|')
 89        {
 90            if(a[i]==','||a[i]==';')
 91            i++;
 92            else
 93            {
 94                xx=0;
 95                while (a[i]>='0'&&a[i]<='9')
 96                {
 97                    xx=xx*10+a[i]-'0';
 98                    i++;
 99                }
100                num[cnt++]=xx;
101            }
102        }
103        for(j=0;j<cnt;j+=2)
104            add(num[j],num[j+1]);
105        /*for(j=0;j<cnt;j++)
106            printf("%d ",num[j]);
107        printf("\n");*/
108        i++;
109        while (i<len&&a[i]!='|')
110        {
111            if(a[i]==','||a[i]==';')
112                i++;
113            else
114            {
115                xx=0;
116                while (a[i]>='0'&&a[i]<='9')
117                {
118                    xx=xx*10+a[i]-'0';
119                    i++;
120                }
121                ss[sscnt++]=xx;
122            }
123        }
124        /*for(j=0;j<sscnt;j++)
125            printf("%d ",ss[j]);
126        printf("\n");*/
127        i++;
128        while (i<len&&a[i]!='|')
129        {
130            if(a[i]==','||a[i]==';')
131                i++;
132            else
133            {
134                xx=0;
135                while (a[i]>='0'&&a[i]<='9')
136                {
137                    xx=xx*10+a[i]-'0';
138                    i++;
139                }
140                ee[eecnt++]=xx;
141            }
142        }
143        bfs(0);
144        bfs(1);
145        cnt=0;
146        for(i=0;i<10005;i++)
147        {
148            if(vis[0][i]&&vis[1][i])
149                num[cnt++]=i;
150        }
151        for(i=0;i<cnt-1;i++)
152            printf("%d,",num[i]);
153        printf("%d\n",num[i]);
154        /*for(j=0;j<eecnt;j++)
155            printf("%d ",ee[j]);
156        printf("\n");*/
157    }
158 }

猜你喜欢

转载自www.cnblogs.com/Carits/p/12548416.html