
C++题解
C++相关内容
繁星伴晚安
热爱诗和代码
展开
-
【C++】判断数据是否全部满足条件
判断一个数组所有元素是否都是奇数,若全部满足返回true#include<iostream>using namespace std;int main() { int num[] = { 1,3,5,6,7,9 }; bool flag = true; for (int i = 0; i < 6; i++) { if (num[i] % 2 != 1) { flag = false; break; } } cout << flag; retu原创 2021-03-23 22:45:03 · 945 阅读 · 0 评论 -
csp
CCF-CSP认证备考要点原创 2021-03-21 22:29:17 · 186 阅读 · 0 评论 -
【C++】十进制和二进制相互转化
二进制转化为十进制1.从左向右:#include<iostream>using namespace std;#include<string>int binary_int(string str) { int sum = 0,t; for (int i = 0;i < str.size();i++) { t = str[i] - '0'; sum = 2*sum + t; } return sum;}in原创 2021-02-13 18:05:22 · 1270 阅读 · 0 评论 -
【c++】【递归】基础题
1.利用递归求数组的和#include <iostream>using namespace std;int rsum(int a[], int n) { if (n == 1) { return a[0]; } else { return a[n - 1] + rsum(a, n - 1); }}int main() { int a[] = { 1,2,3,4,5,6,7,8,9,10 }; cout << rsum(a, 10); return 0原创 2021-01-24 14:48:01 · 2344 阅读 · 0 评论 -
【基础题】【数组】前进、后退一步
1.输入10个元素,将数组每个元素位置整体前进一步、原先第一个元素放到最后一个位置输入格式:0 1 2 3 4 5 6 7 8 9输出格式:1 2 3 4 5 6 7 8 9 0代码 :#include<iostream>using namespace std;int main(){ int a[10],k; for(int i=0;i<10;i++) cin>>a[i]; k = a[0]; for(int i=0;i<10;i++){原创 2020-12-10 14:52:07 · 679 阅读 · 0 评论 -
【基础题】【指针】字符数组、字符串区别
比较大小 赋值 字符数组 strcmp(str1,str2) char temp[10]; 字符串 str1 > str2 string temp;temp = str1;str1 = str2;str2 = temp;原创 2020-12-06 07:59:42 · 215 阅读 · 0 评论 -
【基础题】【指针】整数交换
1.输入三个整数,按由小到大的顺序交换#include<iostream>using namespace std;void swap(int *p1,int *p2){ int p; p = *p1; *p1 = *p2; *p2 = p;}int main(){ int a,b,c; cin>>a>>b>>c; if(a > b) swap(&a,&b); if(a > c) swap(&原创 2020-12-06 07:56:40 · 252 阅读 · 0 评论 -
【基础题】【指针】Swap
1.将两个字符数组交换#include<iostream>#include<string.h>using namespace std;void swap(char *p1,char *p2){ char *temp = new char[10]; strcpy(temp,p1); strcpy(p1,p2); strcpy(p2,temp);}int main(){ char c1[10] = "Chian",c2[10] = "Korea"; cout<原创 2020-12-06 07:56:08 · 287 阅读 · 0 评论 -
【基础题】【指针】排序
形参数组,实参数组#include<iostream>using namespace std;void sort(string str[],int n){ string temp; for(int i=0;i<n;i++){ for(int j=i+1;j<n;j++){ if(str[i] > str[j]){ temp = str[i]; str[i] = str[j]; str[j] = temp; } } }}原创 2020-12-06 07:55:46 · 296 阅读 · 0 评论 -
【基础题】【指针】字符串复制
1.将字符串str1复制为字符串str2形参字符数组,实参字符数组#include<iostream>using namespace std;void copy_string(char from[],char to[]){ int i=0; while(from[i] != '\0'){ to[i] = from[i]; i++; } to[i] = '\0';}int main(){ char a[] = "I am a teacher."; char b[]原创 2020-12-06 07:55:25 · 991 阅读 · 0 评论 -
【基础题】【指针】二维数组遍历
一、形参是数组,实参是数组#include<iostream>using namespace std;void display(int a[][4],int n){ for(int i=0;i<n;i++){ for(int j=0;j<4;j++){ cout<<a[i][j]<<" "; } cout<<"\n"; }}int main(){ int a[3][4] = {1,3,5,7,9,11,13,15,原创 2020-12-06 07:55:07 · 299 阅读 · 0 评论 -
【基础题】【二维数组】地址和值
1.地址在二维数组中,二维数组名即第1行一维数组地址、与第1行一维数组的第一个元素的地址相等,但不等价,当a + i还是表示第i+1个一维数组名a + i 二维地址 #include<iostream>using namespace std;int main(){ int a[3][4] = {1,3,5,7,9,11,13,15,17,19,21,23}; cout<<"第1行首地址:"<<a<<"\n"; cout<<"第3行原创 2020-12-06 07:54:49 · 696 阅读 · 0 评论 -
【基础题】【指针】一维数组遍历
下面所有的指针方法都是三步骤:创建 -> 赋值 -> 使用还有所有的内容都是通过函数体现形参是数组,实参是数组形参是数组,实参是指针形参是指针,实参是数组形参是指针,实参是指针1.指针法一、形参是数组,实参是数组#include<iostream>using namespace std;void display(int a[],int n){ for(int i=0;i<n;i++){ cout<<a[i]<<" "; }原创 2020-12-06 07:54:27 · 819 阅读 · 0 评论 -
【基础题】【指针】交换两个数
1.输入两个数,对其进行交换#include<iostream>#include<string.h>using namespace std;void swap(int *p1,int *p2){ int t ; t = *p1; *p1 = *p2; *p2 = t;}int main(){ int a,b; cin>>a>>b; swap(a,b); cout<<a<<" "<<b; retu原创 2020-12-05 10:43:25 · 249 阅读 · 0 评论 -
【基础题】【字符数组】创建并初始化
1.创建并初始化#include<iostream>using namespace std;int main(){ char c[10] = "China"; cout<<c; return 0;}2.通常使用创建,通过赋值语句初始化一、#include<iostream>#include<string.h>using namespace std;int main(){ char c[10]; cin>>c; co原创 2020-12-05 10:32:24 · 350 阅读 · 0 评论 -
【基础题】【二维数组】创建并初始化
1.通常方法,通过先创建数组,再通过赋值语句初始化输入格式:1 2 3 45 6 7 89 10 11 12输出格式:1 2 3 45 6 7 89 10 11 12代码:#include<iostream>#include<string>using namespace std;int main(){ int a[3][4]; for(int i=0;i<3;i++){ for(int j=0;j<4;j++){ cin>原创 2020-12-05 10:24:26 · 942 阅读 · 0 评论 -
【基础题】【函数】【递归】求和,阶乘
1.求1+2+3+…+100的和#include<iostream>using namespace std;int sum(int n){ if(n == 1) return n; else return n + sum(n-1);}int main(){ int n; cin>>n; cout<<sum(n); return 0;}2. 求1 * 2 * 3 * 4* … * n#include<iostream>u原创 2020-12-05 10:02:06 · 367 阅读 · 0 评论 -
【基础题】【while循环】水仙花数
1.判断一个数是否为水仙花数,“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。#include<iostream>using namespace std;int main(){ int n,t,m=0,p; cin>>n; t = n; while(t > 0){ p = t % 10; m += p*p*p; t /= 10; } if(m == n){ cout<<n<<"是水仙花数."; } re原创 2020-12-05 09:40:43 · 3074 阅读 · 0 评论 -
【基础题】【分支】统计字母、数字个数
1.输入一行字符,分别统计其中英文字母,空格,数字和其他字符的个数。输入格式:My teacher's address is "#123 Beijing Road,Shanghai".输出格式:letters:38,space:6,digit:3,other:6代码:#include<iostream>using namespace std;int main(){ char c; int letters=0,space=0,digit=0,other=0; while(原创 2020-12-05 09:30:42 · 399 阅读 · 0 评论 -
【基础题】【函数】【递归】最大公约数
1.输入两个正整数m和n,求其最大公约数#include<iostream>using namespace std;int gcd(int n,int m){ int t; if(n < m){ t = n; n = m; m = t; } if(n % m == 0){ return m; } else return gcd(m,n%m);}int main(){ int n,m; cin>>n>>m; cout<原创 2020-12-05 09:20:05 · 226 阅读 · 0 评论 -
【基础题】【while循环】最大公约数
1.输入两个正整数m和n,求其最大公约数输入格式 :15 9输出格式 :3输入格式 :12 18输出格式 :6代码:#include<iostream>using namespace std;int main(){ int n,m,t; cin>>n>>m; if(n < m){ t = n; n = m; m = t; } t = m; while(t > 0){ if((n % t == 0) &原创 2020-12-05 09:14:21 · 3504 阅读 · 0 评论 -
【基础题】【for循环】倒序数
1.将一个整数倒序输出输入格式 :12345输出格式 :54321代码:#include<iostream>#include<math.h>using namespace std;int main(){ int t,m=0; cin>>t; while(t > 0){ m = 10 * m + t % 10; t /= 10; } cout<<m; return 0;}...原创 2020-12-05 09:02:57 · 1623 阅读 · 0 评论 -
【基础题】【分支】排顺序
1.输入四个数,要求按由小到大的顺序输出#include<iostream>#include<math.h>using namespace std;int main(){ int a,b,c,d,t; cin>>a>>b>>c>>d; if(a > b){ t = a; a = b; b = t; } if(a > c){ t = a; a = c; c = t; } if(a原创 2020-12-05 08:56:14 · 138 阅读 · 0 评论 -
【基础题】【数组】遍历
1.遍历数组,输出#include<iostream>using namespace std;int main(){ int a[10]; for(int i=0;i<10;i++){ a[i] = i; } for(int j=0;j<10;j++){ cout<<a[j]<<" "; } return 0;}2.倒序遍历数组,输出#include<iostream>#include<math.h>原创 2020-12-04 23:08:42 · 339 阅读 · 0 评论 -
【基础题】【while循环】从键盘一直录入数据
1、从键盘输入一个整数,当输入的整数不为1000时,可继续输入,并且将所有输入的数进行倒序输出。#include<iostream>using namespace std;int main(){ int a[1000],count=0; int t; cin>>t; while(t != 1000){ a[count] = t; count++; cin>>t; } for(int i=count-1;i>=0;i--){ cou原创 2020-12-03 22:53:58 · 1145 阅读 · 0 评论 -
【题解】【多态】Complex
题目描述定义一个复数类Complex,实现+、-、*、/、++(前置)、++(后置)的运算符重载输入格式输入共一行,四个浮点数,分别表示虚数q的实数部分和虚数部分和虚数w的实数部分和虚数部分输出格式输出共6行,分别输出q+w,q-w,q*w,q / w , q++ , ++w的值输入输出样例输入 #1 复制4 3 2 1输出 #1 复制6.00+4.00i2.00+2.00i5.00+10.00i2.20+0.40i4.00+3.00i3.00+1.00i#include原创 2020-12-03 20:46:05 · 503 阅读 · 0 评论 -
【题解】【多态】Shape
题目描述编写一个抽象类Shape,包含两个纯虚函数GetArea()计算面积,GetPerim()计算周长在此基础上派生Rectangle类和circle类,给出纯虚函数的具体实现。通过继承Rectangle类,创建一个派生类Square。在主函数里创建类对象进行测试。输入格式输入共一行,四个浮点数,分别表示长方形的长Lenth、宽Width,圆的半径Radius,正方形的边长x输出格式输出共六行,分别输出长方形,圆和正方形的面积和周长,输出保留5位小数,格式如下输入输出样例输入 #1 复制原创 2020-12-03 20:44:40 · 695 阅读 · 0 评论 -
【基础题】【数组】定义并初始化
1.定义一个长度为5数组,通过赋值语句初始化。#include <iostream>using namespace std;int main(){ int a[5]; for(int i=0;i<5;i++){ a[i] = i; } for(int i=0;i<5;i++){ cout<<a[i]<<" "; }}2.定义数组并初始化。#include <iostream>using namespace std原创 2020-12-03 12:46:45 · 2789 阅读 · 0 评论 -
【基础题】【顺序】大小写转化
1.输入一个小写字母,输出其大写格式#include <iostream>using namespace std;int main(){ char c; cin>>c; c = c - 'a' + 'A'; cout<<c; }3.输入一个大写字母,输出其小写格式#include <iostream>using namespace std;int main(){ char c; cin>>c; c = c - 'A'原创 2020-12-02 22:00:46 · 166 阅读 · 0 评论 -
【基础题】【分支】大小写转化
1.输入一个字母字符,如果是大写转化为小写输出,如果是小写,转化为大写输出。#include <iostream>#include <iomanip>#include<math.h>using namespace std;int main(){ char c; cin>>c; if(c >='a' && c <= 'z') c = c - 'a' + 'A'; else if(c >= 'A' &原创 2020-12-02 21:57:21 · 308 阅读 · 0 评论 -
【基础题】【顺序】两数交换
1.将a,b的值交换后输出#include <iostream>#include <iomanip>using namespace std;int main(){ int a,b,t; cin>>a>>b; cout<<"a = "<<a<<",b = "<<b<<"\n"; t = a; a = b; b = t; cout<<"a = "<<a<原创 2020-12-02 14:44:14 · 206 阅读 · 0 评论 -
【基础题】【for循环】二重循环
1.输出以下格式11 21 2 31 2 3 41 2 3 4 51 2 3 4 5 61 2 3 4 5 6 71 2 3 4 5 6 7 81 2 3 4 5 6 7 8 9代码:#include <iostream>#include <iomanip>using namespace std;int main(){ for(int i=1;i<10;i++){ for(int j=1;j<=i;j++){ cout<&l原创 2020-12-02 12:50:55 · 541 阅读 · 0 评论 -
【基础题】【for循环】一行输出十个数
1.输出1~100的偶数,要求每行十个数#include <iostream>#include <iomanip>using namespace std;int main(){ int count=0; for(int i=1;i<=100;i++){ if(i % 2 == 0){ cout<<i<<" "; count++; if(count % 10 == 0) cout<<"\n"; }原创 2020-12-02 12:38:39 · 7604 阅读 · 0 评论 -
【基础题】【字符串】从键盘录入一段字符穿串
1.使用getchar()#include<iostream>using namespace std;int main(){ char c; while((c = getchar()) != '?'){ cout<<c<<" "; } return 0;}2.使用gets(字符数组)#include<iostream>#include<string>using namespace std;int main(){ c原创 2020-11-30 23:03:36 · 466 阅读 · 0 评论 -
【基础题】【多态】多态的设计思路
#include<iostream>using namespace std; class A{public: void foo() { printf("1\n"); } virtual void fun() { printf("2\n"); }};class B : public A{public: void foo() { printf("3\n"); } void fun() { printf("4\n"); }};int mai原创 2020-11-29 12:36:30 · 304 阅读 · 1 评论 -
【基础题】【继承】继承的设计思路
#include<iostream>#include<iomanip>using namespace std;class Person{ private: string name; int age; char sex; string addr; string tel; public: Person(){} Person(string name,int age,char sex,string addr,string tel); void set原创 2020-11-28 20:39:56 · 227 阅读 · 0 评论 -
【基础题】【for循环】字符串、整型相互转换
1.请输入一个字符串,将其转换为整型输出#include<iostream>#include<iomanip>#include<string>using namespace std;int main(){ string str; cin>>str; int number=0,m; for(int i=0;i<str.length();i++){ m = str[i] - '0'; number = 10 * number + m原创 2020-11-28 16:31:50 · 396 阅读 · 0 评论 -
【基础题】【顺序】字符与数字,ASCII码
1.分别输入A, a, 0,请分别输出它们的ASCII码#include<iostream>#include<iomanip>using namespace std;int main(){ char c1,c2,c3; cin>>c1>>c2>>c3; int a,b,c; a = c1; b = c2; c = c3; cout<<a<<" "<<b<<" "<&l原创 2020-11-28 16:24:20 · 149 阅读 · 0 评论 -
【基础题】【顺序】个位、十位、百位、千位
1、输入一个四位数,分别输出它的个位,十位、百位、千位。#include<iostream>#include<iomanip>using namespace std;int main(){ int number,a,b,c,d; cin>>number; a = number % 10; b = number / 10 % 10; c = number / 100 % 10; d = number / 1000; cout<<"四位数:"原创 2020-11-28 16:12:48 · 1078 阅读 · 0 评论 -
【基础题】【for循环】分别输出A~Z, a ~ z。
1.输出A~Z#include<iostream>#include<iomanip>using namespace std;int main(){ for(char c = 'A';c <= 'Z';c++){ cout<<c<<" "; }return 0; } 2.输出a ~ z```cpp#include<iostream>#include<iomanip>using namespace s原创 2020-11-28 16:05:30 · 1424 阅读 · 0 评论