自动售货机
#include <stdio.h> /* 通过商品索引 返回价格 */ int goods_price(int index){ switch (index) { case 1:case 2:case 3: return 1; break; case 4:case 5: return 2; break; case 6:case 7:case 8: return 3; break; case 9:case 10: return 3; break; default: return 0;; } } /* in message 1 1 2 2 5 5 10 10 -1 1 2 3 5 1 6 9 10 -1 */ int main(void){ int in_money,cost, num, i; char *names[] = {"Table-water", "Table-water", "Table-water", "Coca-Cola", "milk", "Beer", "Orange-Juice", "Sprite","Oolong-Tra", "Green-Tea"}; int count_num[10] = {0}; in_money = cost = 0; while (1) { scanf("%d", &num); if (num != -1) { in_money += num; }else break; } while (1) { scanf("%d", &num); if (num != -1) { cost += goods_price(num); count_num[num-1]++; }else break; } printf("Total: %dyuan, change: %dyuan ", in_money, in_money - cost); for (i = 0; i < 10; i++) { if (count_num[i] > 0) { printf("%s: %d; ", names[i], count_num[i]); } } puts(""); return 0; }