C02-2.23基本运算
原题链接:38471.note12025-02-23 18:09:37
发布于:江苏
一、数据类型
1. 整数:
short 短整型
int 整型
long long 长长整型
2. 小数:(分数,浮点数, 实数)
double 双精度
float 单精度
3. 字符串:
string s = "xmw";
//双引号内容,原样输出 "这个就是字符串"
4. 字符
char ch = 'c'; //注意:"c"是字符串
//单引号内容, 'c'
二、占位符
%d --- int类型占位符
%f --- float类型占位符
%lf --- double类型占位符
#include <iostream>
#include <stdio.h>
using namespace std;
int main(){
printf("Nice to meet you\n");
printf("Nice to meet you too\n");
double pai = 3.1415926;
cout << pai << endl;
printf("胡老师今天早上吃了%09d个包子, %d个水饺\n", 2, 10); //%d是整数占位符
printf("胡老师的钱包有%.3lf元钱\n", 3.1415926); //四舍五入
/*
%f --> float
%lf --> double
*/
return 0;
}
★整数的对齐输出
#include<iostream>
using namespace std;
int main(){
int a;
cin >> a;
printf("%-6d%-6d", a, a); //-表示左对齐
// printf("%06d", a); //默认右对齐
return 0;
}
三、数据类型转换
实例1: 计算平均分
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
int sum, n;
cin >> sum >> n;
double avg = (double)sum/ n; //强制类型转换
printf("%.3lf", avg);
return 0;
}
四、模运算取位数
除10的作用: 消除最后一位 //cout << a/10 << endl;
模10的作用:取出最后一位 //cout << a%10 << endl;
#include<iostream>
using namespace std;
int main()
{
int a = 123;
cout << a%10 << endl; //个位
cout << a/10%10 << endl; //十位
cout << a/100 << endl; //百位
//int / int = int
// cout << a/10 << endl; //消除最后一位
// cout << a%10 << endl; //取出最后一位
// cout << a%100 << endl;
return 0;
}
这里空空如也
有帮助,赞一个