分享C++部分函数/格式/算法02

分享C++部分函数/格式/算法02

前期回顾
分享C++部分函数/格式/算法01

作业

小猪猪
分析:直接输出即可,注意换行
参考代码
#include<iostream>
using namespace std;
int main() {
	cout<<"╭︿︿︿╮"<<endl;
	cout<<"{/ o  o /}"<<endl;
	cout<<"( (oo) )"<<endl;
	cout<<" ︶ ︶︶"<<endl;
	return 0;
}
a-b问题
分析:使用变量+输入输出+运算来完成
参考代码:
#include<iostream>
using namespace std;
int main() {
	long long a,b,c;
	cin>>a>>b;
	c=a-b;
	cout<<c;
	return 0;
}

入门2

if条件句(入门)
  • 例题
    判断奇偶性
#include<iostream>
using namespace std;
int main() {
	int i;
	cin>>i;
	if(i%2==1)cout<<i<<"是奇数.";
	else cout<<i<<"是偶数.";
	return 0;
}

看到什么新的东西了吗?对,是if如果和else否则.
if(条件) {

}
else (可以再接if) {

}
条件:

==:a与b相等
<:a<b
>:a>b
<=:a<b或a==b
>=:a>b或a==b
for循环
  • 例题
    比较两个程序,说说哪个程序更方便
    输出1-20,每个数一行
//程序A
#include<iostream>
using namespace std;
int main() {
	cout<<1<<endl;
	cout<<2<<endl;
	cout<<3<<endl;
	cout<<4<<endl;
	cout<<5<<endl;
	cout<<6<<endl;
	cout<<7<<endl;
	cout<<8<<endl;
	cout<<9<<endl;
	cout<<10<<endl;
	cout<<11<<endl;
	cout<<12<<endl;
	cout<<13<<endl;
	cout<<14<<endl;
	cout<<15<<endl;
	cout<<16<<endl;
	cout<<17<<endl;
	cout<<18<<endl;
	cout<<19<<endl;
	cout<<20<<endl;
	return 0;
}
//程序B
#include<iostream>
using namespace std;
int main() {
	for(int i=1;i<=20;i++)cout<<i<<endl;
	return 0;
}

看到了没有?同样的结果,一个很长,一个很短.如果把输出改成1-99每行一个,
程序B只要改两个数,程序A呢,不敢想象…
程序B运用了循环.
格式:for(变量=数(可省,只要已初始化),终止条件,每轮循环结束时做的事) {
  执行代码;
}

一维数组
  • 例题
    比较两个程序,说说哪个程序更方便
    输入10个数,输出10个数,一行一个
//程序A
#include<iostream>
using namespace std;
int main() {
	int a1,a2,a3,a4,a5,a6,a7,a8,a9,a10;
	cin>a1>>a2>>a3>>a4>>a5>>a6>>a7>>a8>>a9>>a10;
	cout<<a1<<endl;
	cout<<a2<<endl;
	cout<<a3<<endl;
	cout<<a4<<endl;
	cout<<a5<<endl;
	cout<<a6<<endl;
	cout<<a7<<endl;
	cout<<a8<<endl;
	cout<<a9<<endl;
	cout<<a10<<endl;
	return 0;
}
//程序B
#include<iostream>
using namespace std;
int main() {
	int a[10];
	for(int i=0;i<10;i++)cout<<a[i];
	for(int i=0;i<10;i++)cout<<a[i]<<endl;
	return 0;
}

很明显,程序B方便.
程序B运用了一维数组.
类型 名字 [ 变量数 ] ;
访问:
名字 [ 第几个变量(0~变量数-1)]

(练习)三的倍数
输入i,输出i是不是3的倍数.如果是,输出"YES",如果不是,输出"NO".

答案请见下期

(练习)数数
输入n,输出1~n,每行一个.

答案请见下期

(练习)过目不忘
输入n,再输入n个数,请你输出这n个数,一行一个.

答案请见下期

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值