题目一:2000.ASCLL码排序
Problem Description
输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。
Input
输入数据有多组,每组占一行,有三个字符组成,之间无空格。
Output
对于每组输入数据,输出一行,字符中间用一个空格分开。
#include<iostream>
using namespace std;
int main() {
char a, b, c;
while(cin >> a >> b >> c) {
if(a > b) {
swap(a, b); // 如果a的ASCII值大于b,交换a和b
}
if(a > c) {
swap(a, c); // 如果交换后a的ASCII值仍然大于c,交换a和c
}
if(b > c) {
swap(b, c); // 确保b的ASCII值不大于c
}
cout << a << " " << b << " " << c << endl;
}
return 0;
}
题目二:2051.Bitset
Problem Description
Give you a number on base ten,you should output it on base two.(0 < n < 1000)
Input
For each case there is a postive number n on base ten, end of file.
Output
For each case output a number on base two.
#include <iostream>
#include <string>
using namespace std;
int main() {
int n;
while(cin >> n) {
if (n < 1 || n > 1000) {
return 1;
}
string s = "";
while (n > 0) {
s= to_string(n % 2) + s;
n /= 2;
}
cout << s<< endl;
}
return 0;
}
题目三:2052.Picture
Problem Description
Give you the width and height of the rectangle,darw it.
Input
Input contains a number of test cases.For each case ,there are two numbers n and m (0 < n,m < 75)indicate the width and height of the rectangle.Iuput ends of EOF.
Output
For each case,you should draw a rectangle with the width and height giving in the input.
after each case, you should a blank line.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int w, h;
while (cin >> w >> h) {
if (w <= 0 || h <= 0 || w > 75 || h > 75) {
continue;
}
cout << '+';
for (int i = 0; i < w ; ++i) {
cout << '-';
}
cout << '+' << endl;
for (int i = 0; i < h ; ++i) {
cout << '|';
for (int j = 0; j < w; ++j) {
if (j == 0 || j <= w ) {
cout << ' ';
}
else {
cout << '|';
}
}
cout << '|' << endl;
}
cout << '+';
for (int i = 0; i < w ; ++i) {
cout << '-';
}
cout << '+' << endl;
cout << endl;
}
return 0;
}
题目四:2053.Switch Game
Problem Description
There are many lamps in a line. All of them are off at first. A series of operations are carried out on these lamps. On the i-th operation, the lamps whose numbers are the multiple of i change the condition ( on to off and off to on ).
Input
Each test case contains only a number n ( 0< n<= 10^5) in a line.
Output
Output the condition of the n-th lamp after infinity operations ( 0 - off, 1 - on ).
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int n;
while (cin >> n) {
int sqrt_n = sqrt(n);
// 由于可能存在精度问题,我们需要检查sqrt_n的平方是否等于n
if (sqrt_n * sqrt_n == n) {
// 如果是完全平方数,则第n个灯最终是开的
cout << 1 << endl;
}
else {
// 否则,第n个灯最终是关的
cout << 0 << endl;
}
}
return 0;
}
题目五:2054.A= =B?
Problem Description
Give you two numbers A and B, if A is equal to B, you should print "YES", or print "NO".
Input
each test case contains two numbers A and B.
Output
for each case, if A is equal to B, you should print "YES", or print "NO".
#include<iostream>
#include<string>
using namespace std;
void senre(string &a){
int temp=0;
if(a.find('.')!=a.npos){
while(*(a.end()-1-temp)=='0'){
temp++;
}
a.resize(a.size()-temp);
if(*(a.end()-1)=='.'){
a.resize(a.size()-1);
}
}
while(*(a.begin())=='0'){
a.erase(a.begin());
}
}
int main()
{
string a;
string b;
while(cin>>a>>b){
senre(a);
senre(b);
if(a==b){
cout<<"YES"<<endl;
}
else{
cout<<"NO"<<endl;
}
}
return 0;
}
这个题目不知道为什么结果一直不对, 后了解应是题目中对数据的限制比较小,要判断的数据可能是小数,精度问题等,应该转化为字符串进行比较,验证了我们要时刻注意着数据的边界条件以及题干中的限制范围,合理输出。
后面修改了这个题目代码,过了