就 Unicode 正则表达式而言,表达式应该是什么样子:任何字符,不包括世界上任何字母表中的任何单词和任何数字?
任何字符,不包括世界上任何字母表中的任何单词,都表示为:
\\P{L}+
任何字符,不包括任何数字:
\\P{N}
但是如何从中获得一个考虑任何字符的正则表达式,除了数字或任何字母表中的任何单词
以下是 unicode 正则表达式站点 https://www.regular-expressions.info/unicode.html的链接
就 Unicode 正则表达式而言,表达式应该是什么样子:任何字符,不包括世界上任何字母表中的任何单词和任何数字?
任何字符,不包括世界上任何字母表中的任何单词,都表示为:
\\P{L}+
任何字符,不包括任何数字:
\\P{N}
但是如何从中获得一个考虑任何字符的正则表达式,除了数字或任何字母表中的任何单词
以下是 unicode 正则表达式站点 https://www.regular-expressions.info/unicode.html的链接
执行程序时出现以下错误,它们是什么意思,我该如何解决?
[SUREFIRE] std/in stream corrupted
java.io.IOException: Command NOOP unexpectedly read Void data with length 4.
at org.apache.maven.surefire.booter.MasterProcessCommand.decode(MasterProcessCommand.java:130)
at org.apache.maven.surefire.booter.CommandReader$CommandRunnable.run(CommandReader.java:391)
at java.base/java.lang.Thread.run(Thread.java:833)
System.exit() or native command error interrupted process checker.
java.lang.IllegalStateException: Cannot use PPID 10756 process information. Going to use NOOP events.
at org.apache.maven.surefire.booter.PpidChecker.checkProcessInfo(PpidChecker.java:155)
at org.apache.maven.surefire.booter.PpidChecker.isProcessAlive(PpidChecker.java:116)
at org.apache.maven.surefire.booter.ForkedBooter$2.run(ForkedBooter.java:214)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
at java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305)
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:833)
执行这段代码时:
final String testCases = MethodSourceParametrizedTesting.testCases()
.map(arg -> Arrays.stream(arg.get()).map(Object::toString).collect(joining(",")))
.collect(joining(";"));
出现以下错误:
java: cannot find symbol
symbol: method map((arg)->Arr[...]",")))
location: interface java.lang.String[][]
测试本身:
class MethodSourceParametrizedTesting {
Factorial factorial = new Factorial();
public static String[][] testCases() {
return new String[][]{
{"1","1"}, {"2", "2"}, {"5", "120"}};
}
@ParameterizedTest
@MethodSource("testCases")
void testFactorial(String in, String expected) {
assertEquals(expected, factorial.factorial(in));
}
}
给定一个有向图,需要构造一个哈密顿圈。我不知道为什么程序不能正常工作。例如:
顶点 = 5
边 = 12
0 4
0 1
1 2
2 1
2 3
3 2
3 4
4 3
0 3
3 0
0 2
4 1
输出:0,1,2,3,4
预期结果:0,4,1, 2、3
n = int(input('Vertices: '))
m = int(input('Edges: '))
adj = [[0] * n for _ in range(n)]
for i in range(m):
k, l = map(int, input().split())
adj[k][l] = 1
used = [False] * n
path = []
def hamilton (v):
path.append(v)
if len(path) == n:
if adj[path[0]][path[-1]] == 1:
return True
else:
path.pop()
return False
used[v] = True
for next in range(n):
if adj[v][next] == 1 and not used[next]:
if hamilton(next):
return True
used[v] = False
path.pop()
return False
for i in range(n):
hamilton(i)
print (path)
path.clear()
给出了一个没有多个边和环的无向图。该程序给出了这个错误:
File "dfsbfs.py", line 12, in dfs
new_vertices = [j for j in adj[u] if j not in visited]
IndexError: list index out of range.
我不明白为什么会这样。
n = int(input('Vertices: '))
m = int(input('Edges: '))
adj = []
for i in range(m):
adj.append(list(map(int, input().split())))
def dfs(v):
visited = {v}
to_explore = [v]
while to_explore:
u = to_explore.pop()
print (u)
new_vertices = [j for j in adj[u] if j not in visited]
to_explore.extend(new_vertices)
visited.update(new_vertices)
dfs(0)
程序给出-nan,错误是什么?
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
double f(double x) {
return (x * x - 2);
}
double f1(double x) {
double h = 0.01;
return ((f(x+h) - f(x-h))/2*h);
}
double f2(double x) {
double h = 0.01, s;
s = (f(x+h) - 2*f(x) + f(x-h))/h*h;
return s;
}
double newton (double a, double b, double eps) {
double xold, xnew, c;
c = (a + b)/2;
if(f1(c)*f2(c) > 0)
xnew = a;
else
xnew = b;
do {
xold = xnew;
xnew = xold - f(xold)/f1(xold);
} while(fabs(xold - xnew) > eps);
return xnew;
}
int main () {
double a, b, eps;
puts("eps a b");
scanf ("%lf %lf %lf", &eps, &a, &b);
printf ("%lf\n", newton(a, b, eps));
}
你能提出什么问题吗?[0;2]在具有精度的段上0.5,程序应该输出近似0.9,但在这种情况下它输出1.8。
#include <stdio.h>
#include <math.h>
double f(double x) {
return (x*x*x - 3.0*x);
}
double ternar(double a, double b, double eps) {
double l, r, mr, ml;
r = b;
l = a;
while (fabs(r - l) > eps) {
ml = l + (r - l)/3.0;
mr = r - (r - l)/3.0;
if(f(ml) < f(mr))
l = ml;
else
r = mr;
}
return (l+r)/2.0;
}
int main () {
double a, b, eps;
puts("a b eps");
scanf ("%lf %lf %lf", &a, &b, &eps);
printf ("Ternar: %lf\n", ternar(a, b, eps));
}
任务是使用 Jacobi 方法找到一个近似解,但是程序显示为零并且不继续迭代,我不明白如何修复此错误。
#include <stdio.h>
double diff(int n, double x1[n], double x2[n]) {
double s = 0;
int i;
for( i = 0; i < n; i++) {
s += (x2[i]-x1[i]) * (x2[i]-x1[i]);
}
return s;
}
double jacobi(int n, double x1[n], double x2[n]) {
int i, j;
double s = 0, a[n][n], f[n];
for(i = 0; i < n; i++) {
for(j = 0; j < i-1; j++) {
s += x1[j]*a[i][j];
}
for(j = i+1; j < n; j++) {
s += x1[j]*a[i][j];
}
x2[i] = (f[i] - s) / a[i][i];
}
}
int main () {
int n, i, j, k;
double eps;
printf ("Add N:\n");
scanf ("%d", &n);
printf ("Add epsilon:\n");
scanf("%lf", &eps);
double a[n][n], f[n], x1[n], x2[n];
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
scanf("%lf", &a[i][j]);
}
}
for(i = 0; i < n; i++) {
scanf ("%lf", &f[i]);
}
for(i = 0; i < n; i++) {
x2[i] = 0;
}
while(diff(n, x1, x2) > eps*eps) {
for(i = 0; i < n; i++) {
x1[i] = x2[i];
}
jacobi(n, x1, x2);
}
for(i = 0; i < n; i++)
printf ("%lf", x2[i]);
}
需要计算具有给定精度的序列:x - x^3/3 + x^5/5 - x^7/7 + ... 程序只计算序列到第一项,误差是多少,如何加速该计划?
#include <stdio.h>
#include <stdlib.h>
int factorial(int n) {
if (n == 0)
return 1;
if (n > 0)
return factorial(n - 1) * n;
}
long long deg(int n, double k) {
if (n == 0)
return 1;
else
return deg((n - 1), k) * k;
}
int main () {
double eps;
scanf ("%lf", &eps);
double x, sum = 0.0, curr, prev;
int k = 1, i = 0;
scanf ("%lf", &x);
curr =(deg(i,1) * deg(k,x)) / (double)k;
prev = 0.0;
while (abs(curr - prev) > eps) {
if (i % 2 == 0)
sum += curr;
else
sum -= curr;
i++;
k += 2;
prev = curr;
curr = (deg(i,1) * deg(k,x)) / (double)k;
}
printf ("%lf\n", sum);
}
给出了几个正分数,用空格隔开。计算分数之和(将结果减去最大公约数)
该程序给出了分段错误。错误在哪里?
#include <stdio.h>
#include <stdlib.h>
int gcd (int a, int b) {
if (a == b) {
return a;
else if (a > b) {
int tmp;
a = b;
b = tmp;
}
return gcd(a, b - a);
}
int main () {
int a, b, sum = 0, pro = 1, n, *num = NULL, *den = NULL, j, tmp;
scanf ("%d\n", &n);
num = malloc(n * sizeof(int));
den = malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
scanf ("%d/%d\n", &num[i], &den[i]);
}
for (int i = 0; i < n; i++) {
pro *= den[i];
for ((j = 0 && j != i); j < n; j++)
tmp = num[i] * den[j];
sum += tmp;
}
if (gcd (sum, pro) != 1) {
sum %= gcd(sum, pro);
pro %= gcd(sum, pro);
}
printf ("%d/%d", sum, pro);
free(num);
free(den);
}