C++程序设计实践一上(题目来自杭州电子科技大学ACM)

题目一: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;
}

这个题目不知道为什么结果一直不对, 后了解应是题目中对数据的限制比较小,要判断的数据可能是小数,精度问题等,应该转化为字符串进行比较,验证了我们要时刻注意着数据的边界条件以及题干中的限制范围,合理输出。

后面修改了这个题目代码,过了

  • 25
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
杭州电子科技大学ACM是指杭州电子科技大学(Hangzhou Dianzi University)学生所组成的参与国际大学生程序设计竞赛(ACM-ICPC)的团队。ACM-ICPC是全球规模最大、最具影响力的大学生程序设计竞赛,由美国计算机协会(ACM)主办。 作为杭州电子科技大学的代表,ACM团队的成员经过选拔和培训,是学校优秀的计算机科学与技术专业学生。这些学生掌握了扎实的算法和编程基础,具有丰富的解决问题的能力和团队协作精神。 参加ACM竞赛对于学生们来说,不仅是锻炼自己的编程能力和算法思维,更是一个展示才华和拓宽视野的平台。比赛中,团队成员将面对各种难题,需要快速思考并给出高效的解决方案,这对于他们的综合素质和实践能力是一个重要的考验。 杭州电子科技大学ACM团队在比赛中取得了优异的成绩,多次进入国际赛区的决赛,甚至获得了国际冠军头衔。这不仅彰显了团队成员的个人才华,更体现了学校培养优秀计算机人才的实力和质量。 ACM竞赛的参与不仅对于学生个人的成长和发展有积极的影响,也对学校提升学科建设和促进学院声誉有着重要的意义。杭州电子科技大学ACM团队的出色表现是学校计算机学科实力的一大体现,也是学校教育理念和教学质量的有力证明。 总的来说,杭州电子科技大学ACM是一支备受期待和骄傲的团队,他们的参赛经历和成果不仅彰显了他们个人的才华,更代表了学校在计算机科学与技术领域的卓越实力和卓越教育质量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

筱姌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值