【『ACM C++』 PTA 天梯赛练习集L1 | 018-020】教程文章相关的互联网学习教程文章

C++练习2(--与==的重载)【代码】

在复数类Complex内部实现运算符–和== #include<iostream> using namespace std; class Complex {int x,y; public:Complex(int x1=0,int y1=0){x=x1;y=y1;}Complex operator--(int)//后缀-- {x--;y--;return Complex(x+1,y+1); }Complex operator--()//前缀-- {--x;--y;return Complex(x,y);}bool operator==(Complex &z1){if(x==z1.x&&y==z1.y){return true;}else{return false;}}void print(){cout<<x;if(y>0){cout<<"+"<<y<<"i"...

<小甲鱼>C++实例练习27—构造副本构造器【代码】

前言: 参考:' rel='nofollow' target='_blank'>B站UP主鱼C_小甲鱼 代码调试平台:VS2017,调试成功。问题描述: 问题描述:构造副本构造器代码实现: #include <iostream> #include <string>using namespace std;class Myclass { public:Myclass(int *p);Myclass(const Myclass &rhs);~Myclass();Myclass &operator=(const Myclass &rhs); //对赋值运算符进行重载函数声明void print(); private:int *ptr; }; Myclass::Myclass(i...

【_Maiko_的C++练习】洛谷 P1046 [NOIP2005 普及组] 陶陶摘苹果【代码】

【_Maiko_的C++练习】洛谷 P1046 [NOIP2005 普及组] 陶陶摘苹果洛谷本题的原网站 P1046 [NOIP2005 普及组] 陶陶摘苹果 主要就是在循环之中判定、并且通过再循环外定义的计数器累加符合题设的次数,属于遍历各个题给情况。 #include<stdio.h>int apple_h[10]={0};//记录10个苹果的高度 int main() {int taotao=0;//记录淘淘的身高 int amount=0;//记录苹果数量 for(int i=0;i<=9;i++){scanf("%d",&apple_h[i]);//输入苹果高度 }scan...

2021-03-23 C++基础练习——函数专场【代码】【图】

文章目录 1.有参有反函数2.有参无返3.无参有返4.无参无返1.有参有反函数 编写两个函数,函数功能分别是:求两个整数的最大公约数和最小公倍数 【示例代码】 #include<iostream> using namespace std;//最大公约数 int GCD(int a, int b) {int gcd;int max = a;if (a > b)max = b;for (int i = 1; i <= max; i++){if ((a%i == 0) && (b%i == 0))gcd = i;}return gcd;}//最小公倍数 int LCM(int a, int b) {int lcm;int min = a;if (...

[c/c++] 算法笔记 codeup 问题 E: A+B 输入输出练习V【代码】

问题 E: A+B 输入输出练习V 题目描述你的任务是计算若干整数的和。输入输入的第一行是一个正数N,表示后面有N行。每一行的第一个数是M,表示本行后面还有M个数。输出对于每一行数据需要在相应的行输出和。样例输入 Copy2 4 1 2 3 4 5 1 2 3 4 5样例输出 Copy10 15#include<stdio.h>int main(){int n, m, sum = 0, a;scanf("%d", &n); while(n--){sum = 0;scanf("%d", &m);while(m--){scanf("%d", &a);sum += a;}printf("%d\n", sum...

2021/2/25CCF练习201509(c++)【代码】

201509-1 #include<iostream> using namespace std; #define MAX 1000 int a[MAX]; int main() {int n;cin>>n;int count=1;for(int i=0;i<n;i++)cin>>a[i];for(int i=1;i<n;i++)if(a[i]!=a[i-1])count++;cout<<count<<endl;return 0; }201509-2 80分代码 #include<iostream> using namespace std; #define MAX 1000 int a[12]={31,28,31,30,31,30,31,31,30,31,30,31}; int b[12]={31,29,31,30,31,30,31,31,30,31,30,31}; int month...

C++练习(三)【代码】

第四章 复合类型 1,编写一个C++程序,输出如下: What is your first name? Betty Sue What is your last name? Yewe What letter grade do you deserve? B What is your age? 22 Name: Yewe, Betty Sue Grade: C Age: 22 程序将向下调整程序 #include<iostream> uisng namespace std; int main() {char first_name[20],last_name[20];char grade;int age;cout<<"What is your first name?";cin>>getline(first_name,20);cout<<"W...

C++Primer(第五版 )第二章 变量和基本类型 章节编程练习答案【图】

2.8 请利用转义序列编写一段程序,要求先输出 2M,然后转到新一行。修改程序使其先输出 2,然后输出制表符,再输出 M,最后转到新一行。 完成如下:2.17 执行下面的代码段将输出什么结果?2.18 编写代码分别改变指针的值以及指针所指对象的值。 完成如下:

c++练习10——两个栈实现一个队列【代码】

typedef struct Node {int data;Node *next; }Node, *LinkStack;//创建空栈 LinkStack CreateNULLStack(LinkStack &S) {S = (LinkStack)malloc(sizeof(Node));if (nullptr == S){cout << "Failed to malloc a new code" << endl;return nullptr;}S->data = 0;S->next = nullptr;return S; }//栈插入函数 LinkStack Push(LinkStack &S, int data) {if (nullptr == S){cout << "There no node in stack" << endl;return nullptr;}Lin...

C++ primer Plus(第六版)第六章 分支语句和逻辑运算符 章节编程练习答案【图】

1.编写一个程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外), 同时将大写字符转换为小写,将小写字符转换为大写(别忘了cctype函数系列) 完成如下: 用if - else if或用if+continue2.编写一个程序,最多将10个donation值读入到一个double数组中(如果您愿意,也可以使用模板类array)。 程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。 完成如下:3.编写一个...

c++练习08:typedef和#define的区别

用法不同 1)typedef用来定义一种数据类型的别名,增强程序的可读性; 2)define主要用来定义变量,以及书写复杂性使用频繁的宏; 执行时间不同 1)typedef是编译过程的一部分,有类型检查的功能; 2)define是宏定义,是预编译的部分,其发生在编译之前,只是简单的进行字符串的替换,不进行类型的检查; 作用域不同 1)typedef有作用域限定; 2)define不受作用域约束,只要是define声明后的引用都是正确的; 对指针的操作不同

C++飞机大战新手练习【代码】

#include #include<graphics.h> #define X 700 #define Y 650 #define ZX 5 #define ZY 30 #define FX 60 #define FY 60 #define ZHIDAN 30 #define DIREN 30 DWORD t1, t2, w1, w2; bool GameOver = true; struct image{ IMAGE data; IMAGE user; IMAGE zidan; IMAGE master; }image; struct zuobiao { float x; float y; int hp; bool boolz; }user,master[DIREN],zidan[ZHIDAN]; void 加载图片(){ //图片途径可以修改 这是安装...

贪吃蛇程序 C++练习【代码】

#include <iostream> #include <Windows.h> #include <conio.h> #include <deque> #include <ctime> #pragma warning(disable:4996) using namespace std; HANDLE hOut; COORD pos;//1.实现gotoxy函数 void gotoxy(short x, short y) {hOut = GetStdHandle(STD_OUTPUT_HANDLE); //获取句柄pos = { x, y };SetConsoleCursorPosition(hOut, pos); //移动光标到指定位置 } void HideCursor() //隐藏光标 {HANDLE handle = GetStdHand...

c++primer练习4.6-4.12【代码】

4.20 vector<string>::iterator iter; *iter++; (*iter)++; *iter.empty(); iter->empty() ++*iter; iter++->empty(); #include <vector> #include <iostream> using namespace std;int main() {vector<string> i = {"0","1","2","3"};vector<string>::iterator iter;for(iter = i.begin(); iter != i.end(); ){cout << *iter++ <<endl;}} 表达式合理,输出0 1 2 3,表达式的含义是取迭代器指向的参数的值,迭代器向后移一位. (a)...

c++ primer plus第六版第六章编程练习【代码】【图】

编写程序读取键盘输入,回显除数字外字符,同时大写转小写,小写转大写,遇’@就停止。 //练习6.1 读取键盘输入,回显输出(除数字),另外大写字母和小写字母互转,遇“@”则退出程序。 #include <iostream> #include <cctype> using namespace std; int main() {char ch;while ((ch = cin.get()) != '@') //遇'@'则停{if (isdigit(ch)) //判断是否为数字,是则跳过{ continue;}else if (islower(ch)) ...