此代码将扩展因子作为输入并将输出作为hadamard矩阵。例如,如果扩展因子为4,则hadamard矩阵将为4x4。任何人都可以告诉我如何递归地写它。
$proxy_server = '60.12.11.39';
$proxy_port = '1080';
// Configure the connection.
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_PROXY,$proxy_server.':'. $proxy_port);
//curl_setopt($ch, CURLOPT_PROXYPORT,"1080");
curl_setopt($ch, CURLOPT_USERAGENT, Constants::WHATSAPP_USER_AGENT);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: text/json'));
// This makes CURL accept any peer!
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
// Close the connection.
if(curl_exec($ch) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
echo 'Operation completed without any errors';
}
curl_close($ch);
答案 0 :(得分:0)
首先,让我们简化你的算法并将其包装成一个函数:
void mhadam(int a[MAX][MAX], int m, int M)
{
int i, j;
while (m < M) {
for (i = 0; i < m; i++) {
for (j = 0; j < m; j++) {
a[i + m][j] = a[i][j];
a[i][j + m] = a[i][j];
a[i + m][j + m] = -a[i][j];
}
}
m = 2 * m;
}
}
这里的主要功能是while
循环。如果没有达到展开因子M
,您可以递归调用此函数,而不是多次遍历此循环:
void mhadam_rec(int a[MAX][MAX], int m, int M)
{
int i, j;
if (m < M) {
for (i = 0; i < m; i++) {
for (j = 0; j < m; j++) {
a[i + m][j] = a[i][j];
a[i][j + m] = a[i][j];
a[i + m][j + m] = -a[i][j];
}
}
mhadam_rec(a, 2*m, M);
}
}
这里,循环和递归函数非常相似。在我看来,递归方法不会在这里给你买任何东西。实际上,递归调用是函数中最后一个语句的这种递归称为尾递归,它们的一个属性是可以将它们优化为循环。
编辑:为了完整起见,这是一个计算16×16 Hadamard矩阵的小程序:
#include <stdio.h>
#define MAX 128
void mprint(int a[MAX][MAX], int m)
{
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < m; j++) {
printf("%4d", a[i][j]);
}
printf("\n");
}
printf("\n");
}
void mhadam_rec(int a[MAX][MAX], int m, int M)
{
int i, j;
if (m < M) {
for (i = 0; i < m; i++) {
for (j = 0; j < m; j++) {
a[i + m][j] = a[i][j];
a[i][j + m] = a[i][j];
a[i + m][j + m] = -a[i][j];
}
}
mhadam_rec(a, 2*m, M);
}
}
int main()
{
int a[MAX][MAX];
int m = 1;
int M = 16;
a[0][0] = 1;
mhadam_rec(a, m, M);
mprint(a, M);
return 0;
}