程序设计与算法(一)C语言程序设计CAP 第二周编程作业
来源:互联网 发布:android 数据库查询 编辑:程序博客网 时间:2024/06/10 06:53
现给定r,求V。
4
267.95
#include <iostream>#include<iomanip>using namespace std;#define PI 3.14int main(){ int r; double v; cin >> r ; v=4*(r*r*r*PI)/3.0; cout<<fixed<<setprecision(2)<<v<<endl; return 0;}注解;在c++中为了使得结果可以保留两位小数输出 可以采用两种方法 一:调用#include<iomanip>
(iomanip在c++程序里面经常见到下面的头文件#include <iomanip>,io代表输入输出,manip是manipulator(操纵器)
的缩写(在c++上只能通过输入缩写才有效) 输出样式"cout<<fixed<<stprecision(2)<< << endl ;"
二: 使用printf语句直接定义输出小数的位数 “printf("%.2f",v)” 以上两种方法都可以实现保留两位小数的作用
2.读入三个整数,按每个整数占8个字符的宽度,右对齐输出它们。
123456789 0 -1
123456789 0 -1
程序一
#include<iostream>using namespace std;int main(){int a,b,c;cin>> a >> b >> c ;cout<< a <<""<< b <<""<< c <<endl;return 0; }程序二
#include <iostream>#include <cstdio>using namespace std;int main(){int a,b,c;scanf("%d%d%d",&a,&b,&c);printf("%8d %8d %8d",a,b,c);return 0;}3.
读入一个双精度浮点数,保留12位小数,输出这个浮点数。
3.1415926535798932
3.141592653580
#include<iostream>#include<iomanip>using namespace std;int main(){double a;cin>>a;cout<<fixed<<setprecision(12)<<a<<endl;return 0; }
注解;可以尝试使用一下代码
“”scanf("%lf",&a);printf("%.12f",a);”4.
读入一个字符,一个整数,一个单精度浮点数,一个双精度浮点数,然后按顺序输出它们,并且要求在他们之间用一个空格分隔。输出浮点数时保留6位小数。
a122.33.2
a 12 2.300000 3.200000
#include<iostream>using namespace std;int main(){char c;int a;float b;double d;scanf("%c%d%f%lf",&c,&a,&b,&d); printf("%c %d %.6f %.6lf", c,a,b,d);return 0;}注释;可以代用iomanip 函数 使用 cin cout 输出 cout输出样式 为 cout<<fixed<<setprecision<精确位数>5.
一只大象口渴了,要喝20升水才能解渴,但现在只有一个深h厘米,底面半径为r厘米的小圆桶(h和r都是整数)。问大象至少要喝多少桶水
才会解渴。
23 11
3
#include<iostream>using namespace std;#define PI 3.14159int main(){int h ,r ;scanf("%d%d",&h,&r); double f = 20000.0/(h*PI*r*r); int temp; temp=f; if( temp == f) { printf("%d",temp);}else{temp=temp+1;printf("%d",temp);}return 0;}方法二:
#include <iostream>#include <cstdio>using namespace std;#define PI 3.1415926#define EPS 1e-8;int main(){int h,r;scanf("%d%d",&h,&r);double f = 20000.0/(h*PI*r*r);int n = f;n += (f - n) > EPS; //说不定有的编译器 true 不为1,那就不行了 printf("%d",n);return 0;}
阅读全文
0 0
- 程序设计与算法(一)C语言程序设计CAP 第二周编程作业
- 程序设计与算法(一)C语言程序设计CAP 第三周编程作业
- 程序设计与算法(一)C语言程序设计CAP 第一周
- 程序设计与算法(一)C语言程序设计CAP 第四周
- 程序设计基础(C&C++) 戴波、张东祥 第二章 数据类型与表达式 编程作业
- MOOC 程序设计基础(C&C++) 戴波、张东祥 第二章 数据类型与表达式 作业
- C语言程序设计(第二版)第3章编程题
- C语言程序设计(第二版)第6章编程题
- C语言程序设计进阶 第2周编程作业 2 信号报告(5分)
- 程序设计与算法(一)第二周测验(2017夏季) 解题报告
- 【北大MOOC】2014程序设计实习--第二周编程作业
- 程序设计入门—Java语言_第二周编程作业
- 网易云课堂java程序设计(第二周编程作业)
- 网易云课堂java程序设计(第二周编程作业)
- 算法作业(第二周)
- 第二周作业(一)
- 程序设计基础(C&C++) 戴波、张东祥 第四章 数组与结构 编程作业
- 算法设计与分析第二周作业
- maven的安装与使用
- 火狐谷歌驱动安装
- “钢铁侠”2030年前的小目标:上火星、超级高铁和Model Y
- JAVA-各种类型之间转换 parse() 与valueOf()
- 服务器VPN配置教程(1)——界面配置法
- 程序设计与算法(一)C语言程序设计CAP 第二周编程作业
- 模拟实现 strlen 函数
- 12、sql server建立链接服务器
- Opera下载最新版本地址
- this和super的区别
- LeetCode 9.Palindrome Number
- Reflection2017.12.13
- mvc设计模式和mvc框架的区别
- 1.9 使用PuTTY远程连接Linux;1.10 使用xshell连接Linux;1.11 PuTTY密钥认证; 1.12 xshell密钥认证