目录
此篇全是简单题,就没写题解,就是记录一下
L1-013 计算阶乘和
问题描述:
对于给定的正整数N,需要你计算 S=1!+2!+3!+...+N!。
输入格式:
输入在一行中给出一个不超过10的正整数N。
输出格式:
在一行中输出S的值。
输入样例:
3
输出样例:
9
实现代码:
#include<iostream>
using namespace std;
int getnum(int x)
{
if (x == 1) return 1;
int a = getnum(x - 1);
return a * x;
}
int main()
{
int n;
cin >> n;
int sum = 0;
for (int i = 1; i <= n; i++)
{
sum += getnum(i);
}
cout << sum << endl;
}
L1-014 简单题
问题描述:
这次真的没骗你 —— 这道超级简单的题目没有任何输入。
你只需要在一行中输出事实:This is a simple problem.
就可以了。
输入样例:
无
输出样例:
This is a simple problem.
实现代码:
#include<iostream>
using namespace std;
int main ()
{
cout <<"This is a simple problem."<<endl;
}
L1-015 跟奥巴马一起画方块
问题描述:
美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统。2014年底,为庆祝“计算机科学教育周”正式启动,奥巴马编写了很简单的计算机代码:在屏幕上画一个正方形。现在你也跟他一起画吧!
输入格式:
输入在一行中给出正方形边长N(3≤N≤21)和组成正方形边的某种字符C
,间隔一个空格。
输出格式:
输出由给定字符C
画出的正方形。但是注意到行间距比列间距大,所以为了让结果看上去更像正方形,我们输出的行数实际上是列数的50%(四舍五入取整)。
输入样例:
10 a
输出样例:
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
实现代码:
#include<iostream>
using namespace std;
int main()
{
float n;
char c;
cin >> n >> c;
int rol = n / 2 + 0.5;
for(int i = 0; i < rol; i++)
{
cout << string(n, c) << endl;
}
}
原理思路:
我的评价是,这题也配15分? 输出就行,注意四舍五入,这里有个小技巧,除 2 后加 0.5 转成 int 就可以模拟四舍五入了,当然也可以用函数,不过没必要,至于为什么 加 0.5 就可以模拟,你想想就懂了。