特殊篮子问题——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;
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值