YACS-202108

YACS-202108

阅读竞赛

http://iai.sh.cn/problem/462

#include<bits/stdc++.h>
using namespace std;

int a,b,x,y;
int main(){
    cin>>a>>b>>x>>y;
    int diff=b-a;
    if(diff<0){
        cout<<0;
        return 0;
    }
    int ans=0;
    while(diff>=0){
        ans++;
        diff=diff-(x-y);
        if(diff>=b-a){
            cout<<"Impossible";
            return 0;
        }
    }
    cout<<ans;
}

罗马数字

http://iai.sh.cn/problem/478

#include<bits/stdc++.h>
using namespace std;
int n; 
int a[10];
int main(){
    string str[4000];
    str[1]="I";
    str[2]="II";
    str[3]="III";
    str[4]="IV";
    str[5]="V";
    str[6]="VI";
    str[7]="VII";
    str[8]="VIII";
    str[9]="IX";
    
    str[10]="X";
    str[20]="XX";
    str[30]="XXX";
    str[40]="XL";
    str[50]="L";
    str[60]="LX";
    str[70]="LXX";
    str[80]="LXXX";
    str[90]="XC";
    
    str[100]="C";
    str[200]="CC";
    str[300]="CCC";
    str[400]="CD";
    str[500]="D";
    str[600]="DC";
    str[700]="DCC";
    str[800]="DCCC";
    str[900]="CM";
    
    str[1000]="M";
    str[2000]="MM";
    str[3000]="MMM";
    
    cin>>n;
    int i=0,pow=1;
    while(n>0){
        i++;
        a[i]=(n%10)*pow;
        n/=10;
        pow*=10;
    }
    for(int j=i;j>0;j--){
        cout<<str[a[j]];
    }
    
}

四方定理

http://iai.sh.cn/problem/477

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL n;
const int maxC=5e4+10;
LL ans[maxC];
//step 深度几个数 
//sum 几个数的和 
void dfs(LL step, LL sum){
    if (step>4){//深度4以后 保证有四个数相加 
        if (sum==n){//四个数的和等于n 
            for (LL i=1; i<=4; i++){//输出这四个数 
                cout<<ans[i]<<" ";
            }
            cout<<"\n";
        }
        return ;
    }
    for (LL i=0; i*i<=n; i++){
        if (ans[step-1]<=i){//前一个数必须小于等于当前数 
            ans[step]=i;//当前数记录到数组 
            dfs(step+1,sum+i*i);//递归下一个数 总和加上当前数 
        }
    }
}
int main(){
    cin>>n;
    dfs(1,0);//第一个数 此时和为0 
    return 0;
}

栈的判断

http://iai.sh.cn/problem/473

#include<bits/stdc++.h>
using namespace std;
int n,temp;
stack<int> st;
//1.使用出栈顺序逐一模拟入栈 2.小于等于当前数则入栈 
//3.入栈后判断第一个是否和当前数相等 相等则出栈判断下一个 不相等则不符合规则退出 
int main(){
    cin>>n;
    int idx=1;
    bool flag=true;
    for(int i=1;i<=n;i++){
        cin>>temp;//逐一读取出栈的数 
        while(temp>=idx){//循环读入比当前数小的数入栈 
            st.push(idx);//放入栈中 
            idx++;//idx加一 变成下一个数等待入栈 
        }
        if(st.top()!=temp){//如果栈中最后一个数和当前读取数不相等 则和出栈顺序不一致 
            flag=false;//设置无效标识 退出循环 
            break;    
        }else{
            st.pop();//相等 出栈 
        }
    }
    
    if(flag){
        cout<<"Valid";
    }else{
        cout<<"Invalid";
    }
} 

子集和

http://iai.sh.cn/problem/476

#include<bits/stdc++.h>
using namespace std;

int a[30];
int sum = 0,n,t,ans;//n为集合的大小,t为目标值 
//逐一加数 每个数都可以取和不取两种 
void dfs(int step){
    if (step == n){//递归n个数 
        if (sum == t){//sum和目标值相同 一次方案 
            ans++;
            return;
        }
    }else{
        sum += a[step];
        dfs(step + 1);//取当前值计算一次 
        sum -= a[step];//回溯时先还原
        dfs(step + 1);//不取当前值计算一次 
    }
}
int main(){
    cin>>n>>t;
    for (int i = 0; i < n; i++)
        cin>>a[i];
    dfs(0);//从第一个数开始取 
    if(ans>0){
        cout<<"Yes";
    }else{
        cout<<"No";
    }
    return 0;
}
posted @ 2021-10-06 14:37  new-code  阅读(35)  评论(0)    收藏  举报