python - 对于用户输入 n,并且 1<=i
For a user input n, and 1<=i<j<=n, find the number of pairs where i*i*i=j*j using python
问题描述
For a user input n, and 1<=i<j<=n, find the number of pairs where i*i*i=j*j using python
程序需要从用户那里获取输入,如果用户输入是 50,则输出应该是 3,因为有 3 个这样的对:(1,1),(4,8),(9,27)使用 python。
def solution(n):
count=0
for i in range(1,n):
for j in range(i,n):
if (i**3==j*j):
count+=1
return count
n=int(input())
out=solution(n)
print(out)
这是我写的函数。它有效,但在我正在练习的站点中,它超时并要求我进一步优化它。我能做些什么?
无法处理针对 Mac 编写的 C 语言 UDP 客户端-服务器问题
我正在用 C 语言为 MacOS 编写一个 udp 客户端-服务器程序。找到了 UDP 协议的实现,它在 Linux 上正常工作(由我自己检查),但是当我尝试在 Mac 上构建它时,它返回未声明变量 MSG_CONFIRM 的警告,当我尝试运行它时,程序没有t 获取或发送任何消息。
我已经尝试在 Google 中找到解决方案,但没有任何解决方案。接下来我可以尝试什么?
这是“客户”
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdbool.h>
#include "utils.h"
#define BUF_SIZE 1024
int main(int argc, char ** argv) {
Args args = get_args(argc, argv);
int sock_fd;
struct sockaddr_in server_addr;
if ( (sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
throw(BAD_EXIT_STATUS, "Socket creation failed");
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(args.port);
server_addr.sin_addr.s_addr = inet_addr(args.IP);
while(1) {
char buffer[BUF_SIZE];
char *hello = readline(stdin);
int n;
socklen_t len;
sendto(sock_fd, (const char *) hello, strlen(hello), MSG_CONFIRM,
(const struct sockaddr *) &server_addr, sizeof(server_addr));
n = recvfrom(sock_fd, (char *) buffer, BUF_SIZE, MSG_WAITALL,
(struct sockaddr *) &server_addr, &len);
buffer[n] = '\0';
printf("[Server]: %s", buffer);
}
close(sock_fd);
return 0;
}
这是“服务器”
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <math.h>
#include "utils.h"
#include "obrab.h"
#define BUF_SIZE 1024
#ifndef MSG_CONFIRM
#define MSG_CONFIRM 2048
#endif
int main(int argc, char** argv) {
Args args = get_args(argc, argv);
int sock_fd;
struct sockaddr_in server_addr, client_addr;
if ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
throw(BAD_EXIT_STATUS, "Socket creation failed");
}
memset(&server_addr, 0, sizeof(server_addr));
memset(&client_addr, 0, sizeof(client_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(args.port);
server_addr.sin_addr.s_addr = inet_addr(args.IP);
if (bind(sock_fd, (const struct sockaddr*) &server_addr, sizeof(server_addr)) < 0) {
throw(BAD_EXIT_STATUS, "Bind failed");
}
while(1) {
char buffer[BUF_SIZE];
int n;
socklen_t len;
n = recvfrom(sock_fd, (char*) buffer, BUF_SIZE,
MSG_WAITALL, (struct sockaddr*) &client_addr, &len);
buffer[n] = '\0';
if (fork() == 0) {
char *hello = buffer;
sendto(sock_fd, (const char *) hello, strlen(hello),
MSG_CONFIRM, (const struct sockaddr *) &client_addr, len);
} else {
printf("[Client]: %s", buffer);
}
}
}
我还创建了一些头文件,下面有一个代码
“钥匙.h”
#ifndef keys_h
#define keys_h
// Struct for console arguments
unsigned short int prt;
char* L2ADDR;
char* L2LOGFILE;
unsigned short int L2WAIT;
typedef struct Args {
// unsigned short int (*L2PORT) (unsigned short int L2PORT); // -p
// char* (*L2WAIT) (char* L2ADDR); //-a
// char* (*L2LOGFILE) (char* L2LOGFILE); //-l
// unsigned short int (*L2WAIT) (unsigned short int L2WAIT); //-w
} Args;
char ip[] = "128.0.0.1";
unsigned a, b, c, d;
// reception and recognition of args
Args get_args(int argc, char **argv) {
Args args = {};
int opt;
char *opts = "palwvh";
while((opt = getopt(argc, argv, opts)) != -1) {
switch(opt) {
case 'p':
break;
case 'a':
if (sscanf(ip, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 && a < 256) printf("YES\n");
else {
perror("NO\n");
exit(2);
}
break;
case 'l':
// currentParser = lblog;
break;
case 'w':
// currentParser = lbwait;
break;
case 'v':
printf("Version: 0.0.1 omega\n");
break;
case 'h':
printf("Use can use following keys: \n"
"\t-v -- prints out a version of the program;\n"
"\t-h -- output of the manual;\n"
"\t-w N -- Imitates job pausing serving process for N sec;\n"
"\t-d -- Set up program in daemon-mode;\n"
"\t-l /path/to/log -- set up a path to log-file;\n"
"\t-a ip -- Set up an adress in what server listens;\n"
"\t-p port -- Set up port, in what server listens.\n\n");
break;
default:
perror("Unknown key! Use -h key to read manual");
}
}
return args;
}
#endif /* keys_h */
“实用程序.h”
#ifndef UTILS_H
#define UTILS_H
#define BAD_EXIT_STATUS 1
typedef struct Args {
int waitFor; // -w N
bool isDaemon; // -d
char* logFile; // -l path
char* IP; // -a IP
int port; // -p port
} Args;
Args get_args(int, char**);
void throw(int, char*);
char * readline(FILE*);
#endif
编译时需要编写如下命令: ./server -a ip -p port (输入 127.0.0.1 和 8080) 然后你需要在另一个窗口中使用类似的关键参数运行客户端。理论上,您在客户端编写消息,然后将它们发送到服务器。但它不会发送或接收任何消息。
解决方案
您可能无法计算找到匹配项的次数,但请保存索引:
def solution(n):
result = []
for i in range(1, n):
for j in range(i, n):
if i ** 3 == j ** 2:
result.append((i, j))
return result
# with list comprehension
def solution(n):
return [(i, j) for i in range(1, n) for j in range(i, n) if i ** 3 == j ** 2]
优化
通过查看这些值,您可以确定哪些值可以匹配,以获得i**2 == j**3
t=you need i = x**3
,j = x**2
因此一个循环就足够了:
def solution(n):
result = []
for i in range(1, ceil(n ** (1 / 3))):
result.append((i ** 2, i ** 3))
return result
# with list comprehension
def solution(n):
return [(i ** 2, i ** 3) for i in range(1, ceil(n ** (1 / 3)))]
推荐阅读
- flutter - 如何使用预设的电子邮件和密码登录 google oauth?
- angular - Twilio LocalVideoTrack 覆盖开始和停止事件
- javascript - 与列表框绑定的下拉列表和与按钮可见性绑定的列表框的项目选择 React
- swift - URL.init?(string: String) 在不应该的时候返回 'nil'
- powershell - 在powershell上递归重命名其他子目录的子目录文件?
- php - 未在 Apache 服务器上指定空白致命错误
- javascript - 使用 Dojo lang.hitch 将参数传递给被调用函数
- usb - 如何添加字符串描述符
- git - 如何删除由 repo 中的大文件导致的未推送提交,该文件已被删除?
- ios - 如何调用指示和通知作为外围 iOS
For a user input n, and 1<=i<j<=n, find the number of pairs where i*i*i=j*j using python
问题描述
For a user input n, and 1<=i<j<=n, find the number of pairs where i*i*i=j*j using python
程序需要从用户那里获取输入,如果用户输入是 50,则输出应该是 3,因为有 3 个这样的对:(1,1),(4,8),(9,27)使用 python。
def solution(n):
count=0
for i in range(1,n):
for j in range(i,n):
if (i**3==j*j):
count+=1
return count
n=int(input())
out=solution(n)
print(out)
这是我写的函数。它有效,但在我正在练习的站点中,它超时并要求我进一步优化它。我能做些什么?
无法处理针对 Mac 编写的 C 语言 UDP 客户端-服务器问题
我正在用 C 语言为 MacOS 编写一个 udp 客户端-服务器程序。找到了 UDP 协议的实现,它在 Linux 上正常工作(由我自己检查),但是当我尝试在 Mac 上构建它时,它返回未声明变量 MSG_CONFIRM 的警告,当我尝试运行它时,程序没有t 获取或发送任何消息。
我已经尝试在 Google 中找到解决方案,但没有任何解决方案。接下来我可以尝试什么?
这是“客户”
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdbool.h>
#include "utils.h"
#define BUF_SIZE 1024
int main(int argc, char ** argv) {
Args args = get_args(argc, argv);
int sock_fd;
struct sockaddr_in server_addr;
if ( (sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
throw(BAD_EXIT_STATUS, "Socket creation failed");
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(args.port);
server_addr.sin_addr.s_addr = inet_addr(args.IP);
while(1) {
char buffer[BUF_SIZE];
char *hello = readline(stdin);
int n;
socklen_t len;
sendto(sock_fd, (const char *) hello, strlen(hello), MSG_CONFIRM,
(const struct sockaddr *) &server_addr, sizeof(server_addr));
n = recvfrom(sock_fd, (char *) buffer, BUF_SIZE, MSG_WAITALL,
(struct sockaddr *) &server_addr, &len);
buffer[n] = '\0';
printf("[Server]: %s", buffer);
}
close(sock_fd);
return 0;
}
这是“服务器”
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <math.h>
#include "utils.h"
#include "obrab.h"
#define BUF_SIZE 1024
#ifndef MSG_CONFIRM
#define MSG_CONFIRM 2048
#endif
int main(int argc, char** argv) {
Args args = get_args(argc, argv);
int sock_fd;
struct sockaddr_in server_addr, client_addr;
if ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
throw(BAD_EXIT_STATUS, "Socket creation failed");
}
memset(&server_addr, 0, sizeof(server_addr));
memset(&client_addr, 0, sizeof(client_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(args.port);
server_addr.sin_addr.s_addr = inet_addr(args.IP);
if (bind(sock_fd, (const struct sockaddr*) &server_addr, sizeof(server_addr)) < 0) {
throw(BAD_EXIT_STATUS, "Bind failed");
}
while(1) {
char buffer[BUF_SIZE];
int n;
socklen_t len;
n = recvfrom(sock_fd, (char*) buffer, BUF_SIZE,
MSG_WAITALL, (struct sockaddr*) &client_addr, &len);
buffer[n] = '\0';
if (fork() == 0) {
char *hello = buffer;
sendto(sock_fd, (const char *) hello, strlen(hello),
MSG_CONFIRM, (const struct sockaddr *) &client_addr, len);
} else {
printf("[Client]: %s", buffer);
}
}
}
我还创建了一些头文件,下面有一个代码
“钥匙.h”
#ifndef keys_h
#define keys_h
// Struct for console arguments
unsigned short int prt;
char* L2ADDR;
char* L2LOGFILE;
unsigned short int L2WAIT;
typedef struct Args {
// unsigned short int (*L2PORT) (unsigned short int L2PORT); // -p
// char* (*L2WAIT) (char* L2ADDR); //-a
// char* (*L2LOGFILE) (char* L2LOGFILE); //-l
// unsigned short int (*L2WAIT) (unsigned short int L2WAIT); //-w
} Args;
char ip[] = "128.0.0.1";
unsigned a, b, c, d;
// reception and recognition of args
Args get_args(int argc, char **argv) {
Args args = {};
int opt;
char *opts = "palwvh";
while((opt = getopt(argc, argv, opts)) != -1) {
switch(opt) {
case 'p':
break;
case 'a':
if (sscanf(ip, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 && a < 256) printf("YES\n");
else {
perror("NO\n");
exit(2);
}
break;
case 'l':
// currentParser = lblog;
break;
case 'w':
// currentParser = lbwait;
break;
case 'v':
printf("Version: 0.0.1 omega\n");
break;
case 'h':
printf("Use can use following keys: \n"
"\t-v -- prints out a version of the program;\n"
"\t-h -- output of the manual;\n"
"\t-w N -- Imitates job pausing serving process for N sec;\n"
"\t-d -- Set up program in daemon-mode;\n"
"\t-l /path/to/log -- set up a path to log-file;\n"
"\t-a ip -- Set up an adress in what server listens;\n"
"\t-p port -- Set up port, in what server listens.\n\n");
break;
default:
perror("Unknown key! Use -h key to read manual");
}
}
return args;
}
#endif /* keys_h */
“实用程序.h”
#ifndef UTILS_H
#define UTILS_H
#define BAD_EXIT_STATUS 1
typedef struct Args {
int waitFor; // -w N
bool isDaemon; // -d
char* logFile; // -l path
char* IP; // -a IP
int port; // -p port
} Args;
Args get_args(int, char**);
void throw(int, char*);
char * readline(FILE*);
#endif
编译时需要编写如下命令: ./server -a ip -p port (输入 127.0.0.1 和 8080) 然后你需要在另一个窗口中使用类似的关键参数运行客户端。理论上,您在客户端编写消息,然后将它们发送到服务器。但它不会发送或接收任何消息。
解决方案
您可能无法计算找到匹配项的次数,但请保存索引:
def solution(n):
result = []
for i in range(1, n):
for j in range(i, n):
if i ** 3 == j ** 2:
result.append((i, j))
return result
# with list comprehension
def solution(n):
return [(i, j) for i in range(1, n) for j in range(i, n) if i ** 3 == j ** 2]
优化
通过查看这些值,您可以确定哪些值可以匹配,以获得i**2 == j**3
t=you need i = x**3
,j = x**2
因此一个循环就足够了:
def solution(n):
result = []
for i in range(1, ceil(n ** (1 / 3))):
result.append((i ** 2, i ** 3))
return result
# with list comprehension
def solution(n):
return [(i ** 2, i ** 3) for i in range(1, ceil(n ** (1 / 3)))]
推荐阅读
- flutter - 如何使用预设的电子邮件和密码登录 google oauth?
- angular - Twilio LocalVideoTrack 覆盖开始和停止事件
- javascript - 与列表框绑定的下拉列表和与按钮可见性绑定的列表框的项目选择 React
- swift - URL.init?(string: String) 在不应该的时候返回 'nil'
- powershell - 在powershell上递归重命名其他子目录的子目录文件?
- php - 未在 Apache 服务器上指定空白致命错误
- javascript - 使用 Dojo lang.hitch 将参数传递给被调用函数
- usb - 如何添加字符串描述符
- git - 如何删除由 repo 中的大文件导致的未推送提交,该文件已被删除?
- ios - 如何调用指示和通知作为外围 iOS