<cf>A. Exams

A. Exams
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

One day the Codeforces round author sat exams. He had n exams and he needed to get an integer from 2 to 5 for each exam. He will have to re-sit each failed exam, i.e. the exam that gets mark 2.

The author would need to spend too much time and effort to make the sum of his marks strictly more than k. That could have spoilt the Codeforces round. On the other hand, if the sum of his marks is strictly less than k, the author's mum won't be pleased at all.

The Codeforces authors are very smart and they always get the mark they choose themselves. Also, the Codeforces authors just hate re-sitting exams.

Help the author and find the minimum number of exams he will have to re-sit if he passes the exams in the way that makes the sum of marks for all n exams equal exactly k.

Input

The single input line contains space-separated integers n and k (1 ≤ n ≤ 501 ≤ k ≤ 250) — the number of exams and the required sum of marks.

It is guaranteed that there exists a way to pass n exams in the way that makes the sum of marks equal exactly k.

Output

Print the single number — the minimum number of exams that the author will get a 2 for, considering that the sum of marks for all exams must equal k.

Sample test(s)
input
4 8
output
4
input
4 10
output
2
input
1 3
output
0
Note

In the first sample the author has to get a 2 for all his exams.

In the second sample he should get a 3 for two exams and a 2 for two more.

In the third sample he should get a 3 for one exam.

思路:当k==2*n时,要重考n次,当k>=3*n时不需要重考,关键是2*n<k<3*n时,k比2*n大多少就要重考多少次。就是

补考 次数=n-(k-2*n) (2*n<=k<3*n); 补考次数=0(k>=3*n)

#include <iostream>
using namespace std;
int main()
{
    int n,k,sum;
    while(cin>>n>>k)
    {
        if(3*n<=k) sum=0;
        //2*n<=k<3*n
        else sum=n-(k-2*n);
        cout<<sum<<endl;
    }
    return 0;
}



DECLARE v_max_add_time TIMESTAMP; v_check_time TIMESTAMP := SYSTIMESTAMP; -- 统一检测时间 v_sql VARCHAR2(4000); v_actual_value NVARCHAR2(4000); v_null_token CONSTANT NVARCHAR2(20) := '<<<NULL>>>'; -- 特殊标记用于空值比较 TYPE result_rec IS RECORD ( order_no NVARCHAR2(64), tb_name NVARCHAR2(128), col_name NVARCHAR2(128), cf_value NVARCHAR2(512), add_time TIMESTAMP, scene_code NVARCHAR2(32), scene_name NVARCHAR2(128), strategy_id NUMBER(19), strategy_name NVARCHAR2(128), rule_id NUMBER(19), rule_name NVARCHAR2(128) ); TYPE result_tab IS TABLE OF result_rec; v_results result_tab; -- 检查表名是否存在的函数 FUNCTION table_exists(p_table_name VARCHAR2) RETURN BOOLEAN IS v_count NUMBER; BEGIN SELECT COUNT(*) INTO v_count FROM all_tables WHERE table_name = UPPER(p_table_name) AND owner = 'GHANA_RISK'; -- 替换为实际schema名 RETURN (v_count > 0); EXCEPTION WHEN OTHERS THEN RETURN FALSE; END; -- 检查列名是否存在的函数 FUNCTION column_exists(p_table_name VARCHAR2, p_column_name VARCHAR2) RETURN BOOLEAN IS v_count NUMBER; BEGIN SELECT COUNT(*) INTO v_count FROM all_tab_columns WHERE table_name = UPPER(p_table_name) AND column_name = UPPER(p_column_name) AND owner = 'GHANA_RISK'; -- 替换为实际schema名 RETURN (v_count > 0); EXCEPTION WHEN OTHERS THEN RETURN FALSE; END; -- 获取列数据类型函数 FUNCTION get_column_data_type( p_table_name VARCHAR2, p_column_name VARCHAR2 ) RETURN VARCHAR2 IS v_data_type VARCHAR2(128); BEGIN SELECT data_type INTO v_data_type FROM all_tab_columns WHERE owner = 'GHANA_RISK' AND table_name = UPPER(p_table_name) AND column_name = UPPER(p_column_name); RETURN v_data_type; EXCEPTION WHEN OTHERS THEN RETURN 'UNKNOWN'; END; BEGIN -- 1. 获取最新检测时间范围 SELECT MAX(ADD_TIME) INTO v_max_add_time FROM GHANA_RISK.TQ_RULE_ITEM_RESULT_7; -- 2. 获取半小时内需检测的数据 SELECT TQ_ORDER_NO, TB_NAME, COL_NAME, CF_VALUE, ADD_TIME, SCENE_CODE, SCENE_NAME, STRATEGY_ID, STRATEGY_NAME, RULE_ID, RULE_NAME BULK COLLECT INTO v_results FROM GHANA_RISK.TQ_RULE_ITEM_RESULT_7 WHERE ADD_TIME BETWEEN v_max_add_time - INTERVAL '10' MINUTE AND v_max_add_time; -- 3. 遍历每条记录并动态检测 FOR i IN 1..v_results.COUNT LOOP DECLARE v_data_type VARCHAR2(128); v_conversion_expr VARCHAR2(100); BEGIN -- 验证表名和列名是否存在 IF NOT table_exists(v_results(i).tb_name) THEN RAISE_APPLICATION_ERROR(-20001, 'Table not found: ' || v_results(i).tb_name); END IF; IF NOT column_exists(v_results(i).tb_name, v_results(i).col_name) THEN RAISE_APPLICATION_ERROR(-20002, 'Column not found: ' || v_results(i).col_name); END IF; -- 获取列数据类型并构建转换表达式 v_data_type := get_column_data_type(v_results(i).tb_name, v_results(i).col_name); CASE v_data_type WHEN 'NUMBER' THEN -- 数字类型:使用TO_CHAR保留格式(包括小于1的前导0) v_conversion_expr := 'TO_CHAR(' || v_results(i).col_name || ', ''FM9999999999999999999990D99999999999999999999'')'; WHEN 'VARCHAR2', 'CHAR', 'NVARCHAR2', 'NCHAR' THEN -- 字符类型:直接使用原列 v_conversion_expr := v_results(i).col_name; ELSE -- 其他类型:默认转换为字符串 v_conversion_expr := 'TO_CHAR(' || v_results(i).col_name || ')'; END CASE; -- 构建动态SQL(转换为NVARCHAR2以匹配cf_value类型) v_sql := 'SELECT TO_NCHAR(' || v_conversion_expr || ') ' || 'FROM GHANA_RISK.' || v_results(i).tb_name || ' ' || 'WHERE TQ_ORDER_NO = :1'; -- 执行动态查询 EXECUTE IMMEDIATE v_sql INTO v_actual_value USING v_results(i).order_no; -- 4. 比较值并插入差异 IF NVL(v_actual_value, v_null_token) <> NVL(v_results(i).cf_value, v_null_token) THEN INSERT INTO FK_ADS.ADS_MONITORING_RESULT_DI ( TQ_ORDER_NO, TB_NAME, COL_NAME, CF_VALUE, ACTUAL_VALUE, CHECK_TIME, ADD_TIME, SCENE_CODE, SCENE_NAME, STRATEGY_ID, STRATEGY_NAME, RULE_ID, RULE_NAME ) VALUES ( v_results(i).order_no, v_results(i).tb_name, v_results(i).col_name, v_results(i).cf_value, v_actual_value, v_check_time, v_results(i).add_time, v_results(i).scene_code, v_results(i).scene_name, v_results(i).strategy_id, v_results(i).strategy_name, v_results(i).rule_id, v_results(i).rule_name ); END IF; EXCEPTION WHEN NO_DATA_FOUND THEN INSERT INTO FK_ADS.ADS_MONITORING_RESULT_DI ( TQ_ORDER_NO, TB_NAME, COL_NAME, CF_VALUE, ACTUAL_VALUE, CHECK_TIME, ADD_TIME, SCENE_CODE, SCENE_NAME, STRATEGY_ID, STRATEGY_NAME, RULE_ID, RULE_NAME ) VALUES ( v_results(i).order_no, v_results(i).tb_name, v_results(i).col_name, v_results(i).cf_value, 'RECORD_NOT_FOUND', v_check_time, v_results(i).add_time, v_results(i).scene_code, v_results(i).scene_name, v_results(i).strategy_id, v_results(i).strategy_name, v_results(i).rule_id, v_results(i).rule_name ); COMMIT; END; /报错[65000][6550] ORA-06550: 第 116 行, 第 24 列: PLS-00103: 出现符号 ","在需要下列之一时: * & = - + < / > at in is mod remainder not rem then <an exponent (* ...
07-17
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值