当前位置: 首页 > news >正文

《算法笔记》3.1小节——入门模拟->简单模拟

1001 害死人不偿命的(3n+1)猜想

在这里插入图片描述

#include <iostream>
using namespace std;


int main() {
    int n,count=0;
    cin>>n;
    while(n!=1){
        if(n%2==0) n/=2;
        else n=(3*n+1)/2;
        count+=1;
    }
    std::cout <<count;
    return 0;
}

1032 挖掘机技术哪家强

在这里插入图片描述

#include <iostream>
using namespace std;
const int maxn=1e5+10;

int main() {
    int n;
    cin>>n;
    int scores[maxn]={0};
    int schoid,score;
    for (int i = 1; i <= n; ++i) {
        cin>>schoid>>score;
        scores[schoid]+=score;
    }
    int max_score=-1,max_schoid=-1;
    for (int i = 1; i <= n; ++i) {
        if(scores[i]>max_score){
            max_score=scores[i];
            max_schoid=i;
        }
    }
    cout<<max_schoid<<' '<<max_score;
    return 0;
}

剩下的树

在这里插入图片描述

#include <iostream>
using namespace std;
const int maxn=1e4+10;
int main() {
    int tree[maxn];
    int m,n,bin,end;
    while(cin>>n>>m){
        fill(tree,tree+maxn,1);
        int count=0;//要在这里初始化
        if(m==0) break;
        for (int i = 0; i <m; ++i) {
            cin>>bin>>end;
            for (int j = bin; j <=end; ++j) {
                tree[j]=0;
            }
        }
        for (int i = 0; i <=n; ++i) {
            if(tree[i]==1) count++;
        }
        cout<<count<<endl;
    }
    return 0;
}

A+B

在这里插入图片描述

#include <iostream>
#include<string>
using namespace std;

int to_int(char str[]){
    char str1[13];
    int j=0;
    for (int i = 0; str[i]!='\0'; ++i) {
        if(str[i]==',') continue;
        else {
            str1[j]=str[i];
            j++;
        }
    }
    str1[j]='\0';
    int result=0;
    int sign=1;
    int i=0;
    if(str1[0]=='-'){
        sign=-1;
        i=1;
    }
    for (;str1[i]!='\0'; ++i) {
        result=result*10+(str1[i]-'0');
    }
    return result*sign;
}

int main() {
    char str1[13],str2[13];
    while(cin>>str1>>str2){
        int num1=to_int(str1);
        int num2= to_int(str2);
        int ans=num1+num2;
        cout<<ans<<endl;
    }
    return 0;
}

特殊乘法

在这里插入图片描述

#include <iostream>
using namespace std;
int main() {
    char a[10],b[10];
    while(cin>>a>>b){
        int mul=0;
        for (int i = 0;a[i]!='\0'; ++i) {
            for (int j = 0; b[j]!='\0'; ++j) {
                mul+=(a[i]-'0')*(b[j]-'0');
            }
        }
        cout<<mul<<endl;
    }
    return 0;
}

比较奇偶数个数

在这里插入图片描述

#include <iostream>
using namespace std;

int main() {
    int n,m,even=0,odd=0;
    while(cin>>n){
        for (int i = 0; i < n; ++i) {
            cin>>m;
            if(m%2==0) even++;
            else odd++;
        }
        if(odd>even) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}

Shortest Distance (20)

在这里插入图片描述

#include <cstdio>
const int maxn=1e5+10;
int main() {
    int n,m,bin,end,forward,total=0;
    int exits[maxn];
    int preSum[maxn]={0};//使用前缀和
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &exits[i]);
        total+=exits[i];
        preSum[i]=preSum[i-1]+exits[i];
    }
    scanf("%d", &m);
    for (int i = 0; i < m; ++i) {
        forward=0;
        scanf("%d %d", &bin, &end);
        if (bin > end) {
            int temp = bin;
            bin = end;
            end = temp;
        }
        forward+=preSum[end-1]-preSum[bin-1];
        int result=forward<(total-forward)?forward:total-forward;
        printf("%d\n", result);
    }
    return 0;
}

A+B和C (15)

在这里插入图片描述

#include <iostream>
using namespace std;

int main() {
    int n;
    long int a,b,c;
    cin>>n;
    for (int i = 1; i <= n; ++i) {
        cin>>a>>b>>c;
        if(a+b>c)
            cout<<"Case #"<<i<<": true"<<endl;
        else
            cout<<"Case #"<<i<<": false"<<endl;
    }
    return 0;
}

数字分类 (20)

在这里插入图片描述

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    int n,m;
    while(cin>>n){
        int c1=0,c2=0,o2=0,c3=0,c4=0,n4=0,c5=-1;
        for (int i = 0; i < n; ++i) {
            cin>>m;
            if(m%5==0&&m%2==0) c1+=m;
            if(m%5==1) {
                c2+=(o2%2==0)?m:-m;//这里注意是o2
                o2++;
            }
            if(m%5==2) c3++;
            if(m%5==3) {
                c4++;
                n4+=m;
            }
            if(m%5==4) {
                if(m>c5) c5=m;
            }
        }
        double result4;
        if(c4!=0) result4=1.0*n4/c4;
        if(c1!=0) cout<<c1<<' ';else cout<<"N ";
        if(o2>0) cout<<c2<<' ';else cout<<"N ";
        if(c3!=0) cout<<c3<<' ';else cout<<"N ";
        if(c4!=0) cout<<fixed<<setprecision(1)<<result4<<' ';else cout<<"N ";
        if(c5!=-1) cout<<c5<<endl;else cout<<'N'<<endl;
    }
    return 0;
}

部分A+B (15)

在这里插入图片描述

#include <iostream>
using namespace std;

int find_part(char a[],char da){
    int count=0,res=0;
    for (int i = 0; i < 11; ++i) {
        if(a[i]==da) count++;
    }
    for (int i = 0; i < count; ++i) {
        res=res*10+da-'0';
    }
    return res;
}

int main() {
    char a[11],da,b[11],db;
    while(cin>>a>>da>>b>>db){
        int res1= find_part(a,da);
        int res2= find_part(b,db);
        int res=res1+res2;
        cout<<res<<endl;
    }
    return 0;
}

锤子剪刀布

在这里插入图片描述

#include <iostream>
using namespace std;
const int maxn=1e5+10;

int main() {
    int n,ping=0,jiaying=0,jiashu=0,yiying=0,yishu=0;
    cin>>n;
    char jia[maxn],yi[maxn];
    int a[3]={0},b[3]={0};//注意用int类型
    for (int i = 0; i < n; ++i) {
        cin>>jia[i]>>yi[i];
        if(jia[i]==yi[i]) ping++;
        switch (jia[i]) {
            case 'C':
                switch (yi[i]) {
                    case 'J':
                        jiaying++;
                        yishu++;
                        a[0]++;
                        break;
                    case 'B':
                        jiashu++;
                        yiying++;
                        b[2]++;
                        break;
                }
                break;
            case 'J':
                switch (yi[i]) {
                    case 'B':
                        jiaying++;
                        yishu++;
                        a[1]++;
                        break;
                    case 'C':
                        jiashu++;
                        yiying++;
                        b[0]++;
                        break;
                }
                break;
            case 'B':
                switch (yi[i]) {
                    case 'C':
                        jiaying++;
                        yishu++;
                        a[2]++;
                        break;
                    case 'J':
                        jiashu++;
                        yiying++;
                        b[1]++;
                        break;
                }
                break;
        }
    }
    cout<<jiaying<<' '<<ping<<' '<<jiashu<<endl;
    cout<<yiying<<' '<<ping<<' '<<yishu<<endl;
    int maxa=0,indexa=0;
    int maxb=0,indexb=0;
    for (int i = 0; i < 3; ++i) {
        if(a[i]>maxa){
            maxa=a[i];
            indexa=i;
        }
        if(b[i]>maxb){
            maxb=b[i];
            indexb=i;
        }
    }
    if(indexa==0 && a[0]==a[2]) indexa=2;
    if(indexb==0 && b[0]==b[2]) indexb=2;
    if(indexa==1 && a[1]==a[2]) indexa=2;
    if(indexb==1 && b[1]==b[2]) indexb=2;
    char handa=(indexa==0)?'C':(indexa==1)?'J':'B';
    char handb=(indexb==0)?'C':(indexb==1)?'J':'B';
    cout<<handa<<' '<<handb<<endl;
    return 0;
}

相关文章:

  • C#: DxF文件中Spline解析
  • 【前缀和】矩阵区域和(medium)
  • 支付系统设计入门:核心账户体系架构
  • C++ 入门三:函数与模板
  • windows主机中构建适用于K8S Operator开发环境
  • 【软考-高级】【信息系统项目管理师】【论文基础】进度管理过程输入输出及工具技术的使用方法
  • 编程语言中变量定义方式的深度剖析
  • 简化DB操作:Golang 通用仓库模式
  • 【家政平台开发(33)】库存管理模块开发实战:从基础搭建到智能管控
  • 简单实现逆波兰表达式求值
  • C++_智能指针
  • 如何从零构建一个自己的 CentOS 基础镜像
  • WinForm真入门(14)——ListView控件详解
  • Work Experience
  • java相关技术总结
  • 在 openEuler 24.03 (LTS) 操作系统上添加 ollama 作为系统服务的步骤
  • 如何在Android系统上单编ko?
  • c++基础知识二
  • 剑指offer经典题目(三)
  • 基于springboot的“协同过滤算法的高考择校推荐系统”的设计与实现(源码+数据库+文档+PPT)