码迷,mamicode.com
首页 > 其他好文 > 详细

P1932 A+B A-B A*B A/B A%B Problem

时间:2018-02-02 22:02:24      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:length   输出   memset   运算   int   结果   div   输入   max   

P1932 A+B A-B A*B A/B A%B Problem

链接:https://www.luogu.org/problemnew/show/P1932

题目背景

这个题目很新颖吧!!!

题目描述

求A、B的和差积商余!

由于数据有修改,减法运算结果可能带负号!

输入输出格式

输入格式:

 

两个数两行

A B

 

输出格式:

 

五个数

和 差 积 商 余

 

输入输出样例

输入样例#1: 复制
1
1
输出样例#1: 复制
2
0
1
1
0

说明

length(A),length(B)<=10^4

A,B>0 每个点3s。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1000005;
int a[maxn],b[maxn],d[maxn],c[maxn],f,q,flag;
string s1,s2;
void init(){

    cin>>s1>>s2;
    if(s1.size() < s2.size() || (s1.size() == s2.size() && s1 < s2))
        swap(s1, s2),flag = 1;
    if(s2.size() < 8){
        f = 1;
        for(int i = 0; i < s2.size(); i++)
            q = q*10 + s2[i]-0;
    }
    a[0] = s1.size() , b[0] = s2.size();    
    for(int i = 1; i <= a[0]; i++)
        a[i] = s1[a[0] - i] - 0;
    for(int i = 1; i <= b[0]; i++)
        b[i] = s2[b[0] - i] - 0;
    
}

void print(int k){
    if(k == 1)printf("-");        
    for(int i = c[0]; i >= 1; i--)
        printf("%d",c[i]);
    cout<<endl;
    memset(c, 0, sizeof(c));
}
int cmp(){
    if(a[0] < b[0])return 0;
    if(a[0] > b[0])return 1;
    int i;
    for(i = a[0]; i >= 1; i--)
        if(a[i] != b[i])return a[i] > b[i];
    return 1;
}
void isjia(){
    c[0] = a[0] + 1;
    for(int i = 1; i <= c[0]; i++){
        c[i] += a[i] + b[i];
        c[i + 1] = c[i] / 10;
        c[i] %= 10;
    }
    while(c[0] > 1 && !c[c[0]])c[0]--;
    print(0);
}

void isjian(){
    memset(c,0,sizeof(c));
    c[0] = a[0];
    
    for(int i = 1; i <= c[0]; i++){
        c[i] += a[i] - b[i];
        if(c[i] < 0)c[i] += 10, c[i + 1]--;
    }
    while(c[0] > 1 && !c[c[0]])c[0]--;
}

void ischeng(){
    c[0] = a[0] + b[0] + 1;
    for(int i = 1; i <= a[0]; i++){
        int x = 0;
        for(int j = 1; j <= b[0]; j++){
            c[i + j - 1] += x + a[i]*b[j];
            x = c[i + j - 1]/10;
            c[i + j - 1] %= 10;
        }
        c[i + b[0]] = x;
    }
    while(c[0] > 1 && !c[c[0]])c[0]--;
}
void moni(){
    memset(c, 0, sizeof(c));
    int x = 0;
    for(int i = a[0]; i >= 1; i--){
        c[i] = (10*x + a[i])/q;
        x = (x*10 +a[i])% q;
    }
    
    c[0] = a[0];
    while(c[0] > 1 && !c[c[0]])c[0]--;
    print(0);
    cout<<x;
}
void ismod(){
    int cnt = 0;
    if(f){
        moni();return ;
    }
    while(1){
        if(!cmp())break;
        cnt++;
        isjian();
        for(int i = 0; i <= c[0]; i++)
            a[i] = c[i];
        
    }
    cout<<cnt<<endl;
    print(0);    
}

int main(){
    init();
    isjia();    
    isjian();
    print(flag);
    ischeng();
    print(0);
    if(!flag)ismod();
    else {
        cout<<"0"<<endl;
        for(int i = b[0]; i >= 1; i--)
        cout<<b[i];
    }
}

TLE两个点,先放出来吧

P1932 A+B A-B A*B A/B A%B Problem

标签:length   输出   memset   运算   int   结果   div   输入   max   

原文地址:https://www.cnblogs.com/EdSheeran/p/8406566.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!