欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 资讯 > C++算法竞赛基础语法——12

C++算法竞赛基础语法——12

2025/2/25 5:35:33 来源:https://blog.csdn.net/2402_86622585/article/details/145825198  浏览:    关键词:C++算法竞赛基础语法——12

 输入输出cin/cout

万能头文件#include <bits/stdc++.h>

加速指令

#include <iostream>
using namespace std;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);//加速指令后不同时使用cin/cout与scanf/printf

    int a, b;
    cin >> a >> b;  
    int sum = a + b; 
    cout << "sum=" << sum << endl;
    return 0;
}

%s——string

%c——char

%lf——doule

%lld——long long

%llu——unsigned long long

多组数据的输入

奇数偶数统计——以0为结束标志

#include <iostream>
using namespace std;
int main()
{
    int n,J,O;
    while(cin>>n)
    {
        if(n==0) break;
        else
        {
            if(n%2==0) J++;
            else O++;
        }
    }
    cout<<J<<" "<<O<<endl;
    return 0;
}

奇数偶数统计——无以0结束标志"Ctrl+z再按下回车结束程序"

#include <iostream>
using namespace std;
int main()
{
    int n,J,O;
    while(cin>>n)
    {
            if(n%2==0) J++;
            else O++;
    }
    cout<<J<<" "<<O<<endl;
    return 0;
}

getline读入输入的字符串中空格的数量 

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str;
    getline(cin,str);
    int count=0;
    int n=str.length() ;
    for(int i=0;i<n;i++)
    {
        if(str[i]==' ')
        {
            count++;
        }
    } 
    cout<<count<<endl;
    return 0;
}

stoi函数将string转化为数字,字符串转换为数字to_string

#include <iostream>
#include <string>
using namespace std;
int main()
{
     
    int n=12345;
    string str;
    str=to_string(n);
    cout<<str<<endl;
    string str1;
    str1="67890";
    int n1;
    n1=stoi(str1);
    cout<<n1; 
    return 0;
}

日期问题

计算2025年2月24号是2025年的第55天

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int answer=0;
    for(int year = 2025; year <=2025; year++)
    {
        for(int month = 1; month <= 12; month++)
        {
            for(int day = 1; day <= 31; day++)
            {
                if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
                {
                   
                    if(day > 31) break;
                }
                else if(month == 2)
                {
                    if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
                    {
                        if(day > 29) break;
                    }
                    else
                    {
                        if(day > 28) break;
                    }
                }
                else
                {
                    if(day > 30) break;
                }
                answer++;
                if(month==2&&day==24)
                {
                    cout<<answer;
                    return 0;
                }
            }
        }
    }
    return 0;
}

计算经过一段时间过后的年月日

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int flag=0,count=0;
    for(int year = 2024; year <=2030; year++)
    {
        for(int month = 1; month <= 12; month++)
        {
            for(int day = 1; day <= 31; day++)
            {
                if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
                {
                   
                    if(day > 31) break;
                }
                else if(month == 2)
                {
                    if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
                    {
                        if(day > 29) break;
                    }
                    else
                    {
                        if(day > 28) break;
                    }
                }
                else
                {
                    if(day > 30) break;
                }
                if(year==2024&&month==4&&day==21) 
                {
                    flag=1;
                }
                if(flag==1) count++;
                if(count==1001)
                {
                    cout<<year<<"年"<<month<<"月"<<day<<"号";
                    break;
                }
            }
        }
    }
    return 0;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词