当前位置: 首页 > news >正文

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) {//输出结果//分母为1cout << 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) {//输出结果//分母为1cout << 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;
}

 

http://www.gsyq.cn/news/37943.html

相关文章:

  • 2025年数据分类分级工具选型指南:智能合规基座与八大产品全景评估
  • 国产化数据库迁移工具不会用?教你手搓一个万能数据迁移工具。
  • 思维的“幽灵显影”:神经科学捕捉意义重燃的独特签名
  • 搜索百科(6):Meilisearch — Rust 打造的轻量级搜索新锐
  • 软件工程--团队作业
  • C++练习1
  • 2025.11.2总结
  • 第二届数证杯初赛-计算机取证
  • claude_code_clone
  • cf2000左右dp
  • 题解:P7201 [COCI 2019/2020 #1] Džumbus
  • 题解:CF875C National Property
  • lecms在使用redis中设置他缓存时间
  • 博客一年纪
  • 题解:CF291E Tree-String Problem
  • 题解:AT_abc307_f [ABC307F] Virus 2
  • java操作sip
  • 思维的断章,觉知的永恒:一个基于“内观照叙事模型”的认知革命与跨学科范式重构
  • struct page
  • NFS 服务端/客户端配置
  • [Record] CSP-S 2025 邮寄
  • CH59x/CH58X蓝牙从机白名单使用
  • AT ABC285E Work or Rest 题解
  • 代码复杂度的代价远比你想象得大
  • CSP2025 - S 年度总结大会报告
  • minio 服务端加密方式
  • (补11月)代码大全阅读笔记2
  • VisualStudio 2022如何打开.slnx文件格式的解决方案
  • CSP2025 - S 游记
  • C语言字符串及其函数