书接上篇,我感觉前面都挺简单的,如果前两篇有不懂的或者错误的,欢迎大家提出~

接下来我也是照样按顺序记录一些做过的题,有时候会记录一些我认为的有点难记易混淆的点,大家可以当笔记来看噢

三目操作符

//ex1 ? ex2 ! ex3
//真    计算   不计算
//假   不计算   计算---ex3为最终结果 

//输出a,b,c的最大值

#include <iostream>

using namespace std;
int main()
{
    int a,b,c;
    int m;//最大值 
    cin >> a >> b >> c;
    if(a >b)
        m = a;
    else
        m = b;
    if (m < c)
        m = c;
        cout << m << endl;
    
    return 0;
}
//可改为
int main()
{
    int a,b,c;
    int m;
    cin >> a >> b >> c;
    //使用三目操作符 
    m = ( a > b ? a : b);
    
    if( m < c)
        m = c;
    cout << m << endl;
    
    
    return 0;
 } 


//特殊计算,给你两个整数X , Y ,X<=100000000000
//如果X 是 Y 的因素,则Z = X + Y,否则 Z = Y - X
//输出Z 
//因素的意思--x是可以整除y的 

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    long long X,Y,Z;
    cin >> X >> Y;
    
    if(Y % X == 0)
        Z = X + Y;
    else 
        Z = Y - X;
    
    cout << Z << endl;
    
    return 0;
}

//可以使用三目操作符改为 

int main()
{
    long long X,Y,Z;
    cin >> X >> Y;
    
 
   Z = ((Y % X == 0) ? (X + Y) : (Y - X)); 
    
    cout << Z << endl;
    
    return 0;
}

//苹果和虫子
//有m(0<x<100)个苹果, 吃一个苹果需要t(0~100)分钟 
//吃完一个立刻开始吃下一个
//现在过去了s(1~10000)分钟,请问他还有几个完整苹果

//输入三个非负数m,t,s
//输出一个整数 

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int m,t,s;
    int r;
    //剩余的苹果数
     
    //输入 
    cin >> m >> t >> s;
    //计算、
    //处理t = 0的情况 
    if( 0 ==t )
    {
        cout << 0 << endl;
        return 0;
        //结束代码 
    }
    
    //还需要考虑r不能<0的情况 
    
    if(s % t == 0)//不为0,说明开始吃下一个苹果,不过没吃完 
    {
        //r = m - s / t; //这里的t不能为0 
        if(m - s / t < 0) 
            r = 0;
        else
            r = m - s / t;
    }
        
    else
    {
        if( m - s / t - 1 < 0)
            r = 0;
        else
            r = m - s / t;
    }
    
    cout << r << endl;
    
    return 0;
}

//使用三目操作符改造为 

int main()
{
    int m,t,s;
    int r;
    //剩余的苹果数
     
    //输入 
    cin >> m >> t >> s;
    //计算、
    //处理t = 0的情况 
    if( 0 ==t )
    {
        cout << 0 << endl;
        return 0;
        //结束代码 
    }
    
    //还需要考虑r不能<0的情况 
    
  
 if(s % t == 0)//不为0,说明开始吃下一个苹果,不过没吃完 
        r = ( m - s / t < 0 ? 0 : m - s / t); 
        
    else
        r = (m - s / t - 1 < 0 ? 0 : m - s / - 1);

    
    cout << r << endl;
    
    return 0;
}

 

逻辑运算符

!  --逻辑取反

真假相反

&&  --逻辑与

需要运算符两侧表达式都为真,结果才为真,否则为假

||   --逻辑或

两侧表达式只要有一个为真,结果都为真

//闰年的判断 
//闰年输出1 否则输出0 

#include <iostream>

using namespace std;

int main()
{
    int n = 0;
    cin >> n;
    if(n % 4 == 0 && n % 100 != 0)
        cout << 1 << endl;
    else if(n % 400 == 0)
        cout << 1 << endl;
    else
        cout << 0 << endl;
    
    return 0;
 } 

//还可以使用逻辑运算符改为 

int main()
{
    int n = 0;
    cin >> n;
    if((n % 4 == 0 && n % 100 != 0) || (n % 400 == 0))
        cout << 1 << endl;
        
    else
        cout << 0 << endl;
    
    return 0;
 } 

 

//晶晶赴约会
//贝贝邀请晶晶出去玩,但是晶晶1,3,5都有课,必须上课
//请用数字1 ~ 7表示周一到周日,输入贝贝邀请晶晶的日期
//晶晶可以接受输出 YES,否则NO

#include <iostrem>
using namespace std; 

int main()
{
    int n;
    cin >> n;
  
 if( n == 1 || n == 3 || n == 5)
        cout << "NO"  << endl;
    else
        cout << "YES" << endl;
    
    return 0;
}

 

//三角形判断
//给定三个正整数,分别表示三角形三条边
//能构成三角形,输出1,否则0

#include <iostream>

using namespace std; 

int main()
{
    int a, b, c;
    cin >> a >> b >> c;
    
    if(a + b > c && a + c > b && b + c > a)
        cout << 1 << endl;
    else
        cout << 0 << endl; 
    
    
    return 0;

}

//给定一个整数
//判断其是否能被3,5,7整除
//1.能同时被3,5,7整除,输出3,5,7
//2.只能被其中两个整除,从小到大输出这两位数
//3.只能被一个数整除,输出这个数
//4.都不能被整除,输出n 

#include <iostream>

using namespace std; 

int main()
{
    int x;
    cin >> x;
    if(x % 3 == 0 && x % 5 == 0 && x % 7 == 0)
        cout << "3 5 7 " << endl;
    else if(x %3 == 0 & x % 7 == 0)
        cout << "3 7" <<  endl;
    else if(x % 5 == 0 && x % 7 == 0) 
        cout << "5 7" << endl;
    else if(x %3 == 0 & x % 5 == 0)        
        cout << "3 5" << endl;
    else if(x %3 == 0)
        cout << "3" << endl;
    else if(x % 5 == 0)
        cout << "5" << endl;
    else if(x % 7 == 0)    
        cout << "7" << endl;
    else  
        cout << "n" << endl;
        
    return 0;
}

使用了暴力枚举

//换一种思路,使用逻辑与运算符 

int main()
{
    int x;
    cin >> x;
    if( x % 3 == 0)
        cout << 3 << " " ;
    if(x % 5 == 0)
        cout << 5 << " ";
    if(x % 7 == 0)
        cout << 7 << " ";

        
    if(x % 3 != 0 && x % 5 != 0 && x % 7 != 0)

        cout << 'n' << endl;
        
    return 0;
}

但是对于代码的简洁性,前期可以不用追求,可以先暴力解决,再优化

//一些整数可能拥有以下性质:
//1.是偶数
//2.大于4且不大于12
//输入一个数字, 
//现在有4 种情况
//1.A喜欢性质1,2同时成立的数字 
//2.Uim:符合至少有一种性质
//2.B:刚好符合一个
//3.正妹:喜欢都不符合 
//
//输出 --4个人是否喜欢这个数字,喜欢输出1 不喜欢的输出0 
//输出用空格隔开---endl是转行 

#include <iostream>

using namespace std;
int main()
{
    int x;
    cin >> x;
    //判断和输出
    //按照顺序判断每个人是否喜欢这个数字
    
    //A
        if((x % 2 == 0) && (x > 4 && x <= 12)) 
        cout << 1 << " ";
    else
        cout << 0 <<  " ";
        
    //Uim
    if((x % 2 == 0) || (x > 4 && x <= 12)) 
        cout << 1 << " ";
    else
        cout << 0 << " ";
    
    //B
    if((x % 2 == 0) + (x > 4 && x <= 12) ==1 ) 
        cout << 1 << " ";
    else
        cout << 0 << " ";    
    
    //正妹
        if((x % 2 == 0) + (x > 4 && x <= 12) == 0) 
        cout << 1 << " ";
    else
        cout << 0 << " "; 
    
    return 0;
}

switch语句


是一种特殊的if else结构,将多重的if else转换成更易用更可行的形式
switch (exp)---express必须是整型表达式,可以是字符类型,不能是浮点型 
{
    case v1(value 1):statement---case后必须是整型常量表达式 
    case v2:statement
    default:statement
 } 

case是switch语句的决定分支的入口

break是switch语句的决定分支的出口

#include <iostream>

using namespace std;

int main()
{
    int n;
    cin >> n;
    switch(n % 3)
    {
        case 0:
            cout << "余数为0" << endl;
            break;
            
        case 1:
            cout << "余数为1" << endl;
            break;
        case 2:     
            cout << "余数为2" << endl;
            break;
    }

//如果没有break;输入任何一个数--将会打印出--余数为0--余数为1--余数为2
    //注意case:----后面跟的是:


//练习--四季,判断春夏秋冬 
//输入的数据格式是固定的 YYYYMM的形式 201901
//即月份占4个数位,月份占两个数位
//输出月份对应的季节(用英文单词,全小写) 


#include <iostream>
using namespace std; 

int main()
{
    int n,m;
    cin >> n; 
    m = n % 100;
    
    switch(m)
    {
        case 3:
        case 4:
        case 5:
            cout << "spring" << endl;
            break;
        case 6:
        case 7:
        case 8:
            cout << " summer" << endl;
            break;
        case 9:
        case 10:
        case 11:
            cout << "autum" << endl;
            break;
        case 12:
        case 1:
        case 2:
            cout << "winter" << endl;
            break;
    }
    
    return 0;
}

//使用if 语句


int main()
{
    int n ,m;
    cin >> n;
    m = n % 100;
    

        if(m >= 3 && m <= 5)
            cout << "spring" << endl;
        else if(m >= 6 && m <= 8)
            cout << "summer" << endl;
        else if(m >= 9 && m <= 11)
            cout << "autumn" <<endl;
        else
            cout << "winter" << endl;
        
    return 0;
 } 

//简单计算器
//支持+ - * /  只考虑整数运算
//如果出现除数为0 的情况,输出 Divided by zero!
//如果出现无效字符--输出  Invalid operator! 

#include <iostream>
using namespace std;

int main()
{
    int a,b;
    char c;
    cin >> a >> c >> b;
    
    switch(c)
    //对c进行判断 
    {
    case '+':
        cout << a + b << endl;
        break;
    case '-':
        cout << a - b << endl;
        break;
    case '*':
        cout << a * b << endl;    
        break;        
    case '/':
        if(b == 0)
            cout << "Divided by zero!" << endl;
        else
            cout << a / b  << endl;    
            break;
    default:
        cout<< "Invalid operator!" << endl;
        
    }

循环

while  与if语句非常相似

while(表达式)
{
    语句1;
    语句2; 
 } 

#include <iostream>
using namespace std;

int main()
{
    int i = 1;
    while(i <= 10)
    {
        cout << i << " ";
        i++;
        
    }
    
    
    return 0;
}

//输入一个整数
//求这位数的数位和
//--获取这个数的每一位

#include <iostream>
using namespace std;

int main()
{
    int n;
    int sum = 0;
    cin >> n;
    while(n)
    {
        sum += (n % 10);
        n /= 10;
    }
    cout << sum << endl;
    
    return 0;
 } 

如果不太明白可以调试看看~

//整数求和
//输入一个整数
//求出这个数及比其小的所有正整数之和
//预期输出50000000500
//---说明应该使用longlong 

#include <iostream>
using namespace std;

int main()
{
    int n;
    long long sum = 0;
    cin>> n;
    int i = 1;
    while(i <= n)
    {
        sum += i;
        i++;
    }
    
    cout << sum << endl;
    
    
    return 0;
 } 

//可以使用求和公式
//(1 + n) * n--只有等差数列才可以这样 

int main()
{
    long long n;
    long long sum = 0;
    cin >> n;
    sum = (1 + n) * n /2;
    cout << sum << endl;

    return 0;    
}

 


//含k个3的数
//输入两个正整数m和k,(1 < m <= 10^15),(1 < k <= 10)
//m--long long ,k--int 
//判断m是否刚好含有k个3,满足条件输出YES,否则NO; 

#include <iostream>

using namespace std;

int main()
{
    long long m;
    int k;
    int c =0;
    cin >> m >> k;
    //统计m中有几个3
    while(m)
    {
        if(m % 10 == 3)
            c++;
        m /= 10;
     }
     if(c == k) 
         cout << "YES" << endl;
     else
         cout << "NO" << endl;
    
    return 0;
}

123 1--YES

//角谷猜想、/
//输入任意整数
//如果其是奇数,则乘3加1,
//如果是偶数,则除以2,
//得到的结果按 上述重复处理 最终总能得到1 
//输出一个整数 将经过处理得到的1的过程输出 

用C语言做

#include <iostream>

#include <cstdio>

using namespace std;

int main()
{
    long long n= 0;
    //long long 使用printf时是%lld 
    cin >> n;
    while(n != 1)
    {
        if(n % 2 ==1)
        {
          
 printf("%lld * 3 + 1 = %lld\n",n,n * 3 + 1);
            n = n * 3 + 1;

        }
        else
        {
          
 printf("%lld/2=%lld\n",n,n / 2);
            n /= 2;

        }
        
    }
    cout << "End" << endl;
    
    return 0;
}

//用c++方式 

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    long long n= 0;
    //long long 使用printf时是%lld 
    cin >> n;
    while(n != 1)
    {
        if(n % 2 ==1)
        {
         
   cout << n << "*3+1=" << n * 3 +1 << endl;
            n = n * 3 + 1;
        }
        else
        {
        
   cout << n <<"/2=" << n / 2 << endl;
            n /= 2;
        }
        
    }
    cout << "End" << endl;
    
    return 0;
}

 

//计算多项式的和 
//假设多项式的形式为x^n + x^(n-1)+...+x^2 + x + 1
//请给定单精度浮点数x和正整数n值的情况下这个多项式的值
//多项式的值精确到小数点后两位,最终结果保证在double类型
//输入一行,包括x和m,空格隔开 
//输出一个实数,即多项式的和 

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int n;
    double x;
    double sum =1;
    double r = 1;
    cin >> x >> n;

    while(n--)//n次询问 
    {
        r *= x;
        sum += r;
    
     } 

     printf("%.2f\n",sum);
    
    return 0;
}

 

更多推荐