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

简介: 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;
}

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

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

目录
相关文章
|
5天前
|
关系型数据库 MySQL 测试技术
技术分享:深入C++时间操作函数的应用与实践
技术分享:深入C++时间操作函数的应用与实践
11 1
|
8天前
|
C++
C++ : 程序设计简单实例
C++ : 程序设计简单实例
13 3
|
8天前
|
安全 C++
C++:程序设计实例
C++:程序设计实例
11 2
|
10天前
|
C++
C++解决线性代数矩阵转置 小实践
【6月更文挑战第3天】C++解决线性代数矩阵转置
16 2
|
16天前
|
存储 算法 安全
用C++打造极致高效的框架:技术探索与实践
本文探讨了如何使用C++构建高性能框架。C++凭借其高性能、灵活性和跨平台性成为框架开发的理想选择。关键技术和实践包括:内存管理优化(如智能指针和自定义内存池)、并发编程(利用C++的并发工具)、模板与泛型编程以提高代码复用性,以及性能分析和优化。在实践中,应注意代码简洁性、遵循最佳实践、错误处理和充分测试。随着技术发展,不断提升对框架性能的要求,持续学习是提升C++框架开发能力的关键。
26 1
|
20天前
|
存储 搜索推荐 C++
C++课程设计实验杭州电子科技大学ACM题目(中)
C++课程设计实验杭州电子科技大学ACM题目(中)
15 1
|
1天前
|
C++
技术经验分享:C++程序设计的技巧
技术经验分享:C++程序设计的技巧
|
14天前
|
存储 算法 安全
面向对象程序设计C++
面向对象程序设计C++
|
20天前
|
存储 人工智能 测试技术
C++课程设计实验杭州电子科技大学ACM题目(下)
C++课程设计实验杭州电子科技大学ACM题目(下)
12 0
|
20天前
|
存储 C++
C++课程设计实验杭州电子科技大学ACM题目(上)
C++课程设计实验杭州电子科技大学ACM题目(上)
11 0