HJ98. 自动售货系统
描述
商品 名称 | 单价 | 数量 |
A1 | 2 | X |
A2 | 3 | X |
A3 | 4 | X |
A4 | 5 | X |
A5 | 8 | X |
A6 | 6 | X |
钱币面额 | 张数 |
10元 | X |
5元 | X |
2元 | X |
1元 | X |
参数名称 | 参数说明 | 类型 | 取值范围 |
A1数量 | 商品A1数量 | 整数 | [0,30] |
A2数量 | 商品A2数量 | 整数 | [0,30] |
A3数量 | 商品A3数量 | 整数 | [0,30] |
A4数量 | 商品A4数量 | 整数 | [0,30] |
A5数量 | 商品A5数量 | 整数 | [0,30] |
A6数量 | 商品A6数量 | 整数 | [0,30] |
1元张数 | 面额1元钱币张数 | 整数 | [0,30] |
2元张数 | 面额2元钱币张数 | 整数 | [0,30] |
5元张数 | 面额5元钱币张数 | 整数 | [0,30] |
10元张数 | 面额10元钱币张数 | 整数 | [0,30] |
命令 | 输出 | 含义 |
r 6-5-4-3-2-1 4-3-2-1; | S001:Initialization is successful | 初始化成功 |
命令 | 输出 |
p 10; | S002:Pay success,balance=10 |
命令 | 输出 |
b A1; | S003:Buy success,balance=8 |
命令 | 输出 |
c; | 1 yuan coin number=0 2 yuan coin number=0 5 yuan coin number=1 10 yuan coin number=0 |
查询类别 | 查询内容 |
0 | 查询商品信息 |
1 | 查询存钱盒信息 |
命令 | 输出 |
q 0; | A1 2 6 A2 3 5 A3 4 4 A4 5 3 A5 8 2 A6 6 0 |
命令 | 输出 | |
q 1; | 1 yuan coin number=4 2 yuan coin number=3 5 yuan coin number=2 10 yuan coin number=1 |
输入描述
依照说明中的命令码格式输入命令。
输出描述
输出执行结果
示例1
输入:
r 22-18-21-21-7-20 3-23-10-6;c;q0;p 1;b A6;c;b A5;b A1;c;q1;p 5;
输出:
S001:Initialization is successful E009:Work failure E010:Parameter error S002:Pay success,balance=1 E008:Lack of balance 1 yuan coin number=1 2 yuan coin number=0 5 yuan coin number=0 10 yuan coin number=0 E008:Lack of balance E008:Lack of balance E009:Work failure E010:Parameter error S002:Pay success,balance=5
C 解法, 执行用时: 1ms, 内存消耗: 364KB, 提交时间: 2021-02-21
//感谢 牛客222160237号 大佬 的 Python3代码 提供退币dp算法 的思路!! #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char ch, goods_flag[7]; int i, j, k, idx, goods_rest[7], max, money_rest[5], input, balance, previous_piece_cnt, now_piece_cnt; int goods_price[7] = {1008611, 2, 3, 4, 5, 8, 6}; int money_face_value[5] = {0, 1, 2, 5, 10}; for( ; (ch=getchar())!=EOF; ){ switch( ch ) { case 'r' : getchar(); for(i=1; i<=6; i++){ scanf("%d", goods_rest+i); getchar(); } for(i=1; i<=4; i++){ scanf("%d", money_rest+i); getchar(); } balance = 0; printf("S001:Initialization is successful\n"); break; case 'p' : getchar(); scanf("%d", &input); getchar(); if(input!=1 && input!=2 && input!=5 && input!=10){ printf("E002:Denomination error\n"); break; } if(input==5 || input==10){ if(money_rest[1]+money_rest[2]*2 < input){ printf("E003:Change is not enough, pay fail\n"); break; } } for(i=1; i<=6; i++) if( goods_rest[i] ) break; if(i > 6){ printf("E005:All the goods sold out\n"); break; } switch( input ) { case 1 : { money_rest[1]++; break; } case 2 : { money_rest[2]++; break; } case 5 : { money_rest[3]++; break; } case 10 : { money_rest[4]++; break; } } balance += input; printf("S002:Pay success,balance=%d\n", balance); break; case 'b' : getchar(); if((ch=getchar()) != 'A'){ for( ; (ch=getchar())!=';'; ) ; printf("E006:Goods does not exist\n"); break; } scanf("%d", &input); if(input<1 || input>6){ for( ; (ch=getchar())!=';'; ) ; printf("E006:Goods does not exist\n"); break; } if((ch=getchar()) != ';'){ for( ; (ch=getchar())!=';'; ) ; printf("E006:Goods does not exist\n"); break; } if( !goods_rest[input] ){ printf("E007:The goods sold out\n"); break; } if(balance < goods_price[input]){ printf("E008:Lack of balance\n"); break; } balance -= goods_price[input]; goods_rest[input]--; printf("S003:Buy success,balance=%d\n", balance); break; case 'c' : getchar(); if( !balance ){ printf("E009:Work failure\n"); break; } int (*dp)[5] = (int(*)[5])malloc( sizeof(int)*(balance+1)*5 ); memset(dp, -1, sizeof(int)*(balance+1)*5); memset(dp[0], 0, sizeof(int)*5); for(i=1; i<=balance; i++){ for(j=1; j<=4; j++){ if( (idx = i-money_face_value[j])>=0 ){ if(dp[idx][1] == -1) continue; if(dp[idx][j]+1 <= money_rest[j]){ if(dp[i][1] == -1){ for(k=1; k<=4; k++) dp[i][k] = dp[idx][k]; dp[i][j]++; continue; } previous_piece_cnt = 0; for(k=1; k<=4; k++) if( dp[idx][k] ) previous_piece_cnt += dp[idx][k]; now_piece_cnt = 0; for(k=1; k<=4; k++) if( dp[i][k] ) now_piece_cnt += dp[i][k]; if(previous_piece_cnt+1 < now_piece_cnt){ for(k=1; k<=4; k++) dp[i][k] = dp[idx][k]; dp[i][j]++; } } continue; } break; } } for(i=balance; i>0; i--){ if(dp[i][1] == -1) continue; printf("1 yuan coin number=%d\n", dp[i][1]); if( dp[i][1] ) money_rest[1] -= dp[i][1]; printf("2 yuan coin number=%d\n", dp[i][2]); if( dp[i][2] ) money_rest[2] -= dp[i][2]; printf("5 yuan coin number=%d\n", dp[i][3]); if( dp[i][3] ) money_rest[3] -= dp[i][3]; printf("10 yuan coin number=%d\n", dp[i][4]); if( dp[i][4] ) money_rest[4] -= dp[i][4]; break; } free( dp ); balance = 0; break; case 'q' : if((ch=getchar()) != ' '){ printf("E010:Parameter error\n"); for( ; (ch=getchar())!=';'; ) ; break; } scanf("%d", &input); if((ch=getchar()) != ';'){ for( ; (ch=getchar())!=';'; ) ; printf("E010:Parameter error\n"); break; } if(input == 0){ memset(goods_flag, 0, sizeof(goods_flag)); for(i=1; i<=6; i++){ idx = max = -1; for(j=1; j<=6; j++){ if( goods_flag[j] ) continue; if(max < goods_rest[j]){ max = goods_rest[j]; idx = j; } } goods_flag[idx] = 1; printf("A%d %d %d\n", idx, goods_price[idx], goods_rest[idx]); } break; } if(input == 1){ printf("1 yuan coin number=%d", money_rest[1]); printf("2 yuan coin number=%d", money_rest[2]); printf("5 yuan coin number=%d", money_rest[3]); printf("10 yuan coin number=%d", money_rest[4]); break; } printf("E010:Parameter error\n"); break; case '\n' : break; default : printf("Command Error!!\n"); break; } } return 0; }
C 解法, 执行用时: 2ms, 内存消耗: 308KB, 提交时间: 2021-09-19
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char ch, goods_flag[7]; int i, j, k, idx, goods_rest[7], max, money_rest[5], input, balance, previous_piece_cnt, now_piece_cnt; int goods_price[7] = {0, 2, 3, 4, 5, 8, 6}; int money_face_value[5] = {0, 1, 2, 5, 10}; for( ; (ch=getchar())!=EOF; ) { switch( ch ) { case 'r' : getchar(); for(i=1; i<=6; i++){ scanf("%d", goods_rest+i); getchar(); } for(i=1; i<=4; i++){ scanf("%d", money_rest+i); getchar(); } balance = 0; printf("S001:Initialization is successful\n"); break; case 'p' : getchar(); scanf("%d", &input); getchar(); if(input!=1 && input!=2 && input!=5 && input!=10){ printf("E002:Denomination error\n"); break; } if(input==5 || input==10){ if(money_rest[1]+money_rest[2]*2 < input){ printf("E003:Change is not enough, pay fail\n"); break; } } for(i=1; i<=6; i++) if( goods_rest[i] ) break; if(i > 6){ printf("E005:All the goods sold out\n"); break; } switch( input ) { case 1 : { money_rest[1]++; break; } case 2 : { money_rest[2]++; break; } case 5 : { money_rest[3]++; break; } case 10 : { money_rest[4]++; break; } } balance += input; printf("S002:Pay success,balance=%d\n", balance); break; case 'b' : getchar(); if((ch=getchar()) != 'A') { for( ; (ch=getchar())!=';'; ) ; printf("E006:Goods does not exist\n"); break; } scanf("%d", &input); if(input<1 || input>6) { for( ; (ch=getchar())!=';'; ) ; printf("E006:Goods does not exist\n"); break; } if((ch=getchar()) != ';') { for( ; (ch=getchar())!=';'; ) ; printf("E006:Goods does not exist\n"); break; } if( !goods_rest[input] ) { printf("E007:The goods sold out\n"); break; } if(balance < goods_price[input]) { printf("E008:Lack of balance\n"); break; } balance -= goods_price[input]; goods_rest[input]--; printf("S003:Buy success,balance=%d\n", balance); break; case 'c' : getchar(); if( !balance ) { printf("E009:Work failure\n"); break; } int (*dp)[5] = (int(*)[5])malloc( sizeof(int)*(balance+1)*5 ); memset(dp, -1, sizeof(int)*(balance+1)*5); memset(dp[0], 0, sizeof(int)*5); for(i=1; i<=balance; i++) { for(j=1; j<=4; j++) { if( (idx = i-money_face_value[j])>=0 ) { if(dp[idx][1] == -1) continue; if(dp[idx][j]+1 <= money_rest[j]) { if(dp[i][1] == -1) { for(k=1; k<=4; k++) dp[i][k] = dp[idx][k]; dp[i][j]++; continue; } previous_piece_cnt = 0; for(k=1; k<=4; k++) if( dp[idx][k] ) previous_piece_cnt += dp[idx][k]; now_piece_cnt = 0; for(k=1; k<=4; k++) if( dp[i][k] ) now_piece_cnt += dp[i][k]; if(previous_piece_cnt+1 < now_piece_cnt) { for(k=1; k<=4; k++) dp[i][k] = dp[idx][k]; dp[i][j]++; } } continue; } break; } } for(i=balance; i>0; i--) { if(dp[i][1] == -1) continue; printf("1 yuan coin number=%d\n", dp[i][1]); if( dp[i][1] ) money_rest[1] -= dp[i][1]; printf("2 yuan coin number=%d\n", dp[i][2]); if( dp[i][2] ) money_rest[2] -= dp[i][2]; printf("5 yuan coin number=%d\n", dp[i][3]); if( dp[i][3] ) money_rest[3] -= dp[i][3]; printf("10 yuan coin number=%d\n", dp[i][4]); if( dp[i][4] ) money_rest[4] -= dp[i][4]; break; } free( dp ); balance = 0; break; case 'q' : if((ch=getchar()) != ' ') { printf("E010:Parameter error\n"); for( ; (ch=getchar())!=';'; ) ; break; } scanf("%d", &input); if((ch=getchar()) != ';') { for( ; (ch=getchar())!=';'; ) ; printf("E010:Parameter error\n"); break; } if(input == 0) { memset(goods_flag, 0, sizeof(goods_flag)); for(i=1; i<=6; i++) { idx = max = -1; for(j=1; j<=6; j++) { if( goods_flag[j] ) continue; if(max < goods_rest[j]) { max = goods_rest[j]; idx = j; } } goods_flag[idx] = 1; printf("A%d %d %d\n", idx, goods_price[idx], goods_rest[idx]); } break; } if(input == 1) { printf("1 yuan coin number=%d", money_rest[1]); printf("2 yuan coin number=%d", money_rest[2]); printf("5 yuan coin number=%d", money_rest[3]); printf("10 yuan coin number=%d", money_rest[4]); break; } printf("E010:Parameter error\n"); break; case '\n' : break; default : printf("Command Error!!\n"); break; } } return 0; }