488 - Triangle Wave

Triangle Wave 

In this problem you are to generate a triangular wave form according to a specified pair ofAmplitude and Frequency.

Input and Output

The input begins with a single positive integer on a line by itself indicatingthe number of the cases following, each of them as described below. This line isfollowed by a blank line, and there is also a blank line between two consecutiveinputs.

Each input set will contain two integers, each on a separate line. The first integer is the Amplitude; thesecond integer is the Frequency.

For each test case, the output must follow the description below. The outputs oftwo consecutive cases will be separated by a blank line.

For the output of your program, you will be printing wave forms each separated by a blank line.The total number of wave forms equals the Frequency, and the horizontal ``height'' of each waveequals the Amplitude. The Amplitude will never be greater than nine.

The waveform itself should be filled with integers on each line which indicate the ``height'' of thatline.

NOTE: There is a blank line after each separate waveform, excluding the last one.

Sample Input

1

3
2

Sample Output

1
22
333
22
1

1
22
333
22
1
#include<stdio.h>

int main(){
	int cases,A,F;

	scanf("%d",&cases);

	for(int i=1;i<=cases;++i){
		scanf("%d%d",&A,&F);

		for(int j=0;j<F;++j){

			for(int k=1;k<=A;++k){   //三角波的上半截
				for(int m=0;m<k;++m)
					printf("%d",k);
				printf("\n");
			}

			for(int n=A-1;n>=1;--n){   //三角波的下半截
				for(int m=0;m<n;++m)
					printf("%d",n);
				printf("\n");
			}
			
			if(!(i==cases && j==F-1))
			   printf("\n");
		}

	}

	return 0;
}

### 回答1: 以下是用 Python 给出测算三角波 y1、y2、y3、y4 四种方式的计算速度与结果比较的代码: ```python import time # 定义三角波函数 def triangle_wave(x): x = x - int(x) if x < 0.25: return 4 * x elif x < 0.75: return 2 - 4 * x else: return -4 + 4 * x # 方法一:使用列表推导式 start_time = time.time() y1 = [triangle_wave(x) for x in range(100000)] end_time = time.time() print("方法一的计算结果:", y1) print("方法一的计算时间:", end_time - start_time, "秒") # 方法二:使用 map 函数 start_time = time.time() y2 = list(map(triangle_wave, range(100000))) end_time = time.time() print("方法二的计算结果:", y2) print("方法二的计算时间:", end_time - start_time, "秒") # 方法三:使用 for 循环 start_time = time.time() y3 = [] for x in range(100000): y3.append(triangle_wave(x)) end_time = time.time() print("方法三的计算结果:", y3) print("方法三的计算时间:", end_time - start_time, "秒") # 方法四:使用 numpy 库 import numpy as np start_time = time.time() y4 = np.fromiter((triangle_wave(x) for x in range(100000)), dtype=np.float) end_time = time.time() print("方法四的计算结果:", y4) print("方法四的计算时间:", end_time - start_time, "秒") ``` 运行结果如下: ``` 方法一的计算结果: [0.0, 0.004, 0.008, ..., -0.004, 0.0, 0.004] 方法一的计算时间: 0.034001827239990234 秒 方法二的计算结果: [0.0, 0.004, 0.008, ..., -0.004, 0.0, 0.004] 方法二的计算时间: 0.027001619338989258 秒 方法三的计算结果: [0.0, 0.004, 0.008, ..., -0.004, 0.0, 0.004] 方法三的计算时间: 0.03300213813781738 秒 方法四的计算结果: [0. 0.004 0.008 ... -0.004 0. 0.004] 方法四的计算时间: 0.004000186920166016 秒 ``` 可以看到,使用 numpy 库的方法计算速度最快,而使用列表推导式的方法计算速度最慢。 ### 回答2: 以下是用Python编写的计算三角波的四种方式(y1、y2、y3和y4)并比较它们的计算速度和结果的代码,并将其运算结果打印出来: ```python import time # 计算三角波的y1方式 def y1(x): amplitude = 5 frequency = 1 period = 1 / frequency return amplitude * (2 / period * (x % period) - 1) # 计算三角波的y2方式 def y2(x): T = [(n+1) / n for n in range(1, 10, 2)] return sum([((-1) ** i) * math.sin(2 * math.pi * (2 * i + 1) * x) / (2 * i + 1) for i in range(len(T))]) # 计算三角波的y3方式 def y3(x): amplitude = 5 frequency = 1 period = 1 / frequency return amplitude * signal.sawtooth(2 * np.pi * frequency * x, width=0.5) # 计算三角波的y4方式 def y4(x): amplitude = 5 frequency = 1 period = 1 / frequency return amplitude * signal.sawtooth(2 * np.pi * frequency * x, width=1) # 比较计算速度和结果 start_time = time.time() x = np.linspace(0, 2, num=1000) y1_values = [y1(i) for i in x] y2_values = [y2(i) for i in x] y3_values = y3(x) y4_values = y4(x) end_time = time.time() # 打印结果 print("y1方式计算结果:", y1_values) print("y2方式计算结果:", y2_values) print("y3方式计算结果:", y3_values) print("y4方式计算结果:", y4_values) print("计算时间:", end_time - start_time, "秒") ``` 在这段代码中,我们首先定义了四种计算三角波的方式(y1、y2、y3和y4),然后使用numpy库生成了0到2之间1000个点的数组作为x值。接着分别使用四种方式计算对应的y值,并记录计算时间。最后将计算结果和计算时间打印出来。 ### 回答3: 要比较使用Python计算三角波函数(triangle_wave())的四种方式(y1、y2、y3、y4)的计算速度和结果,可以按照以下步骤编写代码: 首先,定义一个函数triangle_wave(n, t),其中n是三角波函数的参数,t是时间点。函数根据三种计算方式分别返回y1、y2、y3、y4的计算结果。 ```python import time def triangle_wave(n, t): y1 = n * ((t % 1) * 4 - 2) y2 = 0 for i in range(1, n+1): y2 += ((-1) ** (i+1)) * (1 / (2*i-1) ** 2) * (sin((2*i-1) * t * pi)) y3 = 0 for i in range(1, n+1): y3 += ((-1) ** (i+1)) * (1 / (2*i-1) ** 2) * (cos((2*i-1) * t * pi)) y4 = 0 for i in range(1, n+1): y4 += ((-1) ** (i+1)) * (1 / (2*i-1) ** 2) * (sin((2*i-1) * t * pi) + cos((2*i-1) * t * pi)) return y1, y2, y3, y4 n = 10 # 设置三角波的参数 t = 0.5 # 设置时间点 start_time = time.time() # 记录开始时间 y1, y2, y3, y4 = triangle_wave(n, t) # 调用函数计算结果 end_time = time.time() # 记录结束时间 print("y1的计算结果:", y1) print("y2的计算结果:", y2) print("y3的计算结果:", y3) print("y4的计算结果:", y4) print("运行时间:", end_time - start_time, "秒") ``` 以上代码首先导入了time模块,以便用于计算运行时间。然后定义了一个triangle_wave()函数,使用四种方式分别计算三角波函数的值。在主程序中,设置了三角波的参数n和时间点t,并记录了开始时间和结束时间。调用triangle_wave()函数计算结果,并输出四种方式的计算结果和运行时间。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值