A1048. Find Coins

Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for each bill, she could only use exactly two coins to pay the exact amount. Since she has as many as 105 coins with her, she definitely needs your help. You are supposed to tell her, for any given amount of money, whether or not she can find two coins to pay for it.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive numbers: N (<=105, the total number of coins) and M(<=103, the amount of money Eva has to pay). The second line contains N face values of the coins, which are all positive numbers no more than 500. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the two face values V1 and V2 (separated by a space) such that V1 + V2 = M and V1 <= V2. If such a solution is not unique, output the one with the smallest V1. If there is no solution, output “No Solution” instead.

Sample Input 1:
8 15
1 2 8 7 2 4 11 15
Sample Output 1:
4 11
Sample Input 2:
7 14
1 8 7 2 4 11 15
Sample Output 2:
No Solution

方法一,散列法:

#include <cstdio>

const int maxn = 100010;
int hashTable[maxn] = {0};

int main() {
    int n, m, a;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++) {
        scanf("%d", &a);
        hashTable[a]++;
    }
    for (int i = 0; i < maxn; i++) {
        if (hashTable[i] && hashTable[m - i]) {
            if (i == m - i && hashTable[i] <= 1) {
                continue;
            }
            printf("%d %d\n", i, m - i);
            return 0;
        }
    }
    printf("No Solution\n");
    return 0;
}

方法二,two pointers:

#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 100010;

void twoPointers(int a[], int n, int m) {
    int i = 0, j = n - 1;
    while (i < j) {
        if (a[i] + a[j] == m) {
            break;
        } else if (a[i] + a[j] < m) {
            i++;
        } else {
            j--;
        }
    }
    if (i < j) {
        printf("%d %d\n", a[i], a[j]);
    } else {
        printf("No Solution\n");
    }
}

int main() {
    int n, m;
    int a[maxn];
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    sort(a, a + n);
    twoPointers(a, n, m);
    return 0;
}
Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are true silver dollars; one coin is counterfeit even though its color and size make it indistinguishable from the real silver dollars. The counterfeit coin has a different weight from the other coins but Sally does not know if it is heavier or lighter than the real coins. Happily, Sally has a friend who loans her a very accurate balance scale. The friend will permit Sally three weighings to find the counterfeit coin. For instance, if Sally weighs two coins against each other and the scales balance then she knows these two coins are true. Now if Sally weighs one of the true coins against a third coin and the scales do not balance then Sally knows the third coin is counterfeit and she can tell whether it is light or heavy depending on whether the balance on which it is placed goes up or down, respectively. By choosing her weighings carefully, Sally is able to ensure that she will find the counterfeit coin with exactly three weighings. 输入 The first line of input is an integer n (n > 0) specifying the number of cases to follow. Each case consists of three lines of input, one for each weighing. Sally has identified each of the coins with the letters A--L. Information on a weighing will be given by two strings of letters and then one of the words ``up'', ``down'', or ``even''. The first string of letters will represent the coins on the left balance; the second string, the coins on the right balance. (Sally will always place the same number of coins on the right balance as on the left balance.) The word in the third position will tell whether the right side of the balance goes up, down, or remains even. 输出 For each case, the output will identify the counterfeit coin by its letter and tell whether it is heavy or light. The solution will always be uniquely determined. 样例输入 1 ABCD EFGH even ABCI EFJK up ABIJ EFGH even 样例输出 K is the counterfeit coin and it is light.用c语音写
最新发布
04-03
### 找到假币并判断其轻重的C语言实现 为了通过天平称重三次找到假币并判断其轻重,可以基于分治法的思想设计算法。以下是完整的解决方案: #### 程序逻辑描述 1. 将硬币分为三组,每组尽可能平均分配。 2. 对前两组进行称重比较: - 如果重量相等,则假币位于第三组; - 如果重量不等,则假币位于较轻的那一组[^3]。 3. 针对含有假币的一组重复上述过程,直到缩小范围至单个硬币。 此方法的时间复杂度为 \(O(\log_3 n)\),因为每次都将问题规模减少为原来的三分之一[^1]。 下面是具体的 C 语言实现代码: ```c #include <stdio.h> // 定义硬币数量和假币位置(用于模拟) #define COIN_COUNT 9 int fakeCoinIndex = 7; // 假设第8号硬币是假币(索引从0开始) // 称重函数:返回值表示左侧、右侧或平衡状态 int weigh(int start, int end) { int mid = (start + end) / 2; int leftWeight = 0, rightWeight = 0; // 计算左半部分重量 for (int i = start; i <= mid; i++) { if (i != fakeCoinIndex) { // 正常硬币 leftWeight += 1; } else { // 假币更轻 leftWeight += 0.5; } } // 计算右半部分重量 for (int i = mid + 1; i <= end; i++) { if (i != fakeCoinIndex) { // 正常硬币 rightWeight += 1; } else { // 假币更轻 rightWeight += 0.5; } } if (leftWeight > rightWeight) return -1; // 左侧更重 if (leftWeight < rightWeight) return 1; // 右侧更重 return 0; // 平衡 } // 查找假币的递归函数 void findFakeCoin(int start, int end) { if (start == end) { printf("假币是第%d号硬币。\n", start + 1); return; } int result = weigh(start, end); if (result == 0) { printf("当前区间[%d,%d]内的硬币都是真的。\n", start + 1, end + 1); return; } int mid = (start + end) / 2; if (result == 1) { // 右侧更轻 findFakeCoin(mid + 1, end); } else if (result == -1) { // 左侧更轻 findFakeCoin(start, mid); } } int main() { printf("共有%d个硬币,其中一个是假币且较轻。\n", COIN_COUNT); findFakeCoin(0, COIN_COUNT - 1); // 调用查找假币函数 return 0; } ``` #### 关键点解释 - **`weigh` 函数**:模拟天平称重的过程,计算两侧硬币总重量,并返回比较结果。 - **`findFakeCoin` 函数**:利用分治思想递归调用,逐步缩小可能包含假币的范围,最终定位假币所在位置。 - **时间复杂度**:由于每次都减少了三分之二的可能性,因此整个算法的时间复杂度接近于 \(O(\log_3 n)\)。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值