C++练习02

//计算复数
#include <iostream>
#include <iomanip>
using namespace std;
typedef struct{
    float x;
    float y;
}Comp;
//打印复数的函数
void Printer(Comp res) {
    if (res.x == 0 && res.y == 0) {
        cout << "0.00" << endl;
    }
    else {
        // 输出实部(如果实部不为0)
        if (res.x != 0) {
            cout << fixed << setprecision(2) << res.x;
        }
        
        // 输出虚部
        if (res.y != 0) {
            // 只有当实部不为0且虚部为正数时,才需要加号
            if (res.y > 0 && res.x != 0) {
                cout << "+";
            }
            cout << fixed << setprecision(2) << res.y << "i";
        }
        cout << endl;  // 换行放在最后
    }
}
//计算加法的函数
Comp add(Comp a,Comp b){
    Comp res;
    res.x=a.x+b.x;
    res.y=a.y+b.y;
    return res;
}
//计算减法的函数
Comp subtract(Comp a,Comp b){
    Comp res;
    res.x=a.x-b.x;
    res.y=a.y-b.y;
    return res;
}
int main(){
    Comp a,b;
    cin>>a.x>>a.y>>b.x>>b.y;
    Comp res1,res2;
    res1=add(a,b);
    res2=subtract(a,b);
    Printer(res1);
    Printer(res2);
    return 0;
}
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
struct stud {
    int num;             // 学号
    char name[10];       // 姓名
    int score[3];        // 3门课成绩
    int sum;             // 总分
};
int main() {
    int N;
    cin >> N;
    vector<stud> arr(N);
    for (int i = 0; i < N; i++) {// 输入学生信息
        cin >> arr[i].num >> arr[i].name >> arr[i].score[0] >> arr[i].score[1] >> arr[i].score[2];
    }
    
    for (int i = 0; i < N; i++) {// 计算每个学生的总分
        arr[i].sum = arr[i].score[0] + arr[i].score[1] + arr[i].score[2];
    }
    
    for (int i = 0; i < N; i++) {//输出每个学生的信息
        cout << arr[i].num << " " << arr[i].name << " " 
             << arr[i].score[0] << " " << arr[i].score[1] << " " << arr[i].score[2] << " " 
             << arr[i].sum << endl;
    }
    double total_sum = 0;//计算总平均分
    for (int i = 0; i < N; i++) {
        total_sum += arr[i].sum;
    }
    double average = total_sum / (N * 3.0); 
    cout << "总平均分=" << fixed << setprecision(6) << average << endl;
    int max_index = 0;//找出总分最高的学生
    int max_sum = arr[0].sum;
    for (int i = 1; i < N; i++) {
        if (arr[i].sum > max_sum) {
            max_sum = arr[i].sum;
            max_index = i;
        }
    }
    cout << arr[max_index].num << " " << arr[max_index].name << " " 
         << arr[max_index].score[0] << " " << arr[max_index].score[1] << " " << arr[max_index].score[2] << " " 
         << arr[max_index].sum << endl;// 输出总分最高的学生信息
    return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
struct Circle {//定义结构体
    int x;
    int y;
    int r;
};

int main() {
    Circle c;
    cin >> c.x >> c.y >> c.r;
    float area = 3.14f * c.r * c.r;//算面积
    cout << fixed << setprecision(2) << area << endl;
    return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
struct Student {
    char num[11];
    char name[11];
    int s1, s2, s3;
    int total;
};

int main() {
    int n;
    cin >> n;
    Student students[100];
    int max_total = -1;//初始化最高分
    for (int i = 0; i < n; i++) {//输入学生信息&计算总分
        cin >> students[i].num >> students[i].name 
            >> students[i].s1 >> students[i].s2 >> students[i].s3;
        students[i].total = students[i].s1 + students[i].s2 + students[i].s3;
        if (students[i].total > max_total) {
            max_total = students[i].total;
        }
    }
    for (int i = 0; i < n; i++) {//找最高分学生
        if (students[i].total == max_total) {
            cout << students[i].num << " " << students[i].name 
                 << " " << students[i].total << endl;
            break;
        }
    }
    return 0;
}
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
void print(double value) {
    if (fabs(value) < 1e-12) {
        cout << "0.0";
        return;
    }
    double rounded = round(value * 10) / 10;
    if (fabs(rounded - round(rounded)) < 1e-6) {
        cout << static_cast<int>(round(rounded)) << ".0";
    } else {
        cout << fixed << setprecision(1) << rounded;
    }
}
int main() {
    double x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    double x = x1 + x2;
    double y = y1 + y2;
    cout << "(";
    print(x);
    cout << ", ";
    print(y);
    cout << ")" << endl;
    return 0;
}
#include <iostream>
using namespace std;
int main() {
    int a1, b1, a2, b2;
    char c;
    cin >> a1 >> c >> b1 >> a2 >> c >> b2;
    long long comp = (long long)a1 * b2 - (long long)a2 * b1;
    cout << a1 << "/" << b1;
    if (comp > 0) cout << " > ";
    else if (comp < 0) cout << " < ";  
    else cout << " = ";
    cout << a2 << "/" << b2 << endl;
    return 0;
}
#include <iostream>
using namespace std;
int gcd(int a, int b) {// 求最大公约数
    return b == 0 ? a : gcd(b, a % b);
}
int main() {
    int a1, b1, a2, b2;
    char slash;
    cin >> a1 >> slash >> b1 >> a2 >> slash >> b2;
    int num = a1 * b2 + a2 * b1;  //算分子
    int den = b1 * b2;          //算分母
    int divers = gcd(num, den); // 约分求最大公约数
    num /= divers;
    den /= divers;
    
    if (den == 1) {//输出结果
        //分母为1
        cout << num << endl;
    } else {
        //其他
        cout << num << "/" << den << endl;
    }
    return 0;
}
#include <iostream>
using namespace std;
int gcd(int a, int b) {// 求最大公约数
    return b == 0 ? a : gcd(b, a % b);
}
int main() {
    int a1, b1, a2, b2;
    char slash;
    cin >> a1 >> slash >> b1 >> a2 >> slash >> b2;
    int num = a1 * b2 + a2 * b1;  //算分子
    int den = b1 * b2;          //算分母
    int divers = gcd(num, den); // 约分求最大公约数
    num /= divers;
    den /= divers;
    
    if (den == 1) {//输出结果
        //分母为1
        cout << num << endl;
    } else {
        //其他
        cout << num << "/" << den << endl;
    }
    return 0;
}
#include <iostream>
#include <vector>
using namespace std;

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}
void func(int &a1, int &b1, int a2, int b2) {
    int num = a1 * b2 + a2 * b1;
    int den = b1 * b2;
    int divers = gcd(num, den);
    a1 = num / divers;
    b1 = den / divers;
}

int main() {
    int N;
    cin >> N;
    int sum_a = 0, sum_b = 1;
    for (int i = 0; i < N; i++) {
        int a, b;
        char slash;
        cin >> a >> slash >> b;
        func(sum_a, sum_b, a, b);
    }
    sum_b *= N;
    int divers = gcd(sum_a, sum_b);
    sum_a /= divers;
    sum_b /= divers;
    if (sum_b == 1) {
        cout << sum_a << endl;
    } else {
        cout << sum_a << "/" << sum_b << endl;
    }
    return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main() {
    string colors[] = {"red", "blue", "yellow", "black"};
    int count = 1;//计数器
    //枚举
    for (int i = 0; i < 4; i++) {//第一个球
        for (int j = 0; j < 4; j++) {//第二个球
            for (int k = 0; k < 4; k++) {//第三个球
                //判断三个球颜色是否都不同
                if (i != j && i != k && j != k) {
                    cout << count << " " << colors[i] << " " << colors[j] << " " << colors[k] << endl;
                    count++;
                }
            }
        }
    }
    return 0;
}

 

posted @ 2025-11-02 23:23  bluepoet  阅读(5)  评论(0)    收藏  举报