特殊篮子问题——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 ...
随机推荐
- linux上安装memcached步骤
libevent: http://libevent.org/ 服务器端:https://code.google.com/archive/p/memcached/downloads 客户端: http: ...
- 让所有浏览器支持HTML5 video视频标签
HTML5究竟需要多少种视频编码格式 当前,video 元素支持三种视频格式:Ogg = 带有 Theora 视频编码和 Vorbis 音频编码的 Ogg 文件MPEG4 = 带有 H.264 视频编 ...
- bootstrap之navbar
container:固定960px宽度,(如果又引入了响应式样式,则会适当调整,例如1600*900,它会显示1200px) container-fluid:自适应屏幕宽度,即满屏显示. row和co ...
- Python判断语句
什么是判断语句 如果 今天是周六或者周日: 约妹子 如果 今天是情人节: 买玫瑰 如果 今天发工资: 先还信用卡的钱 如果 有剩余: 又可以happy了,O(∩_∩)O哈哈~ 否则: 啊啊啊啊,等30 ...
- 为嵌入式mplayer移植添加ALSA音频驱动(全志V3s荔枝派zero)
首先准备mplayer和alsa_lib,我的是bulidroot添加后编译自动下载的,版本分别是alsa-lib-1.1.4.1和mplayer-1.3.0. 首先编译alsa_lib: ./con ...
- 阿里四不像Fourinone
阿里四不像——分布式核心技术框架 Fourinone https://blog.csdn.net/shero_zsmj/article/details/52277194
- 3.STM32复位系统
一.概念 复位: 使系统结束当前运行状态,重新开始运行,并根据复位种类,将系统的寄存器(特定的寄存器除外)恢复到默认状态. 二.复位的种类 1.系统复位 将除了系统后备区域寄存器(BKP)和时钟控制寄 ...
- Oracle 关于concat与双竖线用法的补充
--只能连接2个字符串select concat('nod',' chen is ') from dual; --连接2个列名select concat(name,ip2) from vm_info; ...
- JDBC 中preparedStatement和Statement区别
一.概念 PreparedStatement是用来执行SQL查询语句的API之一,Java提供了 Statement.PreparedStatement 和 CallableStatement三种方式 ...
- Avalon Master 外设