特殊篮子问题——C语言暴力破解
You are given N baskets of gold coins. The baskets are numbered from 1 to N. In all except one of the baskets, each gold coin weighs w grams. In the one exceptional basket, each gold coin weighs w-d grams. A wizard appears on the scene and takes 1 coin from Basket 1, 2 coins from Basket 2, and so on, up to and including N-1 coins from Basket N-1. He does not take any coins from Basket N. He weighs the selected coins and concludes which of the N baskets contains the lighter coins. Your mission is to emulate the wizard's computation. Input The input file will consist of one or more lines; each line will contain data for one instance of the problem. More specifically, each line will contain four positive integers, separated by one blank space. The first three integers are, respectively, the numbers N, w, and d, as described above. The fourth integer is the result of weighing the selected coins. N will be at least 2 and not more than 8000. The value of w will be at most 30. The value of d will be less than w. Output For each instance of the problem, your program will produce one line of output, consisting of one positive integer: the number of the basket that contains lighter coins than the other baskets. Sample Input 10 25 8 1109 10 25 8 1045 8000 30 12 959879400 Sample Output 2 10 50
翻译:
你得到N篮金币。篮子的编号从1到n,除了一个篮子外,每个金币重w克。在一个特殊的篮子里,每枚金币重量为w-d克。一个巫师出现在场景中,从篮子1中取出1枚硬币,从篮子2中取出2枚硬币,以此类推,直到并包括从篮子N-1中取出N-1枚硬币。他不会从篮子里拿任何硬币。他会称出所选的硬币,并得出结论:在N个篮子里,哪一个篮子里的硬币较轻。您的任务是模拟向导的计算。
输入
输入文件将由一行或多行组成;每行将包含问题的一个实例的数据。更具体地说,每行将包含四个正整数,由一个空格分隔。前三个整数分别是数字N、w和d,如上所述。第四个整数是称重所选硬币的结果。
N至少是2,不超过8000。w的值最多为30。d的值小于w。
输出
对于问题的每个实例,您的程序将生成一行输出,其中包含一个正整数:包含比其他篮子更轻硬币的篮子的数量。
样例输入
10 25 8 1109
10 25 8 1045
8000 30 12 9599400
样例输出
2
10
50
思路:暴力破解,按顺序从1到N-1,假设它是特殊的篮子,进行求和,如果求和结果跟输入的相同,就说明假设成立,如果假设都不成立,因为没考虑最后一个(N),说明N是特殊的篮子,另外,N<8000,涉及到大数加法,这里是用char*字符串进行模拟,用string操作简单一些
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char* trans(char a[]) { //字符串倒叙
char ch;
for (int i = 0; i < strlen(a)/2; i++) {
ch = a[i];
a[i] = a[strlen(a) - i - 1];
a[strlen(a) - i - 1] = ch;
}
return a;
}
char* add(char a[], char b[]) { //把2个字符串相加
while (strlen(a) != strlen(b)) {
int al = strlen(a), bl = strlen(b);
if (al > bl)
b[bl] = '0';
else
a[al] = '0';
a[al+1] = b[bl+1] = '\0';
}
a[strlen(a)]=b[strlen(b)] = '\0';
int up = 0, sum; //up储存进位
char c[1000];
int i = 0;
for ( ; i < strlen(a); i++) { //使用字符串模拟大数加法
sum = a[i] - '0' + b[i] - '0' + up;
up = sum / 10;
sum %= 10;
c[i] = char(sum+'0');
c[i + 1] = '\0';
}
if (up > 0) {
c[i] = up + '0';
c[i + 1] = '\0';
}
return c;
}
int main(){
int N, w, d;
char sum[1000]="0", result[1000];
int flag; //用于记录是否有假设成立
char tmp[1000];
while (1) {
flag = 1;
scanf("%d%d%d%s", &N, &w, &d, &result);
int i = 1;
for (; i < N; i++) { //假设第i个篮子是特殊的
strcpy(sum, "0"); //重置sum
for (int j = 1; j < N; j++) {
if (i == j)
itoa((w - d)*j, tmp, 10);
else
itoa(w*j, tmp, 10);
strcpy(sum, trans(add(trans(sum), trans(tmp))));
}
if (strcmp(sum, result) == 0) {
flag = 0;
printf("%d\n", i);
break;
}
}
if (flag)
printf("%d\n", N);
}
return 0;
}
特殊篮子问题——C语言暴力破解的更多相关文章
- 利用Python自动生成暴力破解的字典
Python是一款非常强大的语言.用于测试时它非常有效,因此Python越来越受到欢迎. 因此,在此次教程中我将聊一聊如何在Python中生成字典,并将它用于任何你想要的用途. 前提要求 1,Pyth ...
- 防止服务器被暴力破解使用DenyHosts
公司有台服务器被坏人盯上了,通过日志可以看到一直在做暴力破解ssh. 防止服务器被暴力破解使用DenyHosts 参考链接: 防止ssh破解,Ubuntu安装denyhosts的一些问题 防止你的ss ...
- 安天透过北美DDoS事件解读IoT设备安全——Mirai的主要感染对象是linux物联网设备,包括:路由器、网络摄像头、DVR设备,入侵主要通过telnet端口进行流行密码档暴力破解,或默认密码登陆,下载DDoS功能的bot,运行控制物联网设备
安天透过北美DDoS事件解读IoT设备安全 安天安全研究与应急处理中心(安天CERT)在北京时间10月22日下午启动高等级分析流程,针对美国东海岸DNS服务商Dyn遭遇DDoS攻击事件进行了跟进分析. ...
- Linux 利用hosts.deny 防止暴力破解ssh
一.ssh暴力破解 利用专业的破解程序,配合密码字典.登陆用户名,尝试登陆服务器,来进行破解密码,此方法,虽慢,但却很有效果. 二.暴力破解演示 2.1.基础环境:2台linux主机(centos 7 ...
- Python 黑客 --- 002 入门级 ZIP压缩文件口令暴力破解机
Python 黑客 入门级实战:ZIP压缩文件口令暴力破解机 使用的系统:Ubuntu 14.04 LTS Python语言版本:Python 2.7.10 V 编写zip 压缩文件口令破解器需要使用 ...
- 防止ssh暴力破解的小工具denyhosts
DenyHosts 简介 DenyHosts 是 Python 语言写的一个程序软件,运行于 Linux 上预防 SSH 暴力破解的,它会分析 sshd 的日志文件(/var/log/secure), ...
- DenyHosts 安全限制ssh防暴力破解
DenyHosts是Python语言写的一个程序,它会分析sshd的日志文件(/var/log/secure),当发现重 复的攻击时就会记录IP到/etc/hosts.deny文件,从而达到自动屏IP ...
- SSH密码暴力破解及防御实战
SSH密码暴力破解及防御实战 一.Hydra(海德拉) 1.1 指定用户破解 二.Medusa(美杜莎) 2.1 语法参数 2.2 破解SSH密码 三.Patator 3.1 破解SSH密码 四.Br ...
- 开源服务专题之------ssh防止暴力破解及fail2ban的使用方法
15年出现的JAVA反序列化漏洞,另一个是redis配置不当导致机器入侵.只要redis是用root启动的并且未授权的话,就可以通过set方式直接写入一个authorized_keys到系统的/roo ...
随机推荐
- react的注意点
1.import Form from '../pages/form/view' 与 import Form from '../pages/form/container'的区别: 前者只是引入view. ...
- 串口发送端verilog代码分析
串口发送端verilog代码分析 `timescale 1ns / 1ps ////////////////////////////////////////////////////////////// ...
- Java环境配置之JDK安装
一.下载 现在JDK的版本很多.我下载的是jdk1.7 以下链接是jdk1.8的 http://www.oracle.com/technetwork/java/javase/downloads/jdk ...
- 算法实践--不相交集合(Disjoint Sets)
什么是不相交集合(Disjoint Sets) 是这样的一组set,任何元素最多只能在一个set中 至少支持查找Find和合并Union操作 实现方式(基于树) 每个set都是一棵树 每棵树都由树的根 ...
- python 使用ElementTree解析xml
以country.xml为例,内容如下: <?xml version="1.0"?> <data> <country name="Liech ...
- Cglib源码分析 invoke和invokeSuper的差别(转)
原文 https://blog.csdn.net/makecontral/article/details/79593732 Cglib的实例 本文重在源码的分析,Cglib的使用不再复述. //被代理 ...
- windwos下基于exp的提权
00x1: 本文是基于windwos 补丁的漏洞提权. 其实是一件很简单的工作:如果看成数据库的3张表分别是: 未打补丁表, 补丁漏洞表, 漏洞利用提权程序表. 为一一对应关系,所以就是资源的掌握,以 ...
- Vue todolist练习 知识点
1.localStorage的用法总结 (1).这儿是什么:局部存储器.它是html5新增的一个本地存储API,所谓localStorage就是一个小仓库的意思,它有5M的大小空间,存储在浏览器中,我 ...
- 转: spring静态注入
与其说是静态注入(IOC),不如讲是对JavaBean 的静态成员变量进行赋值. 一般我们在使用依赖注入的时候,如果当前对象(javaBean )创建(实例化)一次,那么非静态的成员变量也会实例化一次 ...
- scrollview嵌套recyclerview显示不全现象
只需在recyclerview的外层加入一个父布局就好了 <RelativeLayout android:layout_width="match_parent" androi ...