数数小木块
来源:互联网 发布:mysql数据文件路径 编辑:程序博客网 时间:2024/05/22 06:22
Ø 题目描述
在墙角堆放着一堆完全相同的正方体小木块,如下图所示:
因为木块堆得实在是太有规律了,你只要知道它的层数就可以计算所有木块的数量了。
现在请你写个程序 给你任一堆木块的层数,求出这堆木块的数量.
Ø 考核知识点
循环的嵌套。
Ø 解题思路
本题目的思路即是循环求出每一层的木块数量,然后将每一层加起来,求出总和即可,思路不难,代码实现起来也较为简单。观察图片,第n层的木块数是:1+2+3+4+5+...+n
#include<stdio.h>int main(){ int n; scanf("%d", &n);//设置测试组数 int i, j, z;//三个循环变量。 int a;//设置统计多少层小木块 int result, s;//result用来记录每一层木块有多少,s记录总和。 for ( i = 1; i <= n; i++) { scanf("%d", &a);//在n次测试当中每次都要重新输入有多少层木块 s = 0;//将总和归零,容易忽略此步骤 for ( z= 1; z <= a; z++) { result = 0; for ( j = 1; j <= z; j++) { result += j; } s += result; } printf("%d\n", s); } return 0;}
阅读全文
0 0
- 数数小木块
- 数数小木块
- 数数小木块
- 数数小木块
- 数数小木块
- 260 数数小木块
- nyoj-数数小木块
- nyoj260数数小木块
- 数数小木块
- NYOJ数数小木块
- 数数小木块
- ACM-数数小木块
- 数数小木块0
- 260 数数小木块
- nyoj260 数数小木块
- 41.数数小木块
- 260.数数小木块
- acm数数小木块
- ZooKeeper伪分布式集群安装及使用
- openstak各种服务同步数据库出错问题
- cin.tie与sync_with_stdio加速输入输出
- 平面点排序(结构体写法)
- 连接数据库的快速方法,用接口解决
- 数数小木块
- Spring 开启Annotation <context:annotation-config> 和 <context:component-scan>诠释及区别
- 使用Docker部署 spring-boot maven应用
- 计数问题-vijos
- Ubuntu Linux 显卡驱动安装
- 大三才开始想写博客
- Angular4_bootstrap模态框打开关闭,点击背景事件
- websocket-shrap源码阅读
- Python学习笔记(2)——print