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语言暴力破解的更多相关文章

  1. 利用Python自动生成暴力破解的字典

    Python是一款非常强大的语言.用于测试时它非常有效,因此Python越来越受到欢迎. 因此,在此次教程中我将聊一聊如何在Python中生成字典,并将它用于任何你想要的用途. 前提要求 1,Pyth ...

  2. 防止服务器被暴力破解使用DenyHosts

    公司有台服务器被坏人盯上了,通过日志可以看到一直在做暴力破解ssh. 防止服务器被暴力破解使用DenyHosts 参考链接: 防止ssh破解,Ubuntu安装denyhosts的一些问题 防止你的ss ...

  3. 安天透过北美DDoS事件解读IoT设备安全——Mirai的主要感染对象是linux物联网设备,包括:路由器、网络摄像头、DVR设备,入侵主要通过telnet端口进行流行密码档暴力破解,或默认密码登陆,下载DDoS功能的bot,运行控制物联网设备

    安天透过北美DDoS事件解读IoT设备安全 安天安全研究与应急处理中心(安天CERT)在北京时间10月22日下午启动高等级分析流程,针对美国东海岸DNS服务商Dyn遭遇DDoS攻击事件进行了跟进分析. ...

  4. Linux 利用hosts.deny 防止暴力破解ssh

    一.ssh暴力破解 利用专业的破解程序,配合密码字典.登陆用户名,尝试登陆服务器,来进行破解密码,此方法,虽慢,但却很有效果. 二.暴力破解演示 2.1.基础环境:2台linux主机(centos 7 ...

  5. Python 黑客 --- 002 入门级 ZIP压缩文件口令暴力破解机

    Python 黑客 入门级实战:ZIP压缩文件口令暴力破解机 使用的系统:Ubuntu 14.04 LTS Python语言版本:Python 2.7.10 V 编写zip 压缩文件口令破解器需要使用 ...

  6. 防止ssh暴力破解的小工具denyhosts

    DenyHosts 简介 DenyHosts 是 Python 语言写的一个程序软件,运行于 Linux 上预防 SSH 暴力破解的,它会分析 sshd 的日志文件(/var/log/secure), ...

  7. DenyHosts 安全限制ssh防暴力破解

    DenyHosts是Python语言写的一个程序,它会分析sshd的日志文件(/var/log/secure),当发现重 复的攻击时就会记录IP到/etc/hosts.deny文件,从而达到自动屏IP ...

  8. SSH密码暴力破解及防御实战

    SSH密码暴力破解及防御实战 一.Hydra(海德拉) 1.1 指定用户破解 二.Medusa(美杜莎) 2.1 语法参数 2.2 破解SSH密码 三.Patator 3.1 破解SSH密码 四.Br ...

  9. 开源服务专题之------ssh防止暴力破解及fail2ban的使用方法

    15年出现的JAVA反序列化漏洞,另一个是redis配置不当导致机器入侵.只要redis是用root启动的并且未授权的话,就可以通过set方式直接写入一个authorized_keys到系统的/roo ...

随机推荐

  1. vs2015 加载项目的时启动:无法启动 IIS Express Web 服务器

    使用Visual Studio 2015 运行ASP.NET项目时,提示“无法启动IIS Express Web服务器”,无法运行,如图: 一般出现在重装系统之后,或者项目是从别的电脑上复制过来的.解 ...

  2. Java遍历Map对象的方式

    public static void main(String[] args) { HashMap<String, String> testMap = new HashMap<> ...

  3. python3学习笔记五(列表2)

    参考http://www.runoob.com/python3/python3-list.html 嵌套列表 a = ['a','b','c']b = [1,2,3]x = [a, b]print(x ...

  4. 如何从 VSS 迁移到Team Foundation Server

    TFS 2012自带了vss upgrade wizard工具, 在Team Foundation Server管理控制台左侧的最后一个菜单其他工具和组件中,选择Visual SourceSafe升级 ...

  5. 滑雪(dp)

    问题 H: [例9.24]滑雪 时间限制: 1 Sec  内存限制: 128 MB提交: 21  解决: 13 题目描述 小明喜欢滑雪,因为滑雪的确很刺激,可是为了获得速度,滑的区域必须向下倾斜,当小 ...

  6. yum安装下的nginx,如何添加模块,和添加第三方模块

    需求:生产有个接口是通过socket通信.nginx1.9开始支持tcp层的转发,通过stream实现的,而socket也是基于tcp通信. 实现方法:Centos7.2下yum直接安装的nginx, ...

  7. 开源ERP系统Odoo搭建文档

    本文参考链接地址: https://devecho.com/v/209/ https://www.cnblogs.com/wxylog/p/6386974.html 什么是Odoo 为什么选择Odoo ...

  8. Northwind学习笔记

    一.单表查询 --1.查询订购日期在1996年7月1日至1996年7月15日之间的订单的订购日期.订单ID.客户ID和雇员ID等字段的值 SELECT OrderID , CustomerID , E ...

  9. 通过eclipse打开jdk native源码

    1.下载 eclipse http://www.eclipse.org/downloads/eclipse-packages/ 建议下载 Eclipse IDE for Eclipse Committ ...

  10. 在线安装TIDB集群

     在线安装TiDB集群 服务器准备 说明:TiDB8需要能够连接外网,以便下载各类安装包 TiDB4非必须,但最好是有一台,因为后续测试Mysql数据同步或者进行性能比较时,都要用到 TiKV最好是采 ...