华为机试题:输入两个超长整型构成的字符串,其间使用一个空格分隔,每个字符串最大长度为100个字符。求第一个整数除以第二个整数以后的余数。。

问题描述: 输入两个超长整型构成的字符串,其间使用一个空格分隔,每个字符串最大长度为100个字符。求第一个整数除以第二个整数以后的余数。。
运行时间限制: 无限制
内存限制: 无限制
输入: 输入两个以空格分隔的字符串,输入的每个字符串最大长度是100个字符。
输出: 输出为两个字符串相除以后的余数。如果结果异常,输出null
样例输入: 123456789 23456789
样例输出: 6172844
答案提示: 6172844

#include <iostream>
#include "string.h"
using namespace std;
//对齐
void just(char *str1,char *str2)
{
    int size1 = 0;
    int size2 = 0;
    char temp[100];
    memset(temp,0,sizeof(temp));
    for (int i = 0; *(str1+i)!='\0'; i++)
    {
        size1++;
    }
    for (int j = 0; *(str2+j)!='\0'; j++)
    {
        size2++;
    }
    if (size1>size2)
    {
        for (int i = 0; i < (size1-size2); i++)
        {
            temp[i] = '0';
        }
        for (int i = 0; i < size2; i++)
        {
            temp[i+size1-size2]=*(str2+i);
        }
        memcpy(str2,temp,size1);
    }
    if(size2>size1)
    {
        for (int i = 0; i < (size2-size1); i++)
        {
            temp[i] = '0';
        }
        for (int i = 0; i < size1; i++)
        {
            temp[i+size2-size1]=*(str1+i);
        }
        memcpy(str1,temp,size2);
    }
}
//比较
bool compare(char *str1,char *str2)
{
    bool flag = false;
    for (int i = 0; i < *(str1+i)!='\0'; i++)
    {
        int a = *(str1+i) - '0';
        int b = *(str2+i) - '0';
        if (a==0&&b!=0)
        {
            flag = false;
            break;
        }
        else if(a!=0&&b ==0)
        {
            flag = true;
            break;
        }else if(a!=0&&b!=0)
        {
            if(a==b)
            {
                flag = true;
            }else if(a>b)
            {
                flag = true;
                break;
            }else
            {
                flag = false;
                break;
            }
        }
    }
    return flag;
}
//利用减法求余
void sub_char(char *str1,char *str2,char *res,int flag = 0)
{
    int size = 0;
    for (int i = 0; *(str1+i)!='\0'; i++)
    {
        size++;
    }
    for (int i = size-1 ; i >= 0; i--)
    {
        char a1 = *(str1+i);
        char b1 = *(str2+i);
        int a = a1 - '0';
        int b = b1 - '0';
        int temp;
        if (flag==1)
        {
            if (a == 0&&i==0)
            { 
                break;
            }
            a = a-1;
        }
        if (a<b)
        {
            flag = 1 ;
            a = a + 10;
            temp = a - b;
            *(res+i)=temp+'0';
        }else
        {
            temp = a-b;
            flag = 0;
            if (temp == 0)
            {
                *(res+i)= temp+'0';
            }else
            {
                *(res+i)=temp+'0';
            }
        }
    }
}
void main()
{
    char str[201];
    char str1[100];
    char str2[100];
    char res[100];
    int str2_index = 0;
    //初始化
    memset(str,0,sizeof(str));
    memset(str1,0,sizeof(str1));
    memset(str2,0,sizeof(str2));
    memset(res,0,sizeof(res));
    //获取输入
    cin.getline(str,201);
    //分割
    bool flag1 = true;
    for (int i = 0; str[i]!='\0'; i++)
    {
        if (str[i]==' ')
        {
            flag1 = false;
            i = i+1;
        }
        if (flag1)
        {
            str1[i]=str[i];
        }else
        {
            str2[str2_index] = str[i];
            str2_index++;
        }
    }//分割结束
    //对齐str1和str2
    just(str1,str2);
    //第一次计算
    sub_char(str1,str2,res);
    if (!compare(res,str2))
    {
        cout<<"余数为:"<<str1<<endl;
    }

    //再次计算
    while (compare(res,str2))
    {
        char temp[100];
        memcpy(temp,res,sizeof(res));
        memset(res,0,sizeof(res));
        sub_char(temp,str2,res);
    }
    cout<<endl<<"余数为:"<<res;
    for (int i = 0; res[i]!=0; i++)
    {
        cout<<res[i];
    }
    cin.get();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值