思路:

2种方法:

1.用数字表示:由于要输出的是后i位数字,那么就一定会有首位为0的情况,有一下解决办法:

1.直接使用整形数来存储最后结果,最后设置输出的长为i再将填充符设置为0即可.

2.使用数组来分别存储每一位数字,

个人更推荐这里的第二种方法:

#include<bits/stdc++.h>
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int x,i;
    cin>>x>>i;
    vector<int>ans(i);
    while(i--){//这里i是要分离的数字的位数,在while循环判断的时候为i,判断完之后自减1,为i-1.
        ans[i] = x%10;
        x/=10;
    }
    for(int i:ans){
        cout<<i;
    }
    
}

2.转化为字符串后输出:

转化之后直接输出后3位即可:

#include<bits/stdc++.h>
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int x,i;
    cin>>x>>i;
    string s=to_string(x);
    for(int k=s.length()-i;k<=s.length()-1;k++){
        cout<<s[k];
    }    
}