西工大noj做题

代码仅供参考,肯定有算法、写法方面不足之处,欢迎指出

第一季

1.Hello World

#include<stdio.h>
int main(){
    printf("Hello World\n");
    return 0;
}

 

2.A+B

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a,b;
    scanf ("%d%d",&a,&b);
    printf("%d",a + b);
}

3.数据类型大小及范围

#include<stdio.h>
int main(){
    int n;
    scanf ("%d",&n);
    if (n == 1)
        printf("1,-128,127\n");
    if (n == 2)
        printf("1,0,255\n");
    if (n == 3)
        printf("2,-32768,32767\n");
    if (n == 4)
        printf("2,0,65535\n");
    if (n == 5)
        printf("4,-2147483648,2147483647\n");
    if (n == 6)
        printf("4,0,4294967295\n");
    if (n == 7)
        printf("8,-9223372036854775808,9223372036854775807\n");
    if (n == 8)
        printf("8,0,18446744073709551615\n");
    if (n == 9)
        printf("8,-9223372036854775808,9223372036854775807\n");
    if (n == 10)
        printf("8,0,18446744073709551615\n");
}

4.平均值

#include <stdio.h>
int main()
{
    long long a, b;
    long long c;
    scanf("%lld %lld", &a, &b);
    c = (a + b) / 2;
    printf("%lld", c);
}

5.进制转换

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
int n;
int main(){
    scanf ("%d",&n);
    printf("%X,%o",n,n);
}

6.浮点数输出

#include <stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    double n;
    scanf("%lf", &n);
    printf("%.6lf,%.2lf,%.8lf", n, n, n);
}

7.动态宽度输出

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
char a[105];
int n;
int main()
{
    scanf("%s", a);
    scanf("%d", &n);
    int l = strlen(a);
    for (int i = 1; i <= n - l;i ++)
        printf("0");
    puts(a);
}

8.计算地球上两点间距离

#include <stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
double hav (double x){
    double s = sin(x / 2);
    return s * s;
}
int main()
{
    double a1, b1, a2, b2;
    scanf("%lf%lf%lf%lf", &a1, &b1, &a2, &b2);
    a1 = a1 / 180 * acos(-1.0);
    a2 = a2 / 180 * acos(-1.0);
    b1 = b1 / 180 * acos(-1.0);
    b2 = b2 / 180 * acos(-1.0);
    double t1 = hav(a1 - a2) + cos(a1) * cos(a2) * hav(b1 - b2);
    printf("%.4lfkm",asin(sqrt(t1)) * 2 * 6371.0);
}
/*
34.260958 108.942369
55.755825 37.617298
*/

9.风寒指数

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
int main(){
    double a, b;
    scanf("%lf%lf", &a, &b);
    printf("%.0lf", 13.12 + 0.6215 * b - 11.37 * pow(a, 0.16) + 0.3965 * b * pow(a, 0.16));
}

10.颜色模型转换

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define max(a, b) a > b ? a : b
#define min(a, b) a > b ? b : a
int main()
{
    double r, g, b;
    double maxn, minn, h, s, v;
    scanf("%lf%lf%lf", &r, &g, &b);
    r /= 255.0, g /= 255.0, b /= 255.0;
    maxn = max(r, g);
    maxn = max(maxn, b);
    minn = min(r, g);
    minn = min(minn, b);
    v = maxn;
    if (v == 0)
        s = 0;
    else
    s = (maxn - minn) / maxn;
    if (maxn == r)
        h = 60 * (0 + (g - b) / (maxn - minn));
    if (maxn == g)
        h = 60 * (2 + (b - r) / (maxn - minn));
    if (maxn == b)
        h = 60 * (4 + (r - g) / (maxn - minn));
    if (h < 0)
        h += 360;
    printf("%.4lf,", h);
    printf("%.4lf", s * 100);
    putchar('%');
    putchar(',');
    printf("%.4lf", v * 100);
    putchar('%');
}

 第二季

1.方阵

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
int n;
int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
            printf("%-3d", abs(j - i));
        printf("\n");
    }
}

2.组合数

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
int n,ans;
int main(){
	scanf ("%d",&n);
	for (int i = 0;i <= 9;i ++)
		for (int j = 0;j <= 9;j ++)
			for (int k = 0;k <= 9;k ++)
				if ((n - i - j - k) >= 0 && (n - i - j - k) <= 9)
					ans ++;
	printf("%d",ans);
}

3.幂数模

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
long long a, b, m, ans;
long long qkpow(long long x,long long y){
    if (y == 0)
        return 1;
    long long t = qkpow(x,y / 2) % m;
    if (y % 2)
        return t * t % m * x % m;
    else 
        return t * t % m;
}
int main()
{
    scanf("%lld%lld%lld", &a, &b, &m);
    ans = 1;
    while (b)
    {
        if (b % 2 == 1)
            ans = ans * a % m;
        b /= 2;
        a = a * a % m;
    }
    printf("%lld", ans);
}

4.倍数和

不要开longlong,暴力求解,不要用等差数列

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
int t, n;
int ans;
int ind;
int main()
{
    scanf("%d", &t);
    while (t--)
    {
        ans = 0;
        scanf("%d", &n);
        for (int i = 3;i < n;i += 3)
            ans += i;
        for (int i = 5;i < n;i += 5)
            ans += i;
        for (int i = 15;i < n;i += 15)
            ans -= i;
        printf("%d\n",ans);
    }
}

5.乘数模

数据水,本应用高精

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
long long t, n;
long long read()
{
    long long s = 0;
    int f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
    {
        s = s * 10 + c - '0';
        c = getchar();
    }
    return s * f;
}
int main()
{
    long long a,b,m;
    scanf ("%lld%lld%lld",&a,&b,&m);
    printf("%lld",(a % m) * (b % m) % m);
}

6.级数和

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
int n;
double ans;
int main()
{
    scanf ("%d",&n);
    for (int i = 1;i <= n;i ++){
        if (i % 10 == 9)
            printf("%d.%d",i,(i+1) / 10);
        else
            printf("%d.%d",i,i + 1);
        ans += i;
        if (i < 9)
            ans = ans + (i + 1) / 10.0;
        else if (i < 99)
            ans = ans + (i + 1) / 100.0;
        else
            ans = ans + (i + 1) / 1000.0;
        if (i != n)
            printf("+");
    }
    printf("=%.2lf",ans);
}

7.比率

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
int n,a,ind,flag;
char s[10005];
int gcd(int x,int y){
    int t;
    while (y){
        t = x % y;
        x = y;
        y = t;
    }
    return x;
}
int getpow(int x,int y){
    int ans = 1;
    while(y --)
        ans = x * ans;
    return ans;
}
int main()
{
    scanf("%s",s);
    int l = strlen(s);
    for (int i = 0;i < l;i ++){
        if (s[i] == '.'){
            flag = 1;
            continue;
        }
        if (s[i] < '0' || s[i] > '9')
            continue;
        a = a * 10 + s[i] - '0';
        if (flag)
            ind ++;
    }
    int c = getpow(10,ind);
    int b = gcd(a,c);
    printf("%d/%d",a / b,c / b);
}

8.分数的加减乘除法

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
int n, a1, a2, b1, b2, ind, flag = 1;
char s[10005];
int gcd(int x, int y)
{
    int t;
    while (y)
    {
        t = x % y;
        x = y;
        y = t;
    }
    return x;
}
int getpow(int x, int y)
{
    int ans = 1;
    while (y--)
        ans = x * ans;
    return ans;
}
int main()
{
    scanf("%d/%d\n%d/%d", &a1, &a2, &b1, &b2);
    int ans1 = a1 * b2 + a2 * b1;
    int ans2 = a2 * b2;
    n = gcd(ans1, ans2);
    printf("(%d/%d)+(%d/%d)=%d/%d\n", a1, a2, b1, b2, ans1 / n, ans2 / n);
    ans1 = (a1 * b2 - a2 * b1);
    if(ans1 < 0)
        ans1 = abs(ans1),flag = -1;
    n = gcd(ans1,ans2);
    printf("(%d/%d)-(%d/%d)=%d/%d\n", a1, a2, b1, b2, ans1 * flag / n, ans2 / n);
    ans1 = a1 * b1,ans2 = a2 * b2;
    n = gcd(ans1,ans2);
    printf("(%d/%d)*(%d/%d)=%d/%d\n", a1, a2, b1, b2, ans1 / n, ans2 / n);
    ans1 = a1 * b2,ans2 = a2 * b1;
    n = gcd(ans1,ans2);
    printf("(%d/%d)/(%d/%d)=%d/%d\n", a1, a2, b1, b2, ans1 / n, ans2 / n);
}

9.操作数

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
int k;
int main()
{
    int n;
    scanf("%d",&n);
    while (n > 0){
        int a = 0,t = n;
        while (t){
            a += t % 10;
            t /= 10;
        }
        n -= a;
        k ++;
    }
    printf("%d",k);
}

10.对称数

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
int k,flag;
char s[10005];
int main()
{
    scanf ("%s",s);
    int l = strlen(s);
    for (int i = 0;i < l / 2;i ++){
        if (s[i] == '3' || s[i] == '4' || s[i] == '7'){
            flag = 1;
            break;
        }
        if (s[i] == '0' && s[l - i + 1] != '0'){
            flag = 1;
            break;
        }
        if (s[i] == '1' && s[l - i - 1] != '1'){
            flag = 1;
            break;
        }
        if (s[i] == '2' && s[l - i - 1] != '2'){
            flag = 1;
            break;
        }
        if (s[i] == '5' && s[l - i - 1] != '5'){
            flag = 1;
            break;
        }
        if (s[i] == '6' && s[l - i - 1] != '9'){
            flag = 1;
            break;
        }
        if (s[i] == '8' && s[l - i - 1] != '8')
        {
            flag = 1;
            break;
        }
        if (s[i] == '9' && s[l - i - 1] != '6')
        {
            flag = 1;
            break;
        }
    }
    if (flag)
        printf("No\n");
    else 
        printf("Yes\n");
}

第三季

1.余数和

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
int n,k;
long long ans;
int main(){
    scanf ("%d%d",&n,&k);
    for (int i = 1;i <= n;i ++)
        ans = ans + k % i;
    printf("%lld",ans);
}

2.好数字

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#define M 1000000007
long long n,n1,n2;
long long qkpow(long long a,long long p){
    long long ans = 1,x = a;
    while (p){
        if (p % 2)
            ans = ans * x % M;
        x = x * x % M;
        p /= 2;
    }
    return ans;
}
int main(){
    scanf ("%lld",&n);
    n2 = n / 2;
    n1 = n % 2 ? (n2 + 1) : n2;  
    printf("%lld",qkpow(5,n1) * qkpow(4,n2) % M);
}

3.方案数

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
int n,ans;
int main(){
    scanf ("%d",&n);
    for (int i = 1;i <= n;i ++){
        if (i % 2)
            if (n % i == 0)
                ans ++;
        else if (n % i == i / 2)
            ans ++;
    }
    printf("%d",ans);
}

4.查找数列

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
int n;
long long s;
int main(){
    scanf("%d",&n);
    for (long long i = 1;i <= n;i ++){
        long long s1 = i * (i + 1) / 2;
        if (s1 > n){
            printf("%d",n - s);
            return 0;    
        }
        s = s1;    
    }
}

5.倒水

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#define min(a,b) a>b?b:a
int n,m,d,ans = 2147483647;
int flag[1005][1005];
void dfs(int x,int y,int step){
    if ((flag[x][y] <= step && step != 0) || step > ans)
        return ;
    flag[x][y] = step;
    if(x == d || y == d){
        ans = min(ans,step);
        return ;
    }
    if (x != m)//倒满杯1
        dfs(m,y,step + 1);
    if (y != n)//倒满杯二
        dfs(x,n,step + 1);
    if (x)//倒1
        dfs(0,y,step + 1);
    if (y)//倒2
        dfs(x,0,step + 1);
    if (x && y != n){//1倒2
        int t = n - y;
        if (x <= t)
            dfs(0,y + x,step + 1);
        else 
            dfs(x - t,n,step + 1);
    }
    if (y && x != m){//2倒1
        int t = m - x;
        if (y <= t)
            dfs(x + y,0,step + 1);
        else 
            dfs(m,y - t,step + 1);
    }
    return ;
} 
int main(){
    scanf ("%d%d%d",&m,&n,&d);
    memset(flag,10000,sizeof(flag));
    dfs(0,0,0);
    printf("%d\n",ans);
}

6.阶乘倍数

#include <stdio.h>
#include <math.h>
int len,w[1005];
long long p[1005],ans;
#define max(a,b) a<b?b:a
int main()
{
    long long k;
    long long n = 1;
    scanf("%lld", &k);
    //1.整数的唯一分解
    for (long long i = 2;i <= k / i;i ++){
        if (k % i == 0){
            p[++ len] = i;
            while (k % i == 0)
                k /= i,w[len] ++;
        }
    }
    if (k != 1)
        p[++ len] = k,w[len] ++;
    //2.寻找n
    for (int i = 1;i <= len;i ++){
        int num = 0;
        for (int j = 1;j <= 63;j ++){//long long 2^64 - 1
            num ++;
            int t = j;
            while (t >= p[i] && t % p[i] == 0){
                num ++;
                t /= p[i];
            }
            if (num >= w[i]){
                ans = max(ans,p[i] * j);
                break;
            }
        }
    }
    printf("%lld",ans);
}
//15645152615

7.最大数字

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
int n;
int check(int x){
    int t = 10;
    while (x){
        x /= 10;
        int u = x % 10;
        if (u > t)
            return 0;
        t = u;
    }
    return 1;
}
int main(){
    scanf ("%d",&n);
    while (1){
        int flag = check(n);
        if (flag){
            printf("%d",n);
            return 0;
        }
        n --;
    }
}

8.竖式乘法

应该有简洁一点的代码

 

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
long long a,b,ans;
int len,len_a,len_b;
int getlen(int x){
    int t = 0;
    if (x == 0)
        return 1;
    while (x){
        x /= 10;
        t ++; 
    }
    return t;
}
int main(){
    scanf("%lld%lld",&a,&b);
    ans = a * b;
    len = getlen(ans);
    len ++;
    len_a = getlen(a);
    len_b = getlen(b);
    int t = len;
    for (int i = 1;i <= len;i ++){
        if(t > len_a){
            putchar(' ');
            t --;
            continue;
        }
        long long x = a / (long long)pow(10,t - 1);
        printf("%lld",x % 10);
        t --;
    }
    putchar('\n'),putchar('x');
    t = len - 1;
    for (int i = 1; i < len; i++)
    {
        if (t > len_b)
        {
            putchar(' ');
            t--;
            continue;
        }
        long long x = b / (long long)pow(10, t - 1);
        printf("%lld", x % 10);
        t --;
    }
    putchar('\n');
    for (int i = 1;i <= len;i ++)
        putchar('-');
    putchar('\n');
    for (int i = 0;i < len_b;i ++){
        int x = b % 10;
        b /= 10;
        long long put_x = x * a;
        int putlen = getlen(put_x);
        int putlen1 = putlen + i;
        if (i == (len_b - 1)){
            putchar('+');
            for (int j = len - 1; j >= 1; j--)
            {
                if (j > putlen1)
                {
                    putchar(' ');
                    continue;
                }
                else if (j <= putlen1 && j > i)
                {
                    long long chushu = (long long)pow(10, putlen - 1 - (putlen1 - j));
                    printf("%lld", (put_x / chushu) % 10);
                }
                else
                    putchar(' ');
            }
        }
        else {
            for (int j = len;j >= 1;j --){
                if (j > putlen1){
                    putchar(' ');
                    continue;
                }
                else if (j <= putlen1 && j > i){
                    long long chushu = (long long) pow(10,putlen - 1 - (putlen1 - j));
                    printf("%lld",(put_x / chushu) % 10);
                }
                else 
                    putchar(' ');
            }
        }
        putchar('\n');
    }
    for (int i = 1; i <= len; i++)
        putchar('-');
    putchar('\n');
    for (int i = len; i >= 1; i--){
        if (i == len)
            putchar(' ');
        else{
            long long chushu = (long long)pow(10, i - 1);
            printf("%lld", (ans / chushu) % 10);
        }
    }
}

9.俄罗斯农夫乘法

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
int n,a,b,ans;
int main(){
    scanf ("%d%d",&a,&b);
    while (a){
        printf("%d %d\n",a,b);
        if (a % 2)
            ans += b;
        a /= 2;
        b *= 2;
    }
    printf("%d\n",ans);
}

10.毕达哥斯拉三元组

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
int n;
int main(){
    scanf ("%d",&n);
    for (int a = 1;a <= n / 2;a ++)
        for (int b = a + 1;b <= n / 2;b ++){
            int c = n - b - a;
            if ((c < b) || (c < a))
                break;
            if (c * c == (b * b + a * a)){
                printf("%d",a * b * c);
                return 0;
            }
        }
    /*for (long long a = 1;a <= n;a ++)
        for (long long b = a + 1;b <= n;b ++)
            for (long long c = b + 1;c <= n;c ++)
                if ((c * c == a * a + b * b) &&(a + b + c == n)){
                    printf("%lld",a * b * c);
                    return 0;
                }*/
}

第四季

1.可变参数平均

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<stdarg.h>
double a,b,c,d,e;
double avg(int n, ...){
    va_list s;
    va_start(s,n);
    double ans = 0;
    for (int i = 1;i <= n ;i ++)
        ans += va_arg(s,double);
    va_end(s);
    return ans / n;
}
int main(){
    scanf ("%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e);
    printf("%.4lf\n",avg(2,a,b) - avg(3,c,d,e));
    //printf("%.4lf",(a + b) / 2 - (c + d + e) / 3);
}

2.冰雹数列

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
int n;
void solve(int x){
    printf("%d ",x);
    while (x != 1){
        if (x % 2 == 0)
            x /= 2;
        else
            x = x * 3 + 1;
        if (x == 1)
            printf("%d",x);
        else
            printf("%d ",x);
    }

}
int main(){
    scanf("%d",&n);
    solve(n);
}

3.光线追踪

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
long long n,X,ans;
int main(){
    scanf ("%d%d",&n,&X);
    long long a = X,b = n - X;
    ans += n;
    while (a != b){
        if (a < b){
            long long t = a;
            a = b;
            b = t;
        }
        ans += b * 2;
        a -= b;
    }
    ans += b;
    printf("%lld",ans);
}

4.基思数

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
int n;
long long s;
int main(){
    scanf("%d",&n);
    for (long long i = 1;i <= n;i ++){
        long long s1 = i * (i + 1) / 2;
        if (s1 > n){
            printf("%d",n - s);
            return 0;    
        }
        s = s1;    
    }
}

5.可变参数累加

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<stdarg.h>
int a,b,c,d,e,f;
int add(int first,...){
    va_list s;
    va_start(s,first);
    int sum = first;
    int x = va_arg(s,int);
    while (x > 0){
        sum += x;
        x = va_arg(s,int);
    }
    va_end(s);
    return sum;
}
int main(){
    scanf ("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f);
    printf("%d",add(a,b,0) - add(c,d,e,f,0));
}

6.哈沙德数

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<stdarg.h>
int n,ans;
int HarshadNumber(int x){
    int t = x,s = 0;
    while (t){
        s += t % 10;
        t /= 10;
    }
    if (s && x % s == 0)
        return x / s;
    return 0;
}
int main(){
    scanf ("%d",&n);
    while (n){
        if (n <= 10){
            ans ++;
            break;
        }
        int next = HarshadNumber(n);
        if (!next)
            break;
        ans ++;
        n = next;
    }
    printf("%d",ans);
}

7.素数

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<stdarg.h>
int vis[10000005];
int sieve(int b){
    for (int i = 1;i <= 10000000;i ++)
        vis[i] = 0;
    int sum = 0;
    for (long long i = 2;i <= b;i ++){
        if (!vis[i]){
            ++ sum;
            for (long long j = i * i;j <= b;j += i)
                vis[j] = 1;
        }
    }
    return sum;
}
int main(){
    int a,b;
    scanf ("%d%d",&a,&b);
    printf("%d",sieve(b) - sieve(a));
}

8.二进制表示

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<stdarg.h>
int n;
void solve(int x){
    if (x == 0){
        printf("0");
        return ;
    }
    int a[105],len = 0,start = 2147483647;
    while (x){
        a[++ len] = x % 2;
        if (start == 2147483647 && x % 2)
            start = len;
        x /= 2;
    }
    for (int i = len; i >= start;i --){
        if (a[i] == 0)
            continue;
        if (i == 2){
            if (i == start)
                printf("2");
            else
                printf("2+");
            continue;
        }
        else {
            printf("2(");
            solve(i - 1);
        }
        if (i != start)
            printf(")+");
        else 
            printf(")");
    }
    return ;
}
int main(){
    scanf ("%d",&n);
    solve(n);
}

9.运动会

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<stdarg.h>
int ans;
int phi(int n){
    int res = n;
    for (int i = 2;i * i <= n;i ++){
        if (n % i == 0){
            res = res / i * (i - 1);
            while (n % i == 0)
                n /= i;
        }
    }
    if (n > 1)
        res = res / n * (n - 1);
    return res;
}
int main(){
    int n;
    scanf ("%d",&n);  
    if (n == 1){
        printf("0\n");
        return 0;
    }
    for (int i = 1;i < n;i ++){
        ans = ans + phi(i) * 2;
    }
    printf("%d\n",ans + 1);
}

10.佩尔数

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<stdarg.h>
int pa(int x){
    if (x == 0)
        return 0;
    if (x == 1)
        return 1;
    return 2 * pa(x - 1) + pa(x - 2);
}
int pb(int x){
    int p0 = 0,p1 = 1,pn;
    for (int i = 0;i <= x;i ++){
        if(i == 0)
            pn = p0;
        else if (i == 1)
            pn = p1;
        else {
            pn = 2 * p1 + p0;
            p0 = p1;
            p1 = pn;
        }
    }
    return pn;
}
int main(){
    int n;
    scanf ("%d",&n);
    if (n % 2)
        printf("%d",pa(n));
    else 
        printf("%d",pb(n));
}

 

posted @ 2023-10-07 20:57  Kotiya_Sanae  阅读(623)  评论(0)    收藏  举报