【九度】题目1486:False coin

108 篇文章 0 订阅
102 篇文章 5 订阅
题目地址:http://ac.jobdu.com/problem.php?pid=1486
题目描述:

The "Gold Bar"bank received information from reliable sources that in their last group of N coins exactly one coin is false and differs in weight from other coins (while all other coins are equal in weight). After the economic crisis they have only a simple balance available (like one in the picture). Using this balance, one is able to determine if the weight of objects in the left pan is less than, greater than, or equal to the weight of objects in the right pan.
In order to detect the false coin the bank employees numbered all coins by the integers from 1 to N, thus assigning each coin a unique integer identifier. After that they began to weight various groups of coins by placing equal numbers of coins in the left pan and in the right pan. The identifiers of coins and the results of the weightings were carefully recorded.
You are to write a program that will help the bank employees to determine the identifier of the false coin using the results of these weightings.

输入:

The first line of the input file contains two integers N and K, separated by spaces, where N is the number of coins (2<=N<=1000 ) and K is the number of weightings fulfilled (1<=K<=100). The following 2K lines describe all weightings. Two consecutive lines describe each weighting. The first of them starts with a number Pi (1<=Pi<=N/2), representing the number of coins placed in the left and in the right pans, followed by Pi identifiers of coins placed in the left pan and Pi identifiers of coins placed in the right pan. All numbers are separated by spaces. The second line contains one of the following characters: '<', '>', or '='. It represents the result of the weighting:
'<' means that the weight of coins in the left pan is less than the weight of coins in the right pan,
'>' means that the weight of coins in the left pan is greater than the weight of coins in the right pan,
'=' means that the weight of coins in the left pan is equal to the weight of coins in the right pan.

输出:

Write to the output file the identifier of the false coin or 0, if it cannot be found by the results of the given weightings.

样例输入:
5 3
2 1 2 3 4
<
1 1 4
=
1 2 5
=
样例输出:
3
来源:
2012年北京大学计算机研究生机试真题
区分假币的问题,和题目1150:Counterfeit Dollar思路一样。
北大真喜欢出类似的题目。
这两个题都只有一个假币。
1、如果出现在等号两边,说明一定为真币。
2、如果出现在大于或者小于号,相应的值递加或者递减。
3、如果某个值最后的结果和不等号出现的次数相等,说明该币为假币。
C++ AC 注意编号从1开始
#include <stdio.h>
#include <string.h>
const int maxn = 1002;
int arrayA[maxn];
int arrayB[maxn];
char symbol[2];
int n,i,j,k;
int abs(int x){
    return x < 0 ? -x : x;
}
void initArray(){
    for(i = 1; i < n+1; i++){
        arrayA[i] = 0;
        arrayB[i] = 1;
    }
}
int main(){
    while(scanf("%d%d",&n,&k) != EOF){
        int num = 0;
        initArray();
        for(i = 0; i < k ; i++){
            int first;
            scanf("%d",&first);
            int *tempArrayA = new int[first];
            int *tempArrayB = new int[first];
            for (j = 0; j < first; j++) {        
                scanf("%d",&tempArrayA[j]);
            }
            for (j = 0; j < first; j++) {        
                scanf("%d",&tempArrayB[j]);
            }
            scanf("%s",symbol);
            if(symbol[0] == '='){
                for(j = 0; j < first; j++){
                    arrayB[tempArrayA[j]] = 0;
                    arrayB[tempArrayB[j]] = 0;
                }
            }else if(symbol[0] == '>'){
                num++;
                for(j = 0; j < first; j++){
                    arrayA[tempArrayA[j]]++;
                    arrayA[tempArrayB[j]]--;
                }
            }else if(symbol[0] == '<'){
                num++;
                for(j = 0; j < first; j++){
                    arrayA[tempArrayA[j]]--;
                    arrayA[tempArrayB[j]]++;
                }
            }
        }
        int maxId = 0;
        int count = 0;
        for(j = 1; j < n+1; j++){
            if(arrayB[j] == 1 && abs(arrayA[j]) == num){
                maxId = j;
                count++;
            }
        }
        printf("%d\n",count == 1 ? maxId : 0);
    }
    return 0;
}
/**************************************************************
    Problem: 1486
    User: wangzhenqing
    Language: C++
    Result: Accepted
    Time:30 ms
    Memory:1428 kb
****************************************************************/

Java AC

import java.util.Arrays;
import java.util.Scanner;  
public class Main {
    /*
     * 1486
     */
    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            int k = scanner.nextInt();
            int num = 0;
            int arrayA[] = new int[n + 1];
            int arrayB[] = new int[n + 1];
            Arrays.fill(arrayB, 1);
            while (k > 0) {
                int first = scanner.nextInt();
                int tempArrayA[] = new int[first];
                int tempArrayB[] = new int[first];
                for (int i = 0; i < first; i++) {
                    tempArrayA[i] = scanner.nextInt();
                }
                for (int i = 0; i < first; i++) {
                    tempArrayB[i] = scanner.nextInt();
                }
                String symbol = scanner.next();
                if (symbol.equals("=")) {
                    for (int i = 0; i < first; i++) {
                        arrayB[tempArrayA[i]] = 0;
                        arrayB[tempArrayB[i]] = 0;
                    }
                }else if (symbol.equals(">")) {
                    num++;
                    for (int i = 0; i < first; i++) {
                        arrayA[tempArrayA[i]]++;
                        arrayA[tempArrayB[i]]--;
                    }
                }else if (symbol.equals("<")) {
                    num++;
                    for (int i = 0; i < first; i++) {
                        arrayA[tempArrayA[i]]--;
                        arrayA[tempArrayB[i]]++;
                    }
                }
                k --;
            }
            int max = 0;
            int maxId = 0;
            int j = 1;
            while (j < n + 1) {
                int tempNum =  Math.abs(arrayA[j]);
                if (arrayB[j] == 1 && tempNum == num ) {
                    max++;
                    maxId = j;
                }
                j++;
            }
            System.out.println(max != 1 ? 0 : maxId);
        }
    }
} 
/**************************************************************
    Problem: 1486
    User: wzqwsrf
    Language: Java
    Result: Accepted
    Time:640 ms
    Memory:106776 kb
****************************************************************/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将代码修改为温度每三秒上升一度,持续9秒时进行温升告警,可以按照以下步骤进行修改: 1. 将 tick2 的时间判断条件改为温度上升一度的时间判断条件,即:bms_get_time_interval(tick2, OSTimeGet()) > 3000/1。 2. 将 old_temp 的初始值改为当前温度,即:old_temp = now_temp; 3. 将温度上升的判断条件改为温度上升了三度,即:now_temp - old_temp >= 3。 4. 将触发告警的判断条件改为温度上升了九度,即:now_temp - old_temp >= 9。 下面是修改后的代码: ``` void bms_temp_rise_diag(void) { static INT8U temp_flag = FALSE; static INT16U old_temp = 0; static INT32U tick = 0, tick2 = 0; INT8U flag = 0; INT16U now_temp = 0; if (temp_flag == FALSE) { tick = OSTimeGet(); tick2 = OSTimeGet(); old_temp = now_temp = bms_get_max_temp(); } if (bms_get_time_interval(tick2, OSTimeGet()) > 3000/1 || temp_flag == FALSE) { if (bms_get_time_interval(tick, OSTimeGet()) > 3000) { now_temp += 3; tick = OSTimeGet(); } tick2 = OSTimeGet(); if (now_temp - old_temp >= 3) { flag = TRUE; } old_temp = now_temp; temp_flag = TRUE; } if (flag == TRUE && now_temp - old_temp >= 9) { if (AlarmLevel2 != bms_get_tr()) { save_event_log(TR_ALARM_TYPE, 0xFF); } bms_set_tr(AlarmLevel2); } else { bms_set_tr(AlarmNone); } if (bms_get_rel_flag() == TRUE) { bms_set_tr(AlarmNone); temp_flag = FALSE; } } ``` 注意,这只是一种修改方法,具体的实现方式可能还需要根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值