c++2021-5-30学习笔记

环境: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;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值