CSP-J/S:CCF非专业级软件能力认证(Certified Software Professional Junior/Senior,简称CSP-J/S)创办于2019年,是由CCF统一组织的评价计算机非专业人士算法和编程能力的活动。全国统一大纲、统一认证题目,任何人均可报名参加。CSP-J/S分两个级别进行,分别为CSP-J(入门级,Junior)和CSP-S(提高级,Senior),两个级别难度不同,均涉及算法和编程。CSP-J/S分第一轮和第二轮两个阶段。第一轮考察通用和实用的计算机科学知识,以笔试为主,部分省市以机试方式认证。第二轮为程序设计,须在计算机上调试完成。第一轮认证成绩优异者进入第二轮认证,第二轮认证结束后,CCF将根据CSP-J/S各组的认证成绩和给定的分数线,颁发认证证书。CSP-J/S成绩优异者,可参加NOI省级选拔,省级选拔成绩优异者可参加NOI。
NOIP:全国青少年信息学奥林匹克联赛(National Olympiad in Informatics in Provinces简称NOIP)自1995年至今。每年由中国计算机学会统一组织。 NOIP在 同一时间、不同地点以各省市为单位由特派员组织。全国统一大纲、统一试卷。初、高中或其他中等专业学校的学生可报名参加联赛。联赛分初赛和复赛 两个阶段。初赛考察通用和实用的计算机科学知识,以笔试为主。复赛为程序设计,须在计算机上调试完成。参加初赛者须达到一定分数线后才有资格参加复赛。联赛分普及组和提高组两个组别,难度不同,分别面向初中和高中阶段的学生。
#include<iostream>
using namespace std;
int main()
{
int n = 5;
cout<<n<<endl;
n = n+2;
cout<<n<<endl;
n += 1;
cout<<n<<endl;
n -= 3;
cout<<n<<endl;
n = n-2+1;
cout<<n<<endl;
n = 0;
cout<<n<<endl;
return 0;
}
#include<iostream>usingnamespace std;int main(){int n =5;
cout<<n<<endl;
n = n+2;
cout<<n<<endl;
n +=1;
cout<<n<<endl;
n -=3;
cout<<n<<endl;
n = n-2+1;
cout<<n<<endl;
n =0;
cout<<n<<endl;return0;}
程序第5行 int n = 5; 定义了一个int类型的变量n,同时给变量n赋初值5。int是一种最基本的整数数据类型(后文会介绍),可以存储一个整数。这一句相当于给了一个专门存放整数的盒子,这个盒子的名称是n,最开始往里面存放了5这个整数。
第11行 n += 1; 也是赋值语句,确切地说是“自赋值”。这一句的效果同 n = n+1; 一致,这里可以简单认为 n += 1; 就是 n = n+1; 的简写形式。作用是将n的值取出来,然后加上1,最后将结果又赋值给变量n。除了+=,还能使用 -= 、*=、/=、%=来进行自赋值。读者可以尝试修改第8行和11行原来的赋值语句为自赋值语句。
#include<iostream>
using namespace std;
int main()
{
int n = 2147483647;
cout<<n<<endl; //正常输出2147483647
n++; //n自加1,超出int存储范围,出现数据溢出(上溢)
cout<<n<<endl; //输出-2147483648(不可靠)
int m = -2147483648;
cout<<m<<endl; //正常输出-2147483648
m--; //n自减1,超出int存储范围,出现数据溢出(下溢)
cout<<m<<endl; //输出2147483647(不可靠)
return 0;
}
#include<iostream>usingnamespace std;int main(){int n =2147483647;
cout<<n<<endl;//正常输出2147483647
n++;//n自加1,超出int存储范围,出现数据溢出(上溢)
cout<<n<<endl;//输出-2147483648(不可靠) int m =-2147483648;
cout<<m<<endl;//正常输出-2147483648
m--;//n自减1,超出int存储范围,出现数据溢出(下溢)
cout<<m<<endl;//输出2147483647(不可靠)return0;}
#include<iostream>
using namespace std;
int main()
{
const double PI = 3.14;
double r,C,S;
r = 12.56;
C = 2 * PI * r;
S = PI * r * r;
cout<<C<<endl<<S<<endl;
return 0;
}
#include<iostream>usingnamespace std;int main(){constdouble PI =3.14;double r,C,S;
r =12.56;
C =2* PI * r;
S = PI * r * r;
cout<<C<<endl<<S<<endl;return0;}
第5行 const double PI = 3.14; ,是在普通的定义double变量PI并赋初值的语句 double PI = 3.14; 前面额外添加了一个修饰词 const。这样这个PI就成为了一个符号常量。这里的符号常量与变量用法一致,只是不能再修改它的值。一般约定符号常量名全部大写。
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include<iostream>
usingnamespace std;
intmain()
{
constdouble PI = 3.14;
PI = 3.14159;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
const double PI = 3.14;
PI = 3.14159;
return 0;
}
#include<iostream>usingnamespace std;int main(){constdouble PI =3.14;
PI =3.14159;return0;}
第6行使用赋值语句尝试修改符号常量PI的值,会出现编译错误。不能修改符号常量的值。
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include<iostream>
usingnamespace std;
intmain()
{
constdouble PI;
PI = 3.14;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
const double PI;
PI = 3.14;
return 0;
}
#include<iostream>usingnamespace std;int main(){constdouble PI;
PI =3.14;return0;}
#include<iostream>
using namespace std;
int main()
{
const double PI = 3.14;
double r,C,S;
r = 12.56;
C = 2 * PI * r;
S = PI * r * r;
cout<<C<<endl<<S<<endl;
return 0;
}
#include<iostream>usingnamespace std;int main(){constdouble PI =3.14;double r,C,S;
r =12.56;
C =2* PI * r;
S = PI * r * r;
cout<<C<<endl<<S<<endl;return0;}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include<iostream>
usingnamespace std;
intmain()
{
constdouble PI = 3.14159;
double r,C,S;
r = 12.56;
C = 2 * PI * r;
S = PI * r * r;
cout<<C<<endl<<S<<endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
const double PI = 3.14159;
double r,C,S;
r = 12.56;
C = 2 * PI * r;
S = PI * r * r;
cout<<C<<endl<<S<<endl;
return 0;
}
#include<iostream>usingnamespace std;int main(){constdouble PI =3.14159;double r,C,S;
r =12.56;
C =2* PI * r;
S = PI * r * r;
cout<<C<<endl<<S<<endl;return0;}
此外,还可以通过C风格的#define语句来定义符号常量,注意语法与const的不同:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include<iostream>
usingnamespace std;
intmain()
{
#define PI 3.14
double r,C,S;
r = 12.56;
C = 2 * PI * r;
S = PI * r * r;
cout<<C<<endl<<S<<endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
#define PI 3.14
double r,C,S;
r = 12.56;
C = 2 * PI * r;
S = PI * r * r;
cout<<C<<endl<<S<<endl;
return 0;
}
#include<iostream>usingnamespace std;int main(){#define PI 3.14double r,C,S;
r =12.56;
C =2* PI * r;
S = PI * r * r;
cout<<C<<endl<<S<<endl;return0;}
#include<iostream>
#define PI 3.14
using namespace std;
int main()
{
double r,C,S;
r = 12.56;
C = 2 * PI * r;
S = PI * r * r;
cout<<C<<endl<<S<<endl;
return 0;
}
#include<iostream>#define PI 3.14usingnamespace std;int main(){double r,C,S;
r =12.56;
C =2* PI * r;
S = PI * r * r;
cout<<C<<endl<<S<<endl;return0;}
习惯性地将#define语句写到程序的头部
#define其实是宏定义,编译时宏定义之后的代码中所有的 PI 会被简单粗暴地全部替换成3.14,所以这里不需要指定数据类型。
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b; //使用cin语句连续输入2个整数存储到变量a、b中
c = a+b; //计算a+b的值,保存到变量c中
cout<<c<<endl;
cout<<a<<"+"<<b<<"="<<c;
return 0;
}
#include<iostream>usingnamespace std;int main(){int a,b,c;
cin>>a>>b;//使用cin语句连续输入2个整数存储到变量a、b中
c = a+b;//计算a+b的值,保存到变量c中
cout<<c<<endl;
cout<<a<<"+"<<b<<"="<<c;return0;}
再来看一个经典问题:输入两个整数a和b,计算并输出 a ÷ b 的结果:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include<iostream>
usingnamespace std;
intmain()
{
int a,b;
cin>>a>>b;
cout<<a/b<<endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
cout<<a/b<<endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
const double PI = 3.14159;
double r; //为了增强程序的实用性,应该使用double类型来存储输入的半径
cin>>r;
cout<<2 * PI * r<<endl;
cout<<PI * r * r<<endl;
return 0;
}
#include<iostream>usingnamespace std;int main(){constdouble PI =3.14159;double r;//为了增强程序的实用性,应该使用double类型来存储输入的半径
cin>>r;
cout<<2* PI * r<<endl;
cout<<PI * r * r<<endl;return0;}
最后试一试下面的程序,并和前一段程序比较:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include<iostream>
usingnamespace std;
intmain()
{
constdouble PI = 3.14159;
double r,C,S; //声明三个double类型(小数/浮点数)的变量r、C、S
cin>>r;
C = 2 * PI * r; //计算圆的周长并存储到变量C中
S = PI * r * r; //计算圆的面积并存储到变量S中
cout<<C<<endl<<S<<endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
const double PI = 3.14159;
double r,C,S; //声明三个double类型(小数/浮点数)的变量r、C、S
cin>>r;
C = 2 * PI * r; //计算圆的周长并存储到变量C中
S = PI * r * r; //计算圆的面积并存储到变量S中
cout<<C<<endl<<S<<endl;
return 0;
}
#include<iostream>usingnamespace std;int main(){constdouble PI =3.14159;double r,C,S;//声明三个double类型(小数/浮点数)的变量r、C、S
cin>>r;
C =2* PI * r;//计算圆的周长并存储到变量C中
S = PI * r * r;//计算圆的面积并存储到变量S中
cout<<C<<endl<<S<<endl;return0;}
#include<iostream>
using namespace std;
int main()
{
long long m = 123;
//long long类型赋值给int类型
//因为long long类型变量的值仍在int存储范围内,不会出现数据溢出
cout<<(int)m<<endl;
int n = m;
cout<<n<<endl;
return 0;
}
#include<iostream>usingnamespace std;int main(){longlong m =123;//long long类型赋值给int类型//因为long long类型变量的值仍在int存储范围内,不会出现数据溢出
cout<<(int)m<<endl;int n = m;
cout<<n<<endl;return0;}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include<iostream>
usingnamespace std;
intmain()
{
longlong m = 2147483648;
//long long类型赋值给int类型
//因为long long类型变量的值超出int存储范围内,会出现数据溢出
cout<<(int)m<<endl;
int n = m;
cout<<n<<endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
long long m = 2147483648;
//long long类型赋值给int类型
//因为long long类型变量的值超出int存储范围内,会出现数据溢出
cout<<(int)m<<endl;
int n = m;
cout<<n<<endl;
return 0;
}
#include<iostream>usingnamespace std;int main(){longlong m =2147483648;//long long类型赋值给int类型//因为long long类型变量的值超出int存储范围内,会出现数据溢出
cout<<(int)m<<endl;int n = m;
cout<<n<<endl;return0;}
这个表展示了字符与十进制数一一对应的关系,称之为ASCII表(American Standard Code for Information Interchange,美国信息交换标准代码)。第0个到第31个字符(也是十进制数0~31对应的字符)是控制字符(不可见字符),从第32个字符开始,每个整数对应一个打印字符(英文标点符号、数字、大小写字母等)。
#include<cstdio>
int main()
{
const double PI = 3.14159;
double r,C,S;
scanf("%lf",&r);
C = 2 * PI * r;
S = PI * r * r;
printf("R=%f\nC=%.5f\nS=%.5f",r,C,S);
return 0;
}
#include<cstdio>int main(){constdouble PI =3.14159;double r,C,S;
scanf("%lf",&r);
C =2* PI * r;
S = PI * r * r;
printf("R=%f\nC=%.5f\nS=%.5f",r,C,S);return0;}
#include<iostream>
using namespace std;
int main()
{
const double PI = 3.14159;
double r,C,S;
cin>>r;
C = 2 * PI * r;
S = PI * r * r;
cout<<C<<endl<<S<<endl;
return 0;
}
#include<iostream>usingnamespace std;int main(){constdouble PI =3.14159;double r,C,S;
cin>>r;
C =2* PI * r;
S = PI * r * r;
cout<<C<<endl<<S<<endl;return0;}
int n = 4,m = 6;
n = n + m;
n += m; //自赋值(与 n = n + m; 等效,可以认为是简写)
n = n + 1;
n += 1; //自赋值(与 n = n + 1; 等效,可以认为是简写)
n++; //自赋值( n += 1;的简写)
++n; //自赋值( n += 1;的简写)
n = n - 1;
n -= 1;
n--; //自赋值( n -= 1;的简写)
--n; //自赋值( n -= 1;的简写)
int n =4,m =6;
n = n + m;
n += m;//自赋值(与 n = n + m; 等效,可以认为是简写)
n = n +1;
n +=1;//自赋值(与 n = n + 1; 等效,可以认为是简写)
n++;//自赋值( n += 1;的简写)++n;//自赋值( n += 1;的简写)
n = n -1;
n -=1;
n--;//自赋值( n -= 1;的简写)--n;//自赋值( n -= 1;的简写)
#include<iostream>
using namespace std;
int main()
{
int a,b,c,d;
cin>>a>>b;
cout<<a<<" "<<b<<endl;
c = a;
d = b;
a = d;
b = c;
cout<<a<<" "<<b<<endl;
return 0;
}
#include<iostream>usingnamespace std;int main(){int a,b,c,d;
cin>>a>>b;
cout<<a<<" "<<b<<endl;
c = a;
d = b;
a = d;
b = c;
cout<<a<<" "<<b<<endl;return0;}
#include<iostream>
using namespace std;
int main()
{
int a,b,t;
cin>>a>>b;
cout<<a<<" "<<b<<endl;
t = a;
a = b;
b = t;
cout<<a<<" "<<b<<endl;
return 0;
}
#include<iostream>usingnamespace std;int main(){int a,b,t;
cin>>a>>b;
cout<<a<<" "<<b<<endl;
t = a;
a = b;
b = t;
cout<<a<<" "<<b<<endl;return0;}
再来试试下面的代码,不借助额外的变量,靠简单的加减运算也能实现交换:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include<iostream>
usingnamespace std;
intmain()
{
int a,b;
cin>>a>>b;
cout<<a<<" "<<b<<endl;
a = a+b;
b = a-b;
a = a-b;
cout<<a<<" "<<b<<endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
cout<<a<<" "<<b<<endl;
a = a+b;
b = a-b;
a = a-b;
cout<<a<<" "<<b<<endl;
return 0;
}
#include<iostream>usingnamespace std;int main(){int a,b;
cin>>a>>b;
cout<<a<<" "<<b<<endl;
a = a+b;
b = a-b;
a = a-b;
cout<<a<<" "<<b<<endl;return0;}
#include<iostream>
using namespace std;
int main()
{
int s = 0; //初值0
//第1位同学捐献爱心图书5本
s += 5;
//第2位同学捐献爱心图书2本
s += 2;
//第3位同学捐献爱心图书1本
s += 1;
//...
//对于每位同学,s累加上捐献图书本数
cout<<s<<endl;
return 0;
}
#include<iostream>usingnamespace std;int main(){int s =0;//初值0 //第1位同学捐献爱心图书5本
s +=5;//第2位同学捐献爱心图书2本
s +=2;//第3位同学捐献爱心图书1本
s +=1;//...//对于每位同学,s累加上捐献图书本数
cout<<s<<endl;return0;}
令 p = (a+b+c)/2,则三角形面积 S = \sqrt{p(p-a)(p-b)(p-c)}
问题描述:计算给定三边长的三角形的面积
输入格式:三角形的三边长,用空格隔开(数据保证能组成三角形)
输出格式:三角形的面积,保留2位小数
问题分析:输入三个浮点数存储到double变量中,利用问题背景提供的公式来计算
算法设计:
Input \ a,b,c
p ← (a+b+c)/2
s ← sqrt(p*(p-a)*(p-b)*(p-c))
Output \ s
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include<cstdio>
#include<cmath>
intmain()
{
double a,b,c,p,s;
scanf("%lf%lf%lf",&a,&b,&c);
p = (a+b+c)/2;
//表达式中*不能省略
s = sqrt(p*(p-a)*(p-b)*(p-c));
printf("%.2lf",s);
return 0;
}
#include<cstdio>
#include<cmath>
int main()
{
double a,b,c,p,s;
scanf("%lf%lf%lf",&a,&b,&c);
p = (a+b+c)/2;
//表达式中*不能省略
s = sqrt(p*(p-a)*(p-b)*(p-c));
printf("%.2lf",s);
return 0;
}
#include<cstdio>#include<cmath>int main(){double a,b,c,p,s;
scanf("%lf%lf%lf",&a,&b,&c);
p =(a+b+c)/2;//表达式中*不能省略
s = sqrt(p*(p-a)*(p-b)*(p-c));
printf("%.2lf",s);return0;}
#include<iostream>
using namespace std;
int main()
{
int n,g,s,b;
cin>>n;
g = n%10;
s = n/10%10;
b = n/100%10;
cout<<g<<s<<b;
return 0;
}
#include<iostream>usingnamespace std;int main(){int n,g,s,b;
cin>>n;
g = n%10;
s = n/10%10;
b = n/100%10;
cout<<g<<s<<b;return0;}
程序运行测试情况如下:
输入123,输出:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
321
321
321
输入120,输出:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
021
021
021
输入100,输出:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
001
001
001
分析测试结果,可知右边两种情况(个位是0或者除百位外全是0),直接输出拆分出来的个、十、百位数的数字,不能满足题目“逆序数高位不能为0”的要求。那么我们设计的算法还不能全面地解决问题,需要进行调整。其实拆出来g、s、b后,可以再重新组合成新整数:g*100+s*10+b,这也是 n 的逆序数,并且这样做可以自动去掉高位多余的0。我们可以自行修改代码,并再次进行全面的测试。
注意错误提示不会提示大片区域错误,只会提示在某行某列处第一次匹配不上(内容不同),例如程序运行结果是123,但是标准答案是136,那么只会提醒第一处不匹配的内容,也就是 line 1 column 2,此时错误信息可能是:Wrong Answer.wrong answer On line 1 column 2.read 2,except 3.
在倒推的过程中,可以用变量 n 一直来记录这一天猴子吃桃子前的桃子数量,第四天就是输入的 n 值,那么倒推第三天的桃子数应该是 (n+1)*2 ,直接将这个值又赋值给 n(n = (n+1)*2;),这样的话第三天的桃子数量仍然记录在变量 n 中。接着倒推第二天、第一天,仍然用这样的方法。在倒推的过程中,通过连续的赋值语句不断修改变量 n 的值,让 n 一直记录的是倒推到的天数的桃子数量,也就是用 n 来持续“追踪”倒推过程中每天的桃子数量。
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include<iostream>
usingnamespace std;
intmain()
{
int n;
cin>>n; //第4天桃子数
n = (n+1)*2; //第3天桃子数,仍然保存在n中
n = (n+1)*2; //第2天桃子数,仍然保存在n中
n = (n+1)*2; //第1天桃子数,仍然保存在n中
cout<<n<<endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n; //第4天桃子数
n = (n+1)*2; //第3天桃子数,仍然保存在n中
n = (n+1)*2; //第2天桃子数,仍然保存在n中
n = (n+1)*2; //第1天桃子数,仍然保存在n中
cout<<n<<endl;
return 0;
}
#include<iostream>usingnamespace std;int main(){int n;
cin>>n;//第4天桃子数
n =(n+1)*2;//第3天桃子数,仍然保存在n中
n =(n+1)*2;//第2天桃子数,仍然保存在n中
n =(n+1)*2;//第1天桃子数,仍然保存在n中
cout<<n<<endl;return0;}
#include<iostream>
using namespace std;
int main()
{
int a,b,c,n;
cin>>n;
a = b = c = n/3; //结束后三人钱数都是n/3
//逆推丙分钱前三人钱数
//对于甲来说,他从丙那里拿到了和他已有钱相等的钱数,两份相加是a
//那么在丙分钱之前,甲的钱数应该是a/2。乙的情况类似。
a /= 2; //重新赋值a,赋值后a就是丙分钱前甲的钱数
b /= 2;
c = n-a-b; //丙分钱前丙的钱数是n-a-b.也可以用 c+=a+b;
//逆推乙分钱前三人钱数
a /= 2;
c /= 2;
b = n-a-c; //也可以用 b+=a+c;
//逆推甲分钱前三人钱数
b /= 2;
c /= 2;
a = n-b-c; //也可以用 a+=b+c;
cout<<a<<" "<<b<<" "<<c<<endl;
return 0;
}
#include<iostream>usingnamespace std;int main(){int a,b,c,n;
cin>>n;
a = b = c = n/3;//结束后三人钱数都是n/3//逆推丙分钱前三人钱数//对于甲来说,他从丙那里拿到了和他已有钱相等的钱数,两份相加是a//那么在丙分钱之前,甲的钱数应该是a/2。乙的情况类似。
a /=2;//重新赋值a,赋值后a就是丙分钱前甲的钱数
b /=2;
c = n-a-b;//丙分钱前丙的钱数是n-a-b.也可以用 c+=a+b; //逆推乙分钱前三人钱数
a /=2;
c /=2;
b = n-a-c;//也可以用 b+=a+c; //逆推甲分钱前三人钱数
b /=2;
c /=2;
a = n-b-c;//也可以用 a+=b+c;
cout<<a<<" "<<b<<" "<<c<<endl;return0;}
#include<iostream>
using namespace std;
int main()
{
int a,b,max;
cin>>a>>b;
//if语句{}中如果只有一条语句或者是一个整体
//可以不写{},甚至写到一行中
if(a>b) max = a;
else max = b;
cout<<max<<endl;
return 0;
}
#include<iostream>usingnamespace std;int main(){int a,b,max;
cin>>a>>b;//if语句{}中如果只有一条语句或者是一个整体//可以不写{},甚至写到一行中if(a>b) max = a;else max = b;
cout<<max<<endl;return0;}
y=\left\{ \begin{aligned} x^3+2x^2+1\ (x \ge 10) \\ x+1\ (5 \le x <10) \\ 0\ (-5 < x <5)\\ -x-1\ (-10 < x \le -5)\\-x^3-2x^2-1\ (x \le -10)\end{aligned} \right.
分段函数一共5段,每次分出一种情况(一段)来处理:
如果x>=10,那么...
否则如果x>=5,那么...
否则如果x>-5,那么...
否则如果x>-10,那么...
否则,...
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include<cstdio>
intmain()
{
double x,y;
scanf("%lf",&x);
if(x>=10) y = x*x*x+2*x*x+1;
elseif(x>=5) y = x+1;
elseif(x>-5) y = 0;
elseif(x>-10) y = -x-1;
else y = -x*x*x-2*x*x-1;
printf("%.5lf",y);
return 0;
}
#include<cstdio>
int main()
{
double x,y;
scanf("%lf",&x);
if(x>=10) y = x*x*x+2*x*x+1;
else if(x>=5) y = x+1;
else if(x>-5) y = 0;
else if(x>-10) y = -x-1;
else y = -x*x*x-2*x*x-1;
printf("%.5lf",y);
return 0;
}
#include<cstdio>int main(){double x,y;
scanf("%lf",&x);if(x>=10) y = x*x*x+2*x*x+1;elseif(x>=5) y = x+1;elseif(x>-5) y =0;elseif(x>-10) y =-x-1;else y =-x*x*x-2*x*x-1;
printf("%.5lf",y);return0;}
#include<iostream>
using namespace std;
int main()
{
int a,b,c,max;
cin>>a>>b>>c;
if(a>b){ //a>b,那么此时只需要考虑a、c
if(a>c) max = a;
else max = c;
}else{ //a<=b(else),那么此时只需要考虑b、c
if(b>c) max = b;
else max = c;
}
cout<<max<<endl;
return 0;
}
#include<iostream>usingnamespace std;int main(){int a,b,c,max;
cin>>a>>b>>c;if(a>b){//a>b,那么此时只需要考虑a、c if(a>c) max = a;else max = c;}else{//a<=b(else),那么此时只需要考虑b、cif(b>c) max = b;else max = c;}
cout<<max<<endl;return0;}
这里强调代码缩进对于确保程序可读性的重要性,我们对比下面两段代码就能体会到:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include<iostream>
usingnamespace std;
intmain()
{
int a,b,c,max;
cin>>a>>b>>c;
if(a>b){
if(a>c)
max = a;
else
max = c;
}else{
if(b>c)
max = b;
else
max = c;
}
cout<<max<<endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int a,b,c,max;
cin>>a>>b>>c;
if(a>b){
if(a>c)
max = a;
else
max = c;
}else{
if(b>c)
max = b;
else
max = c;
}
cout<<max<<endl;
return 0;
}
#include<iostream>usingnamespace std;int main(){int a,b,c,max;
cin>>a>>b>>c;if(a>b){if(a>c)
max = a;else
max = c;}else{if(b>c)
max = b;else
max = c;}
cout<<max<<endl;return0;}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include<iostream>
usingnamespace std;
intmain()
{
int a,b,c,max;
cin>>a>>b>>c;
if(a>b){
if(a>c)
max = a;
else
max = c;
}else{
if(b>c)
max = b;
else
max = c;
}
cout<<max<<endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int a,b,c,max;
cin>>a>>b>>c;
if(a>b){
if(a>c)
max = a;
else
max = c;
}else{
if(b>c)
max = b;
else
max = c;
}
cout<<max<<endl;
return 0;
}
#include<iostream>usingnamespace std;int main(){int a,b,c,max;
cin>>a>>b>>c;if(a>b){if(a>c)
max = a;else
max = c;}else{if(b>c)
max = b;else
max = c;}
cout<<max<<endl;return0;}
#include<iostream>
using namespace std;
int main()
{
int a,b,c,max;
cin>>a>>b>>c;
max = a;
if(b>max) max = b;
if(c>max) max = c;
cout<<max<<endl;
return 0;
}
#include<iostream>usingnamespace std;int main(){int a,b,c,max;
cin>>a>>b>>c;
max = a;if(b>max) max = b;if(c>max) max = c;
cout<<max<<endl;return0;}
在上面程序的基础上,添加相似代码很容易实现求更多整数的最大值。
如果我们再添加一个前提条件:三个整数都是正整数,我们还可以进一步修改代码,让代码更加工整:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include<iostream>
usingnamespace std;
intmain()
{
//求三个正整数a、b、c的最大值(注意:a、b、c都是正整数)
int a,b,c,max;
cin>>a>>b>>c;
max = 0; //体会这里赋值成0的用途
if(a>max) max = a;
if(b>max) max = b;
if(c>max) max = c;
cout<<max<<endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
//求三个正整数a、b、c的最大值(注意:a、b、c都是正整数)
int a,b,c,max;
cin>>a>>b>>c;
max = 0; //体会这里赋值成0的用途
if(a>max) max = a;
if(b>max) max = b;
if(c>max) max = c;
cout<<max<<endl;
return 0;
}
#include<iostream>usingnamespace std;int main(){//求三个正整数a、b、c的最大值(注意:a、b、c都是正整数) int a,b,c,max;
cin>>a>>b>>c;
max =0;//体会这里赋值成0的用途 if(a>max) max = a;if(b>max) max = b;if(c>max) max = c;
cout<<max<<endl;return0;}
再来分析前面的代码,程序结构有if嵌套结构,还有先后执行的多个if语句:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include<iostream>
usingnamespace std;
intmain()
{
int a,b,c,max;
cin>>a>>b>>c;
if(a>b&&a>c){
max = a;
}elseif(b>a && b>c){
max = b;
}else{
max = c;
}
cout<<max<<endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int a,b,c,max;
cin>>a>>b>>c;
if(a>b&&a>c){
max = a;
}else if(b>a && b>c){
max = b;
}else{
max = c;
}
cout<<max<<endl;
return 0;
}
#include<iostream>usingnamespace std;int main(){int a,b,c,max;
cin>>a>>b>>c;if(a>b&&a>c){
max = a;}elseif(b>a && b>c){
max = b;}else{
max = c;}
cout<<max<<endl;return0;}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include<iostream>
usingnamespace std;
intmain()
{
int a,b,c,max;
cin>>a>>b>>c;
max = a;
if(b>max) max = b;
if(c>max) max = c;
cout<<max<<endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int a,b,c,max;
cin>>a>>b>>c;
max = a;
if(b>max) max = b;
if(c>max) max = c;
cout<<max<<endl;
return 0;
}
#include<iostream>usingnamespace std;int main(){int a,b,c,max;
cin>>a>>b>>c;
max = a;if(b>max) max = b;if(c>max) max = c;
cout<<max<<endl;return0;}
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
switch(n){
case 0:cout<<"Sunday"<<endl;break;
case 1:cout<<"Monday"<<endl;break;
case 2:cout<<"Tuesday"<<endl;break;
case 3:cout<<"Wednesday"<<endl;break;
case 4:cout<<"Thursday"<<endl;break;
case 5:cout<<"Friday"<<endl;break;
case 6:cout<<"Saturday"<<endl;break;
default:cout<<"Error"<<endl;
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
char degree;
cin>>degree;
switch(degree){
case 'A':
case 'B':
case 'C':
case 'D':
cout<<"Passed";
break;
case 'E':
cout<<"Not Passed";
break;
default:
cout<<"Input Error"<<endl;
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
char degree;
cin>>degree;
switch(degree){
case 'A':
case 'B':
case 'C':
case 'D':
cout<<"Passed";
break;
case 'E':
cout<<"Not Passed";
break;
default:
cout<<"Input Error"<<endl;
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
char degree;
cin>>degree;
switch(degree){
case 'A':
case 'B':
case 'C':
case 'D':
cout<<"Passed";
break;
case 'E':
cout<<"Not Passed";
break;
default:
cout<<"Input Error"<<endl;
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int y,m,d;
cin>>y>>m;
switch(m){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:d = 31;break;
case 4:
case 6:
case 9:
case 11:d = 30;break;
case 2:
if(y%4==0 && y%100!=0 || y%400==0) d = 29;
else d = 28;
break;
}
cout<<d<<endl;
return 0;
}
#include<iostream>usingnamespace std;int main(){int y,m,d;
cin>>y>>m;switch(m){case1:case3:case5:case7:case8:case10:case12:d =31;break;case4:case6:case9:case11:d =30;break;case2:if(y%4==0&& y%100!=0|| y%400==0) d =29;else d =28;break;}
cout<<d<<endl;return0;}