定义表达式如下:
- x 是一个表达式。
- 如果 S 是一个表达式,那么 (S) 也是一个表达式;
- 如果 S1 和 S2 是表达式,则 S1 + S2 和 S1 - S2 是表达式。
在程序的输入中,接收到单个整数 N (0?N?10^6)。
程序应该打印给定长度 N 的可能表达式的数量。
例如,如果输入收到 3,则答案为 3。如果输入收到 5,则答案为 11。
因为,长度为 3 的可能表达式:
(x)
x + x
x ? x
Run Code Online (Sandbox Code Playgroud)
长度为 5 的可能表达式:
((x))
(x) + x
(x) ? x
(x + x)
(x ? x)
x + (x)
x + x + x
x + x ? x
x ? (x)
x ? x + x
x …Run Code Online (Sandbox Code Playgroud) 我想在 heroku 中创建一个应用程序,它将一些数据作为 websocket 服务器并将其作为不和谐机器人发送。
这就是我尝试实现这个想法的方式:
# -*- coding: utf-8 -*-
import asyncio, websockets, discord
client = discord.Client()
channel = client.get_channel(...)
print(channel)
async def response(websocket, path):
global channel
r = await websocket.recv()
print(r)
await channel.send(str(r))
PORT = os.environ.get('PORT')
start_server = websockets.serve(response, '0.0.0.0', os.environ.get('PORT'))
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
Run Code Online (Sandbox Code Playgroud)
但我不知道如何在这样的代码中启动一个不和谐的机器人,以便它与 websocket 服务器并行工作。请告诉我该怎么做。
该程序接收一个整数数组。有必要在每一个新行上输出数组的第 i 个元素的总和。例如,对于这样的数组{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},答案将是这样的:
55 //1+2+3+4+5+6+7+8+9+10
30 //2+4+6+8+10
18 //3+6+9
12 //4+8
15 //5+10
6 //6
7 //7
8 //8
9 //9
10 //10
Run Code Online (Sandbox Code Playgroud)
这是我解决这个问题的代码:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int main(void) {
int N, i, j, S;
scanf("%d",&N); //length of array
int a[N];
for(i = 0; i < N; ++i) scanf("%d",&a[i]);
for(i = 1; i <= N; ++i) {
S = 0;
for(j = 0; j < N; …Run Code Online (Sandbox Code Playgroud)