环境:ide:Mac+clion
我是看这个视频,进行学习跟进:
https://www.bilibili.com/video/BV1Hb411Y7E5?p=5
算术运算符:
double d1 = 0.5;
double d2 = 0.22;
cout << d1 / d2 << endl;//2.27273 整数相除是整数,浮点数相除是浮点数
//取模运算%
cout << 10 % 3 << endl;//1 这里的取模其实就是余数
cout << 10 % 20 << endl;//10
注意:两个小数是不可以进行取模运算。
//前置递增:先+1,再表达式运算。
//后置递增:先表达式运算,后加1
int a = 10;
int b = ++a;
cout<< a <<","<< b<<endl;//11,11
int c = 10;
int d = c++;
cout<< c <<","<< d<<endl;//11,10
//switch 结构清晰,不能判断区间,效率高。
//猜数字 1-100
srand((unsigned int)time(NULL));//这里把时间加进来生成随机数种子
int aimNum = rand()%100 +1;//生成1-100的随机数。 rand()%100 生成的是0-99随机数
int guessNum = 0;
cout <<“生成的随机数结果是:”<< aimNum << endl;
cout <<“青输入你猜的数字:”<< endl;
while(true){
cin >> guessNum;
if(guessNum < 1 || guessNum > 100){
cout << “您输入的数字不合法,请输入1-100之内的数。” << endl;
}
if(aimNum == guessNum){
cout << “恭喜您猜对了。gameOver” << endl;
break;
}else if(aimNum > guessNum){
cout << “您猜测的数过小,青重新输入。” << endl;
}else if(aimNum < guessNum){
cout << “您猜测的数过大,青重新输入。” << endl;
}
}
//do … while 循环打印 0-9;
int x = 0;
do {
cout<< x << endl;
x++;
} while (x < 10);
//所有三位数的水仙花数
int val = 100;
do{
int a = val % 10;//个位
int ten = val / 10 % 10;//十位
int hundred = val / 100 ;//百位
int sum = a*a*a + ten*ten*ten + hundred*hundred*hundred;
if(sum == val){
cout<<"水仙花数是:"<<val<<endl;
}
val ++;
} while (val< 1000);
//1-100,敲桌子,个位7,十位7,或者7的倍数打印出来
int target = 100;
for (int i = 1; i<=target; i++){
if(i % 7 == 0 || (i % 10 ) == 7 || (i / 10 ) == 7){
cout << "敲桌子:" << i << endl;
}
}
//外层是行,相当于执行一次,里面循环执行一周
for(int i = 0;i<10;i++){
for(int j=0;j<10;j++){
cout<<"* ";
}
cout<<endl;
}
//九九乘法表
for (int i = 1; i < 10; i++) {
for (int j = 1; j <= i; j++) {
cout << j << " * " << i << " = " << i * j << "\t";
}
cout << endl;
}
//goto 无条件跳转代码
for(int i=0;i<5;i++){
if(i==3){
goto FLAG;
}
cout<<i<<endl;
}
FLAG:
cout << "标记位置" << endl;