UASCO Combination Lock 解析 and C 语言实现

来源:互联网 发布:七友软件 编辑:程序博客网 时间:2024/05/29 10:09

Combination Lock

Farmer John's cows keep escaping from his farm and causing mischief. To try and prevent them from leaving, he purchases a fancy combination lock to keep his cows from opening the pasture gate.

Knowing that his cows are quite clever, Farmer John wants to make sure they cannot easily open the lock by simply trying many different combinations. The lock has three dials, each numbered 1..N (1 <= N <= 100), where 1 and N are adjacent since the dials are circular. There are two combinations that open the lock, one set by Farmer John, and also a "master" combination set by the lock maker.

The lock has a small tolerance for error, however, so it will open even if the numbers on the dials are each within at most 2 positions of a valid combination.

For example, if Farmer John's combination is (1,2,3) and the master combination is (4,5,6), the lock will open if its dials are set to (1,3,5) (since this is close enough to Farmer John's combination) or to (2,4,8) (since this is close enough to the master combination). Note that (1,5,6) would not open the lock, since it is not close enough to any one single combination.

Given Farmer John's combination and the master combination, please determine the number of distinct settings for the dials that will open the lock. Order matters, so the setting (1,2,3) is distinct from (3,2,1).

PROGRAM NAME: combo

INPUT FORMAT:

Line 1:

The integer N.

 

 

Line 2:

Three space-separated integers, specifying Farmer John's combination.

Line 3:

Three space-separated integers, specifying the master combination (possibly the same as Farmer John's combination).

SAMPLE INPUT (file combo.in):

501 2 35 6 7

INPUT DETAILS:

Each dial is numbered 1..50. Farmer John's combination is (1,2,3), and the master combination is (5,6,7).

OUTPUT FORMAT:

Line 1:

The number of distinct dial settings that will open the lock.

SAMPLE OUTPUT (file combo.out):

249

 

 

John的牛总是逃离farm,故John买了密码锁锁住了牧场大门。密码锁有3个拨号键,其上的数字为11<=N<=100),且1N是相邻的,因为拨号键是圆形的。锁有两个可以打开的数字组合,一个是John设置的,一个是生产商设定的。

这把锁的质量很差,当每个数字键上的数字与正确组合中相对应的数字,相邻的位置不超过2时,就可以被打开。

例如:

正确组合:(1,2,3)     那么组合(1,3,5)也能打开。

 


 

第一个数字差第二个数字差第三个数字差2  相差的位置均小于等于2

 


给定N,和两个密码组合,求能够打开的该锁的组合数目。

 

这道题典型的暴力破解,(密码学中常用)

 

因为N转完了,就是1,定义:

正向距离:a - b      (a >= b)

反向距离:N - (a - b)  (a >= b)


/*ID: abc18711LANG: CTASK: combo*/#include <stdio.h>#include <stdlib.h>#include <math.h>int N;struct combiatn  //correct combination{int a;int b;int c;};int distn(int a,int b){if(abs(a-b) <= 2){return 1;}if((N - abs(a-b)) <= 2){return 1;}return 0;}int check(struct combiatn a,struct combiatn b){int num = 0;if(distn(a.a,b.a) && distn(a.b,b.b) && distn(a.c,b.c)){return 1;}return 0;}int main(){FILE *fin,*fout;struct combiatn a;struct combiatn b;struct combiatn user;int i,j,k;int flag;int num = 0;fin = fopen("combo.in","r");fout = fopen("combo.out","w");fscanf(fin,"%d",&N);fscanf(fin,"%d %d %d",&a.a,&a.b,&a.c);fscanf(fin,"%d %d %d",&b.a,&b.b,&b.c);for(i = 1;i <= N ;i ++)for(j = 1;j <= N ;j ++)for(k = 1;k <= N;k ++){flag = 0;user.a = i;user.b = j;user.c = k;if(check(a,user))flag = 1;if(check(b,user))flag = 1;if(flag)num ++;}fscanf(fout,"%d\n",num);return 0;}


0 0
原创粉丝点击