ACM - 算法篇,基础题目

5-1-1 左移键盘

c#include <stdio.h>
#include <string.h>
#include <assert.h>

char *s = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";

int main()
{
    char c;
    int i;

#ifdef DEBUG
    freopen("input", "r", stdin);
#endif

    while((c = getchar()) != EOF)
    {
        for(i = 1; s[i] && s[i]!=c; i++);
        if (s[i]) putchar(s[i-1]);
        else putchar(s[i]);
        if(c == ' ')
            putchar(' '); 
    }
    return 0;
}

5-1-2 Tex括号

c#include <stdio.h>
#include <string.h>
#include <assert.h>


int main()
{
    char c;
    int is = 1;

    freopen("input", "r", stdin);

    while((c = getchar()) != EOF)
    {
        if(c == '"')
        {
            if(is)
            {
                is = 0;
                printf("``");
            }
            else
            {
                is = 1;
                printf("''");
            }

        }
        else
            putchar(c);
    }
    return 0;
}

5-1-3 周期串

c#include <stdio.h>
#include <string.h>


int main()
{
    int i, j;
    int len;
    int temp;
    char str[100];

    freopen("input", "r", stdin);

    while(~scanf("%s",str))
    {
        len = strlen(str);
        for(i = 1; i <= len; i++)
        {
            temp = i;
            // 判断整除
            if(!len % temp)
                continue;

            for(j = temp; j < len; j++)
            {
                if(str[j] != str[j % temp])
                    break;
            }
            if(j != len)
                continue;
            else
            {
                printf("%d\n", temp);
                break;
            }
        }
    }
    return 0;
}

5.2 高精度

5.2.1 小学生算数

ace()是模拟手算加法

c#include <stdio.h>
#include <string.h>

void ace()
{
    char n1[20];
    char n2[20];
    int count, incre;
    int i, j;
    freopen("input", "r", stdin);
    while(scanf("%s %s", n1, n2))
    {
        incre = 0, count = 0;
        if(n1[0] == '0' && n2[0] == '0')
            break;

        int len1 = strlen(n1);
        int len2 = strlen(n2);
        int t1, t2;

        i = len1 -1;
        j = len2 -1;
        while(1)
        {
            t1 = n1[i] - '0';
            t2 = n2[j] - '0';
            if(t1 + t2 + incre >= 10)
            {
                incre = 1;
                count ++;
            }
            else incre = 0;

            i--, j--;
            if(i == -1 || j == -1)
            {
                if(i == -1 && j == -1)
                    break;
                else if(i == -1)
                    t1 = n2[j] - '0';
                else
                    t1 = n1[i] - '0';
                t1 + incre >= 10 ? count++ : 0;
                break;
            }
        }

        printf("%d\n", count);
    }
}

void ace2()
{
    int a, b;
    int count;
    int i, inc;
    freopen("input", "r", stdin);

    while(~scanf("%d %d", &a, &b))
    {
        if(a == 0 && b == 0)
            break;
        inc = count = 0;
        for(i = 9; i >= 0; i--)
        {
            count += inc = (a%10) + (b%10) + inc >= 10 ? 1 : 0;
            a /= 10, b /= 10;
        }
        printf("%d\n", count);
    }
}

int main()
{
    ace2();
    return 0;
}

5-2-2 阶乘精确值

包含模拟手算大数

c#include <stdio.h>
#include <string.h>
#include <assert.h>

const int maxn = 3000;

/**
 * 模拟手算乘法
 * 手算乘法的时候,不使用字符
 */
void mul(int *f, int n)
{
    int i, s, incre;
    incre = 0;
    for(i = 0; i < maxn; i++)
    {
        s = f[i] * n + incre;
        f[i] = s % 10;
        incre = s / 10;
    }
}

void ace()
{
    int n, i;
    int f[maxn];

    freopen("input", "r", stdin);

    while(~scanf("%d", &n))
    {
        memset(f, 0, sizeof(f));
        f[0] = 1;
        for(i = 1; i <= n; i++)
        {
            mul(f, i);
        }

        // 寻找最大
        for(i = maxn - 1; i >= 0; i--) if(f[i]) break;
        while(i >= 0)
        {
            printf("%d", f[i]);
            i--;
        }
        printf("\n");
    }
}

int main()
{
    ace();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值