东方博宜OJ题解

拆位练习

1605: 【入门】求一个两位数的个位和十位的和

#include <bits/stdc++.h>
using namespace std;
int main(){
    int n,g=0,s=0;
    cin>>n;
    g=n%10;
    s=n/10;
    cout<<g+s;
    return 0;
}

1606: 【入门】求一个两位数倒序的结果

#include <bits/stdc++.h>
using namespace std;
int main(){
    int n,g=0,s=0;
    cin>>n;
    g=n%10;
    s=n/10;
    if(g==0){
        cout<<s;
    }else{
        cout<<g<<s;
    }
     
    return 0;
}

1027: 【入门】求任意三位数各个数位上数字的和

#include<bits/stdc++.h>
using namespace std;
int main(){
	int x,g,s,b=0;
	cin>>x; 
	g=x%10;
	s=x/10%10;
	b=x/100;
	cout<<g+s+b;
	

	return 0;
}

1028: 【入门】输入一个三位数,把个位和百位对调后输出

本人题解(较繁琐,可参考下方较优题解):

#include<bits/stdc++.h>
using namespace std;
int main(){
	int x,g,s,b=0;
	cin>>x; 
	g=x%10;
	s=x/10%10;
	b=x/100;
	if(g==0&&s==0){
		cout<<b;
	}else if(g==0){
		cout<<s<<b;
	}else{
		cout<<g<<s<<b;
	}
	
	

	return 0;
}

较优题解(转载于https://blog.csdn.net/qq_35463630/article/details/120942442?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163568811516780269875107%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=163568811516780269875107&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-1-120942442.first_rank_v2_pc_rank_v29&utm_term=%E4%B8%9C%E6%96%B9%E5%8D%9A%E5%AE%9C%E7%A8%8B%E5%BA%8F%E9%A2%98%E8%A7%A3&spm=1018.2226.3001.4187):

#include <iostream>
using namespace std;

int main(){
	int n;
	cin>>n;
	cout<<n%10*100+n/100+n%100/10*10;
	return 0;
}


上一篇:SWUST OJ 281: 逃跑的蠕虫


下一篇:华为OJ-元辅音大小写转换