7-3 画菱形 分数 10 作者 翁恺 单位 浙江大学 菱形是一种特殊的平行四边形,是四条边均相等的平行四边形。题目给出菱形的边长n,用*画出菱形。如n=1,输出: * n=2,输出: * *** * n=3,输出: * *** ***** *** *
时间: 2024-07-03 08:00:52 浏览: 92
菱形图案的绘制通常使用嵌套循环和字符串拼接的方法实现。翁恺教授,浙江大学的作者,编写了一个程序来生成指定边长的菱形。这里是一个简单的Python代码示例,用于生成给定边长n的菱形:
```python
def draw_diamond(n):
for i in range(n):
# 上半部分菱形
if i <= n // 2:
print(' ' * (n // 2 - i) + '*' * (2 * i + 1))
# 下半部分菱形
else:
print(' ' * (i - n // 2) + '*' * (2 * (n - i) - 1))
# 示例
draw_diamond(3)
```
当你调用`draw_diamond(3)`时,会得到以下输出:
```
*
***
*****
*******
*********
*******
*****
***
*
```
相关问题
duplicated numbers 分数 10 作者 翁恺 单位 浙江大学 this program reads a lot
of integers and checks whether there are any duplicates. If there are duplicates, it prints "YES", otherwise it prints "NO". Here's the code:
```python
n = int(input())
a = list(map(int, input().split()))
s = set(a)
if len(a) != len(s):
print("YES")
else:
print("NO")
```
The program first reads in the number of integers `n`, and then reads in `n` integers. It then converts the input into a list `a`, and creates a set `s` from `a`. Since sets only contain unique elements, if the length of `a` is not equal to the length of `s`, it means that there are duplicates, and the program prints "YES". Otherwise, the program prints "NO".
6-3 Duplicated Numbers 分数 10 作者 翁恺 单位 浙江大学 This program reads a lot of integers, in which may be duplicated numbers. The program picks out all the duplicated ones and sorts the remainders in a descendent order. 函数接口定义: public static ArrayList<Integer> pi
ecewiseDuplicatedNumbers(int[] nums)
其中,
输入参数:
int[] nums :整型数组,表示输入的一组整数。
输出参数:
ArrayList<Integer> :整型数组列表,表示所有重复出现过的整数,按照降序排列。
示例:
输入:nums = {3, 1, 2, 3, 4, 5, 2, 4}
输出:[4, 3, 2]
解释:其中,4、3、2 都是重复出现过的整数,按照降序排列后,输出为 [4, 3, 2]。
算法思路:
1. 使用HashMap记录每个数字出现的次数;
2. 遍历HashMap,将出现次数大于1的数字加入到ArrayList中;
3. 对ArrayList进行降序排列,返回结果。
Java代码实现: