#include<stdio.h> const unsigned long long MOD=1844674407370955161; unsigned long long a[2][2]={{1,1},{1,0}}; unsigned long long b[2][2]={{1,1},{1,0}}; unsigned long long c[2][2]={{1,0},{0,1}}; unsigned long long FB (unsigned long long c[2][2],unsigned long long a[2][2]){ unsigned long long temp[2][2]={0}; for(int i=0;i<2;i++){ for(int j=0;j<2;j++){ temp[i][j]=0; for(int k=0;k<2;k++){ temp[i][j]+=c[i][k]*a[k][j]; } } } for(int i=0;i<2;i++){ for(int j=0;j<2;j++){ c[i][j]=temp[i][j]; } } return 0; } unsigned long long account(unsigned long long n){ if(n==1){ FB(a,c); } else{ if(n%2==0){ account(n/2); FB(a,a); } if(n%2==1){ account((n-1)/2); FB(a,a); FB(a,b); } } return 0; } int main(){ unsigned long long n; scanf("%lld",&n); account(n); printf("%lld",a[0][1]%MOD); return 0; }
时间: 2024-03-30 07:37:09 浏览: 19
这是一个斐波那契数列求解的代码,使用了矩阵乘法的方式进行求解,时间复杂度为O(log n)。其中,函数account(n)计算斐波那契数列的第n项,函数FB(c,a)实现了矩阵乘法,最终的结果通过a[0][1]输出。需要注意的是,由于斐波那契数列的值非常大,使用unsigned long long和取模运算避免了计算结果溢出或超出计算机所能表示的范围。
相关问题
#include <stdio.h> #include <stdlib.h> #include <string.h> unsigned int GetIpv4(const char *ipStr) { unsigned int ip = 0; unsigned int octet; int shift = 0; printf("asfasfa\n"); const char * token = strtok((char *)ipStr, "."); while (token != NULL && shift <= 24) { octet = atoi(token); ip |= (octet >> shift); shift += 8; token = strtok(NULL, "."); } return ip; } int main() { const char *MyIp = "192.168.28.251"; unsigned int IP = GetIpv4(MyIp); printf("IP = 0x%08x\n", IP); return 0; } 上述程序中,const char * token = strtok((char *)ipStr, ".");此条语句报错
这个错误可能是因为 `strtok` 函数的参数是一个 `char *` 类型,而 `ipStr` 是一个 `const char *` 类型。由于 `strtok` 函数会修改传入的字符串,所以需要将参数的类型从 `const char *` 转换为 `char *`。
你可以通过将参数类型从 `const char *` 转换为 `char *` 来解决这个问题,如下所示:
```c
char ip[16];
strcpy(ip, ipStr);
const char *token = strtok(ip, ".");
```
这样,我们首先将 `ipStr` 复制到一个新的字符数组 `ip` 中,并将其传递给 `strtok` 函数。这样就可以避免传递 `const` 类型参数给 `strtok` 函数而引发错误。
修改后的代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
unsigned int GetIpv4(const char *ipStr)
{
unsigned int ip = 0;
unsigned int octet;
int shift = 0;
printf("asfasfa\n");
char ip[16];
strcpy(ip, ipStr);
const char *token = strtok(ip, ".");
while (token != NULL && shift <= 24) {
octet = atoi(token);
ip |= (octet >> shift);
shift += 8;
token = strtok(NULL, ".");
}
return ip;
}
int main() {
const char *MyIp = "192.168.28.251";
unsigned int IP = GetIpv4(MyIp);
printf("IP = 0x%08x\n", IP);
return 0;
}
```
这样修改后的代码应该能够消除这个错误。
当你输入很长的字符串时,会出现缓冲区溢出,在把name覆盖掉的同时,并且会更改gold的值为你的字符串后四位,请修改以下代码来实现:// savegame.c // Compile using gcc -fno-stack-protector savegame.c #include <stdio.h> #include <stdlib.h> #include <string.h> struct hero { unsigned int hp; char name[10]; unsigned int gold; }; void secret_function() { printf("You made it!\n"); return; } struct hero setup_hero() { struct hero h; h.hp = 30; h.gold = 8; scanf("%s", h.name); return h; } int main(int argc, const char *argv[]) { struct hero h = setup_hero(); printf("Name: %s\n", h.name); printf("HP: %d | Gold: %d\n", h.hp, h.gold); return 0; }
为了避免缓冲区溢出,可以使用 fgets() 函数代替 scanf() 函数读取输入。此外,可以使用 strncpy() 函数来复制字符串,以确保不会溢出。修改后的代码如下:
```
// savegame.c
// Compile using gcc -fno-stack-protector savegame.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct hero {
unsigned int hp;
char name[10];
unsigned int gold;
};
void secret_function() {
printf("You made it!\n");
return;
}
struct hero setup_hero() {
struct hero h;
h.hp = 30;
h.gold = 8;
fgets(h.name, 10, stdin);
h.name[strcspn(h.name, "\n")] = '\0'; // Remove newline character from input
strncpy(&h.gold, &h.name[strlen(h.name) - 4], 4); // Copy last 4 characters of input into gold
return h;
}
int main(int argc, const char *argv[]) {
struct hero h = setup_hero();
printf("Name: %s\n", h.name);
printf("HP: %d | Gold: %d\n", h.hp, h.gold);
return 0;
}
```
在新的代码中,我们使用 fgets() 函数读取输入,并且使用 strcspn() 函数来删除输入中的换行符。然后,我们使用 strncpy() 函数将输入的最后四个字符复制到 gold 字段中。这样,即使输入的字符串很长,也不会溢出缓冲区。
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)