HNUCM_入门级题库 (练习1)【题解】
发布日期:2021-06-29 14:25:56 浏览次数:2 分类:技术文章

本文共 2371 字,大约阅读时间需要 7 分钟。

欢迎大家积极参与ACM训练计划中~

在这里插入图片描述

简述

题解会包含两种版本,C与C++的,但一般C++用的比较多,当然java也会用到。

既然常用的是C++ ,学弟学妹们可以试着习惯C++的代码风格

(等过了一段时间后,题解就只用C++来写了,目前先把C的基础打牢,然后慢慢过渡到C++,C++版本的也可以动手敲一敲代码)

A


题目大意

编写一个程序来计算给定整数x的立方体。


题解

C语言版本的就不作解释了,详情看代码。

关于C++版本的 用到了一个内置函数pow(x , y)
头文件:#include <math.h>
pow() 函数用来求 x 的 y 次幂(次方),x、y及函数值都是double型 ,其原型为:
double pow(double x, double y);
pow()用来计算以x 为底的 y 次方值,然后将结果返回。设返回值为 ret,则 ret = xy。
(c语言后面也会讲到,详情可百度查阅更多知识)


C语言版本

#include
int main(){
int x; scanf("%d",&x); x=x*x*x; printf("%d\n",x); return 0;}

C++版本

#include
//万用头文件using namespace std;int main(){
ios::sync_with_stdio(false); cin.tie(0); int x; cin>>x; cout<<(int)pow(x,3)<

注:以上就是这个题的C++版本,采用了万用头文件形式(一般情况下都是能够使用的,只用写这一个头文件即可),并且用到了平常用到的加速模板,下面两段代码不写也是可以的,如果大家想了解一下相关知识,可以查阅下面的博客~

ios::sync_with_stdio(false);//常用加速模板cin.tie(0);

B


题目大意

编写一个程序来计算给定矩形的面积和周长。


题解

面积周长公式


C语言版本

#include
int main(){
int a,b; scanf("%d %d",&a,&b); printf("%d %d\n",a*b,(a+b)*2); return 0;}

C++版本

#include
using namespace std;int main(){
ios::sync_with_stdio(false); cin.tie(0); int a,b; cin>>a>>b; cout<
<<" "<<(a+b)*2<

C


题目大意

编写一个程序,读取一个整数S[秒],并将其转换为h : m : s,其中h、m、s分别表示小时、分钟(小于60)和秒(小于60)。


题解

转化方式详情见代码


C语言版本

#include
int main(){
int S,h,m,s; scanf("%d",&S); h = S/3600; m = S%3600/60; s = S%3600%60; printf("%d:%d:%d\n",h,m,s); return 0;}

C++版本

#include
using namespace std;int main(){
int S,h,m,s; cin>>S; h = S/3600; m = S%3600/60; s = S%3600%60; cout<
<<":"<
<<":"<
<

D


题目大意

编写一个程序,打印给定两个整数a和b的小/大/等关系。


题解

注意输出符号间是有空格的,不注意的话容易格式错误


C语言版本

#include
int main(){
int a,b; scanf("%d %d",&a,&b); if(a
b) printf("a > b\n"); else printf("a == b\n"); return 0;}

C++版本

#include
using namespace std;int main(){
int a,b; cin>>a>>b; if(a
b) cout<<"a > b"<

E


题目大意

编写一个程序,读取三个整数a、b和c,如果a < b < c,则输出“Yes”,否则输出“No”。


题解

&&符号运用


C语言版本

#include
int main(){
int a,b,c; scanf("%d %d %d",&a,&b,&c); if(a

C++版本

#include
using namespace std;int main(){
int a,b,c; cin>>a>>b>>c; if(a

结尾

对比两个版本,不难发现还是有较多相似之处,读者看完题解后对于不会做的题还是要多敲几次,熟能生巧!另外,C++是需要自学的,也可以去看我的一些分类专栏

在这里插入图片描述
在这里插入图片描述

学如逆水行舟,不进则退

转载地址:https://chocolate.blog.csdn.net/article/details/102420927 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:HNUCM 19级ACM训练计划【题解模板】
下一篇:Codeforces Round #277.5 (Div. 2), problem: (C) Given Length and Sum of Digits...(贪心)【贪心策略:最小值与最大值】

发表评论

最新留言

不错!
[***.144.177.141]2024年04月09日 00时26分07秒