1.求长方形的面积和周长

#include<bits/stdc++.h> 
using namespace std;
int main()
{
    int a,b,S,P;
    cin>>a>>b;
    S=a*b;
    P=2*(a+b);
    cout<<S<<" "<<P;
    return 0;
}

没什么需要特别注意,主要是万能头文件bits/stdc++.h

2.数列和

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b;
    cin>>a;
    b=a*(a+1)/2;
    cout<<b;
    return 0;
}

s=n*(a1+an)/2或s=na1+n(n-1)d/2

3.解方程

#include<bits/stdc++.h>
using namespace std;
int main()
{
    float a,b,x;
    cin>>a>>b;
    x=(5-3*b)/(2*a);
    cout<<fixed<<setprecision(1)<<x;
    return 0;
}

反解即可,保留一位小数,注意要把变量设成float或double型,不然行不通,而且不能忘记fixed,不然是保留一位有效位,加上fixed,是保留一位有效小数

4.一个月的天数

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int year,month;
    cin>>year>>month;
    int days;
    if((year%4==0&&year%100!=0)||year%400==0)
    {
        switch (month){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days=31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                days=30;
                break;
            case 2:
                days=29;
                break;
        }
    }
    else{
        switch (month){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days=31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                days=30;
                break;
            case 2:
                days=28;
                break;        
        }
    }
    cout<<days;
    return 0;
}

闰年咋判断有点忘记了,闰年为可以整除以400或者  可以整除以4但不能整除以100的数

switch语句自己用的比较少

5.银行存款到期日

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int year,month,day,term,newmonth = 0; // 初始化newmonth,避免未定义行为
    cin>>year>>month>>day>>term;
    
    // 修复1:处理月份为12的整数倍的情况(核心错误)
    if((month+term)>12)
    {
        newmonth=(month+term)%12;
        // 当余数为0时,月份应为12,年份减1(因为多算了一次)
        if(newmonth == 0){
            newmonth = 12;
            year = year + ((month+term)/12) - 1;
        }else{
            year=year+((month+term)/12);
        }
    }
    else 
    {
        newmonth=month+term;
    }
    
    // 修复2:修正2月天数判断(原代码默认29天,未区分平年)
    if(day==31)
    {
        switch(newmonth){
            case 4:
            case 6:
            case 9:
            case 11:
                day=30; 
                break;
            case 2:
                // 平年2月修正为28,闰年保留29
                if(year%400==0||(year%4==0&&year%100!=0)){
                    day=29;
                }else{
                    day=28;
                }
                break;
        }
    }
    else if(day==30)
    {
        if(year%400==0||(year%4==0&&year%100!=0))
        {
            switch(newmonth){
                case 2:
                    day=29;
                    break;
            }
        }
        else{
            switch(newmonth){
                case 2:
                    day=28;
                    break;
            }
        }
    }
    else if(day==29)
    {
        if(year%400==0||(year%4==0&&year%100!=0))
        {
            switch(newmonth){
                case 2:
                    day=29;
                    break;
            }
        }
        else{
            switch(newmonth){
                case 2:
                    day=28;
                    break;
            }
        }
    }
    cout<<year<<" "<<newmonth<<" "<<day;
    return 0; 
}

刚开始写的时候,逻辑有点混乱,以为只定期存3个月或6个月,后面才发现定期月份不限制

主要思路是先区分,定期存款的时间,会不会影响到年份的变化

1.如果当前月份加上存款月份大于12,也就是到了下一年,就要更新年份和月份

 newmonth=(month+term)%12;
            year=year+((month+term)/12);}//

2.如果当前月份小于等于12,则不需要更新年份,单纯更新月份即可

 newmonth=month+term;

但是写的时候发现提交的也有逻辑问题,month=12, term=12totalMonth=2424%12=0,修正后newmonth=12,且年份只加 1(而非 2)。

switch语句中,

  • 无 default 且所有 case 不匹配:程序会直接跳过整个 switch 代码块,不会执行 switch 内任何代码,直接执行 switch 之后的内容。
  • “顺序往后执行” 是case 匹配但缺少 break导致的 “穿透现象”,和是否写 default 无关。
  • 建议在 switch 中始终添加 default 分支(哪怕只写一行注释),可以明确处理所有未匹配的情况,提升代码健壮性。
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐