码力恢复——51NOD基础篇

1000 A + B

#include<bits/stdc++.h>
using namespace std;

#define clr(a) memset(a, 0, sizeof(a))
#define line cout << "--------" << endl


typedef long long ll;
const int maxn = 1e5 + 10;

int main(){
    ll a, b;
    cin >> a >> b;
    cout << a + b << endl;

    return 0;
}

1005 大数加法

Java 调用BigInteger函数

//package text;

import java.util.*;
import java.math.*;
import java.io.*;
 
public class Main {
    public static void main(String[] args) {
    	Scanner cin = new Scanner(System.in);
    	BigInteger a = cin.nextBigInteger();
    	BigInteger b = cin.nextBigInteger();
    	BigInteger c = a.add(b);
    	System.out.println(c);
    }
}

C++竖式大模拟:

#include<bits/stdc++.h>
using namespace std;

#define clr(a) memset(a, 0, sizeof(a))
#define line cout << "--------" << endl


typedef long long ll;
const int maxn = 1e5 + 10;


char a[maxn], b[maxn], ta[maxn], tb[maxn];
int na[maxn], nb[maxn], ans[maxn], cnt = 0;
bool flaga = true, flagb = true;

int cmp(char sa[], char sb[]){
    int lsa = strlen(sa);
    int lsb = strlen(sb);
    if(strcmp(sa, sb) == 0) return 0;
    if(lsa > lsb) return 1;
    if(lsa < lsb) return -1;
    if(lsa == lsb){
        for(int i = lsa; i >= 0; i--){
            if(sa[i] > sb[i]) return 1;
            else if(sa[i] < sb[i]) return -1;
            else continue;
        }
    }
}

int main(){

    gets(a);
    gets(b);
    int la = strlen(a);
    int lb = strlen(b);
    int tt = 0;

    if(a[0] == '-'){
        tt ++;
        flaga = false;
        for(int i = 1; i < la; i++)
            ta[i-1] = a[i];
        la --;
    }
    else{
        for(int i = 0; i < la; i++)
            ta[i] = a[i];
    }
    reverse(ta, ta+la);

    if(b[0] == '-'){
        tt ++;
        flagb = false;
        for(int i = 1; i < lb; i++)
            tb[i-1] = b[i];
        lb --;
    }
    else {
        for(int i = 0; i < lb; i++)
            tb[i] = b[i];
    }
    reverse(tb, tb+lb);

    for(int i = 0; i < la; i++)
        na[i] = ta[i] - '0';
    for(int i = 0; i < lb; i++)
        nb[i] = tb[i] - '0';
    if(tt % 2 == 0){
        int len = max(la, lb);
        int temp = 0;
        for(int i = 0; i < len; i++){
            int x = na[i] + nb[i] + temp;
            if(x > 9){
                ans[cnt++] = x % 10;
                temp = 1;
            }
            else{
                ans[cnt++] = x;
                temp = 0;
            }
        }
        if(temp) ans[cnt++] = 1;
        reverse(ans, ans+cnt);
        //line;
        if(!flaga) cout << "-";
        for(int i = 0; i < cnt; i++){
            cout << ans[i];
        }
        cout << endl;
    }
    else{
        if(flaga){
            int x = cmp(ta, tb);
            if(x == 0){
                cnt = 1;
                ans[0] = 0;
            }
            else if(x > 0){
                int temp = 0;
                for(int i = 0; i < la; i++){
                    int k = na[i];
                    //printf("%d - %d - %d = ", k, nb[i], temp);
                    if(temp) k--;
                    k -= nb[i];
                    if(k < 0){
                        temp = 1;
                        k += 10;
                    }
                    else temp = 0;
                    ans[cnt++] = k;
                    //printf("%d!!\n", k);
                }
                /*
                for(int i = cnt-1; i >= 0; i--){
                    printf("%d ", ans[i]);
                }line;
                */
                int pos = cnt-1;
                for(int i = cnt-1; i >= 0; i--){
                    if(ans[i] == 0) pos--;
                    else break;
                }
                for(int i = pos; i >= 0; i--)
                    cout << ans[i];
                cout << endl;
                /*
                line;
                for(int i = 0; i < cnt; i++){
                    cout << ans[i] << endl;
                }
                */
            }
            else{
                int temp = 0;
                for(int i = 0; i < lb; i++){
                    int k = nb[i];
                    if(temp) k--;
                    k -= na[i];
                    if(k < 0){
                        temp = 1;
                        k += 10;
                    }
                    else temp = 0;
                    ans[cnt++] = k;
                }
                cout << "-";
                int pos = cnt-1;
                for(int i = cnt-1; i >= 0; i--){
                    if(ans[i] == 0) pos--;
                    else break;
                }
                for(int i = pos; i >= 0; i--)
                    cout << ans[i];
                cout << endl;

                /*
                for(int i = cnt-1; i >= 0; i--){
                    printf("%d ", ans[i]);
                }

                //line;
                cout << "-";
                int pos = 0;
                for(int i = 0; i < cnt; i++){
                    if(ans[i] == 0) pos++;
                    else break;
                }
                line;
                cout << pos << endl;
                for(int i = pos; i < cnt-1; i++)
                    cout << ans[i];
                cout << endl;

                line;
                for(int i = 0; i < cnt; i++){
                    cout << ans[i] << endl;
                }
                */
            }

        }
        else{//b + a -
            int x = cmp(ta, tb);
            if(x == 0){
                cnt = 1;
                ans[0] = 0;
            }
            else if(x > 0){
                int temp = 0;
                for(int i = 0; i < la; i++){
                    int k = na[i];
                    //printf("%d - %d - %d = ", k, nb[i], temp);
                    if(temp) k--;
                    k -= nb[i];
                    if(k < 0){
                        temp = 1;
                        k += 10;
                    }
                    else temp = 0;
                    ans[cnt++] = k;
                    //printf("%d!!\n", k);
                }
                cout << "-";
                int pos = cnt-1;
                for(int i = cnt-1; i >= 0; i--){
                    if(ans[i] == 0) pos--;
                    else break;
                }
                for(int i = pos; i >= 0; i--)
                    cout << ans[i];
                cout << endl;
            }
            else{
                int temp = 0;
                for(int i = 0; i < lb; i++){
                    int k = nb[i];
                    if(temp) k--;
                    k -= na[i];
                    if(k < 0){
                        temp = 1;
                        k += 10;
                    }
                    else temp = 0;
                    ans[cnt++] = k;
                }
                //cout << "-";
                int pos = cnt-1;
                for(int i = cnt-1; i >= 0; i--){
                    if(ans[i] == 0) pos--;
                    else break;
                }
                for(int i = pos; i >= 0; i--)
                    cout << ans[i];
                cout << endl;
            }
        }
    }
    return 0;
}

1006 最长公共子序列Lcs

 

1018 排序

#include<bits/stdc++.h>
using namespace std;

#define clr(a) memset(a, 0, sizeof(a))
#define line cout << "--------" << endl


typedef long long ll;
const int maxn = 1e5 + 10;
const int N = 1010;


int main(){

    int n, a[maxn];
    cin >> n;
    for(int i = 0; i < n; i++){
        scanf("%d", &a[i]);
    }
    sort(a, a+n);
    for(int i = 0; i < n; i++)
        cout << a[i] << endl;

    return 0;
}

1019 逆序数

 

1027 大数乘法

import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;

public class Main{

    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        BigInteger a = cin.nextBigInteger();
        BigInteger b = cin.nextBigInteger();
        BigInteger c = a.multiply(b);
        System.out.println(c);
    }
}

1046 A^B Mod C

#include <bits/stdc++.h>
#define MAXN 50010
#define line cout << "------------" << endl
using namespace std;

typedef long long ll;


ll qkm(ll a, ll b, ll c){
    ll base = 1;
    while(b > 0){
        a = a % c;
        if(b % 2 == 1)
            base = base * a % c;
        b /= 2;
        a = a * a % c;
    }
    return base % c;
}


int main(void){
    ll a, b, c;
    while(scanf("%lld%lld%lld", &a, &b, &c) != EOF){
        //line;
        ll ans = qkm(a, b, c);
        printf("%lld\n", ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值