无聊写个小程序╮(╯▽╰)╭
来源:互联网 发布:淘宝网刷收藏 编辑:程序博客网 时间:2024/06/18 15:48
一瓶可乐一块钱,两个可乐瓶可换一瓶可乐,三个可乐瓶盖可换一瓶可乐,你用20块可以喝到几瓶可乐
#include<stdio.h>#include<stdlib.h>#include<math.h>int* bottle_ex(int* bottle) { int tmp = 0, tmp1 = 0; tmp = bottle[1] % 2; tmp1 = bottle[1] / 2; bottle[0] += tmp1; bottle[1] = tmp; bottle[2] += tmp1; return bottle;}int* cap_ex(int* cap_num) { int tmp = 0; int tmp1 = 0; tmp = cap_num[2] % 3; tmp1 = cap_num[2] / 3; cap_num[0] += tmp1; cap_num[2] = tmp; cap_num[1] += tmp1; return cap_num;}int* cola_ex(int* cola) { int tmp = 0; if (cola[1] >= 1) { cola[1] -= 1; cola[0] += 1; cola[2] += 1; } if (cola[2] >= 2) { cola[2] -= 2; cola[0] += 1; cola[1] += 1; } return cola;}int main() { int cola[3] = { 20 , 0 , 0 }; int* colax = cola; cola[1] = cola[0]; cola[2] = cola[0]; while (cola[1] >1 || cola[2] > 2) { colax = bottle_ex(colax); colax = cap_ex(colax); } while ((cola[1] >= 1) || (cola[2] >= 2)) { colax = cola_ex(colax); } printf("The final cola num is %d.\nThe final bottle num is %d.\nThe final cap num is %d.\n", cola[0], cola[1], cola[2]); system("pause"); return 0;}//1、在只有一个瓶子的情况下,通过借一个瓶子,可以换得一瓶可乐,加一个瓶盖,即一个瓶子 = 一瓶可乐 + 一个瓶盖//2. 在只有一个瓶盖的情况下,通过借两个瓶盖,可知还不起,所以当瓶盖数小于等于1时,无法继续//3. 在有两个瓶盖的情况下,通过借一个瓶盖,可以得到一瓶可乐+一个瓶子,即两个瓶盖 = 一瓶可乐 + 一个瓶子.//据以上分析进行函数建立
阅读全文
0 0
- 无聊写个小程序╮(╯▽╰)╭
- 一个无聊的小程序
- “一道无聊的程序题”
- 无聊
- 无聊
- 无聊
- 无聊
- 无聊...
- 无聊
- 无聊
- 无聊
- 无聊
- 无聊
- 无聊
- 无聊
- 无聊
- 无聊
- 无聊
- 初识Linux多线程编程
- ADXL345加速度计使用心得(带中断唤醒)
- 【ARM】Ubuntu下使用Qt Creator创建交叉编译项目
- B1037
- Spring Security 4 (02)—— 用户信息
- 无聊写个小程序╮(╯▽╰)╭
- MyEclipse中如何安装spket插件
- python多线程 简单例子
- BUMPMAPPING WITH GLSL
- java web 在jsp中使用include指令导入带中文的HTML文件乱码问题
- ReactNative分组列表SectionList使用详情及示例详解
- 机器学习
- 尽量避免多进程多线程混用,当下死锁
- Linux系统入门学习:教你在VirtualBox 安装 Ubuntu 15.04