当前位置: 首页 > news >正文

C++微基础蓝桥杯之旅9.9-9.12

这里主要还是强制类型转换的使用

//打印字符ASCII码值 
//输入一个除空格以外的可见字符
//输出其ASCII值--十进制整数 

#include <iostream>
using namespace std;

int main()
{
char ch;

cin >> ch;//字符


cout <<  (int)ch << endl; 

return 0;
}

//打印字符 

#include <iostream>
using namespace std;
//之前写的是 
int main()
{
int n = 0;
cin >> n;
char ch = n;
cout << ch << endl; 

return 0;

//现在学习了强制类型转换可以写成 
int main()
{
int n = 0; 
cin >> n;

cout << (int)ch << endl; 

    
return 0;

//++和 ——
//前置++  后置++
//无论前置后置++ --都是为了让操作数自+1 -1

#include <iostream>
using namespace std;

int main()
{
int a = 10;
//a++;
//
//cout << a << endl;//----11

//++a;
//cout << a << endl;//-----11

//    a--;
//    cout << a << endl;//----9
//    
//    --a;
//    cout << a << endl;//----9

return 0;
}

//区别是后置++是先使用再+1 


#include <iostream>
using namespace std;

int main()
{
int a = 10;
int b = a++;

cout << "a = " << a << endl;
cout << "b = " << b << endl;
//a = 11,,b = 10    

return 0;
}

#include <iostream>
using namespace std;

int main()
{
int a = 10;
int b = ++a;

cout << "a = " << a << endl;
cout << "b = " << b << endl;
//a = 11,,,b = 10

return 0;
}

#include <iostream>
using namespace std;

int main()
{
int a = 10;
int b = a--;

cout << "a = " << a << endl;
cout << "b = " << b << endl;
//a =9,,,b = 10

return 0;
}

#include <iostream>
using namespace std;

int main()
{
int a = 10;
int b = a--;

cout << "a = " << a << endl;
cout << "b = " << b << endl;
//a = 9,,,b = 9

return 0;
}

C/C++的输入输出

getchar/putchar

gechar-----必须包含头文件---#include <cstdio>

int getchar (void);

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
int ch = gechar();//输入端口--()是传参需要,必不可少 
cout << ch << endl;
//输入a--输出97 
//注意gechar有严格的输入格式要求   

return 0;

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
int ch = gechar();
cout << ch << endl;
cout << (char)ch;

ch = gechar();
cout << ch << endl;
cout << (char)ch;
cout << "xxxxx" << endl;

//输出a
//     97
//     a10
----gechar有严格的格式,在输入a时输入的回车(/n)键也被记录的--10 
      //
//   xxxxx 
return 0;

}

putchar---输出字符

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{

//    int ch = getchar();
//    putchar(ch);

putchar('x');
putchar('\n');
putchar('y');
putchar('\n');
putchar('z');
putchar('\n');

//输出
// x
// y
// z
// 


return 0;

//输入两个整数a,b,输出a除以b的值,保留三位小数
//输入两位整数,在int范围内
//输出一个浮点数,保留三位小数 

#include <iostream>
#include <cstdio>
using namespace std;

int main() 
{
int a, b;
cin >> a >>b;
double r = a * 1.0 / b;
printf("%.3f\n",r);


return 0;
}

//甲流疫情死亡率
//输入共两行,第一行第一个整数为确诊数a,第二行为整数死亡数b
//输出仅一行,甲流死亡率,以百分数形式输出,精确到小数点后三位 


#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
int a,b;

cin >> a >>b;
//计算
printf("%.3f%%\n",b*0.1 / a*100); 


return 0;
}

//温度表达转换 
//利用公式C = 5 8 (F- 32)/ 9 
//C是摄氏度,F是华氏温度 输入华氏温度F,输出摄氏温度C,
//要求精确到小数点后5位 
//输入一行,包含一个实数F(小数),表示华氏温度,(F >=-456.67)
//输出一行,包含一个实数,表示对应的摄氏温度 

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
double f;

cin >> f;
double c = 5 * (f - 32) / 9;

printf("%.5f\n",c);


return 0;
}

//计算并联电阻的阻值
//对于r1和r2的电阻,公式
//R=1/(1/r2 + 1/r2) 
//输入r1,r2,输出并联后的阻抗大小,
//结果保留小数点后两位 


#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
double r1,r2;
cin >> r1 >> r2;

double r = 1 / (1 / r1 + 1 / r2);

printf("%.2f\n",r);

return 0;
}

//与圆相关的计算
//给出圆的半径,求圆的周长,直径和面积
//每个 数保留4位小数 

#include <iostream>
#include <cstdio>

const double PI = 3.14159;

using namespace std;

int main()
{
double r ;

cin >> r;

double z = 2 * r;
double l = 2 * PI *r;
double a = PI * r * r;

printf("%.4f %.4f %.4f\n",z,l,a);

return 0;
}

//输入三个整数,按每一个整数占8字符的宽度,右对齐输出他们,
//按照格式要求依次输出他们,空格隔开

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
int a,b,c;

cin>> a >> b >> c;

printf("%8d %8d %8d\n",a,b,c);


return 0;
}

//5(1,2,3,4,5)个小朋友有(输入)若干 糖果,
//他们都将糖果均分位三份, 自己留一份,其余的分给相邻的两个小朋友
//一轮后,每个小朋友手上分别有多少糖果 
//

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
int a,b,c,d,e;

cin >> a >> b >> c >> d >> e;
//1
a /= 3; b += a; e =+ a;
//2
b /= 3; c += b; a += b;
//3
c /= 3; d += c; b += c;
//4
d /= 3; e += d; c += d;
//5
e /= 3; a += e; d += e;

printf("%5d%5d%5d%5d%5d\n",a,b,c,d,e);

return 0;

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{



return 0;
}

//数字反转
//输入一个不小于100,且小于1000,同时包括小数点后一位的一个浮点数
//例如 123.4,要求把这个数字翻过来,变成4.321并输出

//#include <iostream>
//#include <cstdio>
//
//using namespace std;

//int main()
//{
//    char a,b,c,d,e;
//    
//    cin >> a >> b >> c >> d >> e;
//    //     1    2    3    .    4
//    cout << e << d << c << b << a; 
//    
//    
//    return 0;
// } 

//int main()
//{
//    char a,b,c,d,e;
//    scanf("%c%c%c.%c",&a,&b,&c,&d,&e);
//    print("%c.%c%c%c\n",e,d,c,b,a);
//    
//    return 0;
//}

//输入三角形三条边a,b,c 
// 其面积为 (p*(P-a)(p-b)(p-c))*0.5;
//p=(a +b+c)/2     
//输出 面积 

#include <iostream>
#include <cstdio>
#include <cmath>
#include <iomanip>

using namespace std;

double a,b,c; 

int main()
{
cin >> a >> b>> c;

double p = (a + b + c) / 2;
double area = sqrt(p * (p - a) * (p - b) * (p - c));

cout << fixed << setprecision(1) << area << endl;

return 0;
}


using namespace std;

double a,b,c;

int main()
{
cin >> a >> b >> c;

double p = (a + b + c) / 2;
double area = sqrt(p * (p - a) * (p - b) * (p - c));

printf("%.1f\n",area);

return 0;
}

  

//输入两个整数m,n,整数输出yes,否则输出no 


#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
int a,b;
cin >> a >> b;

if (a % b == 0)
{
cout << "yes" << endl;
}
else
{
cout << "no" << endl;
}
return 0;
}


//输入两个整数,比较其大小
//输出< > = 

#include <iostream>
#include <cstdio>
#include <cmath>
#include <iomanip> 

using namespace std;

int main()
{
long long x,y;
cin >> x >> y;
if(x > y)
cout << ">" << endl;

else if(x == y)
cout << "=" << endl;

else
cout << "<" << endl;

return 0;
}

//输入一个浮点数,输出其绝对值
//保留小数点后两位
//---只是浮点数--float 

#include <iostream>
#include <cstdio>
#include <cmath>
#include <iomanip> 

using namespace std;

int main()
{
float n;
cin >> n; 
if(n < 0)

printf("%.2f\n",-n);
else
printf("%.2f\n",n);

return 0;
}

但是这个还有其他的解法

//fabs--专用来求浮点数绝对值的函数 
//--头文件cmath

int main()
{
float n;
cin >> n; 

n = fabs(n);

    printf("%.2f\n",n);

return 0;
}

拓展一下~
abs----专门计算整数的绝对值的
其所需头文件---cstdlib

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
int n;
cin >> n;
int m = abs(n);
cout << m << endl;

//奇偶数判断
//n是奇数 输出odd
//n是偶数 输出even
//-100 <= n <= 100 

#include <iostream>
#include <cstdio>
#include <cmath>
#include <iomanip> 
#include <cstdlib>

using namespace std;

 
int main()
{
int n;
cin >> n;
if(n % 2 == 1)
cout << "odd" << endl;
else if(n % 2 == -1)
cout << "odd" << endl;
else
cout << "even" << endl;    

return 0;
}  

//还有其他写法 
int main()
{
int n;
cin >> n;
if(n % 2)
cout << "odd" << endl;
else if(n % 2 == -1)
cout << "odd" << endl;
else
cout << "even" << endl;    

return 0;

//学生成绩
//输入3个0~100的数字
//输出 该学生刚好有一门不及格输出1,否则输出0 

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
int s1,s2,s3;
int cnt = 0;
cin >> s1 >> s2 >> s3;
if(s1 , 60)
cnt++;
if(s2 < 60)
cnt++;
if(s3 < 60)
cnt++;

if(cnt == 1)
cout << 1 << endl;
else
cout << 0 << endl;

return 0;
}

//另一种做法
int main()
{
int s1,s2,s3;
cin >> s1 >> s2 >> s3; 

if((s1 <60)+(s2 < 60)+(s3 <60)==1)
cout << 1 <<endl;
else
cout << 0 << endl;


return 0;

//等差数列末项计算
//输入三个整数a1,a2,n,(-100<=a1,a2<=100,0<n<=1000;)
//输出一个整数,即第n项的值 

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
int a1,a2,n;
cin >> a1 >> a2 >> n;
if(n == 1)
cout << a2 << endl;
else if(n == 2)
cout << a2 << endl;
else
cout << a2 + (n - 2)*(a2 - a1) << endl; 


return 0;


文章转载自:

http://Z5WuSRhO.srbsr.cn
http://qJ3PYLb7.srbsr.cn
http://OijK7rFu.srbsr.cn
http://Fbaqfa8O.srbsr.cn
http://beHyOYT9.srbsr.cn
http://uUrjgKAx.srbsr.cn
http://t2AKs37q.srbsr.cn
http://G0EbDnpR.srbsr.cn
http://JorN8IT1.srbsr.cn
http://MMc7LhWG.srbsr.cn
http://QyOr03f8.srbsr.cn
http://2MqeSNml.srbsr.cn
http://BCztZuLS.srbsr.cn
http://80gcXeFD.srbsr.cn
http://AwBJuAbH.srbsr.cn
http://rWRkYu4h.srbsr.cn
http://6z8qSt28.srbsr.cn
http://ebih76MP.srbsr.cn
http://8U26ocp7.srbsr.cn
http://e8R3Y2eA.srbsr.cn
http://lXAWZU3C.srbsr.cn
http://nieeX4pG.srbsr.cn
http://ybY54dPK.srbsr.cn
http://w0L8ZAWr.srbsr.cn
http://Jc1TyADJ.srbsr.cn
http://m5KypcLx.srbsr.cn
http://1MoW85kv.srbsr.cn
http://mg4FwjPq.srbsr.cn
http://KRPkN5Cq.srbsr.cn
http://xXXbxFUS.srbsr.cn
http://www.dtcms.com/a/379893.html

相关文章:

  • 一款好看的jQuery前端框架-HisUI
  • Go语言io.Copy深度解析:高效数据复制的终极指南
  • k8s-init容器学习
  • 【算法磨剑:用 C++ 思考的艺术・Dijkstra 实战】弱化版 vs 标准版模板,洛谷 P3371/P4779 双题精讲
  • Java大厂面试实录:产业互联网大数据与AI服务场景下的微服务与智能搜索(含详细解读)
  • 苍穹外卖项目笔记day08
  • 智能逗猫球方案MCU控制方案浅析-智能宠物玩具,宠物解闷神器
  • Unity键盘控制角色运动
  • 大数据毕业设计-基于Spark的全国高速公路实时路况融合与拥堵预测系统(高分计算机毕业设计选题·定制开发·真正大数据)
  • zmq源码分析之session
  • Xcode 上传 ipa 全流程详解 App Store 上架流程、uni-app 生成 ipa 文件上传与审核指南
  • Java 泛型详解:从基础到高级应用
  • 第6.2节 Android Agent开发<二>
  • ubuntu挂载新硬盘的方法
  • Kubernetes Ingress:使用 Apache APISIX 进行外部流量路由
  • 初学者如何选择适合的云平台进行AIGC训练?
  • Docker存储卷(Volume)完全指南:从入门到精通
  • STM32-FreeRTOS操作系统-二值信号量与计数信号量
  • 蒸面器/蒸脸仪方案开发,蒸面器/蒸脸仪MCU控制方案分析
  • 容器技术崛起:从PaaS到Docker的变革探问
  • 如何定位Mysql慢查询和短而频的查询
  • 机器学习的基本流程:从数据到模型
  • springboot rabbitmq 消息队列入门与实战
  • 使用vllm部署neo4j的text2cypher-gemma-2-9b-it-finetuned-2024v1模型
  • 栈-844.比较含退格的字符串-力扣(LeetCode)
  • [Dify] HTTP 请求节点详解:如何在 Dify 中配置与调用第三方 API
  • SQL优化简单思路
  • 构建AI智能体:三十一、AI医疗场景实践:医学知识精准问答+临床智能辅助决策CDSS
  • HTTP的Web服务测试在Python中的实现
  • 华为HCIE-云计算培训课程有哪些?