将以下代码转换成C语言% Matlab function [Lambda_S,N,f] = linear_attack() S = [2 6 8 7 1 4 9 5 3 0]; for u = 0:9 N = Lambda_0(S,u); f = fabu(u); Lambda_S = N - f; end function N = Lambda_0(S,u) for i = 1:10 % a for j = 1:10 % b n = 0; for x = 1:10 % x if mod((i-1)*(x-1) -(j-1)*S(x),10) == u n = n + 1; end end N(i,j) = n; end end function f = fabu(u) for i = 1:10 % a for j = 1:10 % b n = 0; for x = 1:10 % x for y = 1:10 % y if mod((i-1)*(x-1) - (j-1)*(y-1),10) == u n = n + 1; end end end N(i,j) = n / 10; end end f = N;
时间: 2023-06-16 15:05:46 浏览: 207
以下是代码的C语言版本:
#include <stdio.h>
void Lambda_0(int S[], int u, int N[][10]) {
int i, j, x;
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
int n = 0;
for (x = 0; x < 10; x++) {
if (((i * x) - (j * S[x])) % 10 == u) {
n++;
}
}
N[i][j] = n;
}
}
}
void fabu(int u, double f[][10]) {
int i, j, x, y;
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
double n = 0.0;
for (x = 0; x < 10; x++) {
for (y = 0; y < 10; y++) {
if (((i * x) - (j * y)) % 10 == u) {
n++;
}
}
}
f[i][j] = n / 100.0;
}
}
}
void linear_attack(int S[]) {
int u;
for (u = 0; u < 10; u++) {
int N[10][10];
Lambda_0(S, u, N);
double f[10][10];
fabu(u, f);
int i, j;
printf("Lambda_S for u = %d:\n", u);
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
printf("%d ", N[i][j] - (int) (f[i][j] * 100));
}
printf("\n");
}
printf("\n");
}
}
int main() {
int S[] = {2, 6, 8, 7, 1, 4, 9, 5, 3, 0};
linear_attack(S);
return 0;
}
其中,Lambda_0函数计算N数组的值,fabu函数计算f数组的值,linear_attack函数调用Lambda_0和fabu函数,并输出Lambda_S。注意,为了保持和Matlab版本一致,f数组中的元素应为double类型,输出Lambda_S时需要将其转换为整型。
阅读全文