算法题Day1
1. 练习1:Hello,World!
解题步骤:
using namespace std;
int main()
{cout<<"Hello,World!"<<endl;return 0;
}
2. 练习2:打印飞机
解题步骤:
#include <iostream>
using namespace std;
int main()
{cout << " ** " << endl;cout << " ** " << endl;cout << "************" << endl;cout << "************" << endl;cout << " * * " << endl;cout << " * * " << endl;return 0;
}
3. 练习3:第⼆个整数
解题步骤:
#include <iostream>
using namespace std;
int main()
{int n1;
// cin >> n1 >>endl; cin后面不能使用endlcin >> n1; cout << n1 << endl;return 0;
}
注意事项:
- 这⾥的 n1 和 n2 两个整数,可以分两次读⼊,也可以⼀次读⼊,只有获取到第⼆个输⼊值才能输出结果。
- cin 是⽀持连续读⼊⼏个数值的
- cout 也是⽀连续输出⼏个数值的
- 在 C++ 中, cin 是用于从标准输入(通常是键盘)读取数据的输入流对象,它的用法是 cin >> 变量 ,用于将输入的数据存储到指定变量中。而 endl 是用于输出流(如 cout )的操纵符,作用是输出一个换行符并刷新输出缓冲区,它不能用于 cin 的输入操作中。所以第 6 行 cin >> n1 >> endl; 是错误的,正确的写法应该是 cin >> n1; 。
4. 练习4:字符三⻆形
解题步骤:
#include<iostream>
using namespace std;
int main()
{char c = 0;cin >> c;cout << " " << c << endl;cout << " " <<c << c << c << endl;cout << c << c << c << c << c <<endl;return 0;
}
5. 练习5:整数
解题步骤:
#include <iostream>
using namespace std;
int main()
{int a;cin >> a;cout << a;return 0;
}