华为机试题:输入两个超长整型构成的字符串,其间使用一个空格分隔,每个字符串最大长度为100个字符。求第一个整数除以第二个整数以后的余数。。
来源:互联网 发布:windows下pyqt4的安装 编辑:程序博客网 时间:2024/04/29 17:38
问题描述: 输入两个超长整型构成的字符串,其间使用一个空格分隔,每个字符串最大长度为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
- 华为机试题:输入两个超长整型构成的字符串,其间使用一个空格分隔,每个字符串最大长度为100个字符。求第一个整数除以第二个整数以后的余数。。
- 有三个参数的函数,一个为字符,两个为整数,第二个整数代表行数,第一个整数代表每行的字符的个数
- 输入两个字符串,从第一字符串中删除第二个字符串中所有的字符
- 输入两个字符串,从第一字符串中删除第二个字符串中所有的字符
- 输入两个字符串,从第一字符串中删除第二个字符串中所有的字符
- 输入两个字符串,从第一字符串中删除第二个字符串中所有的字符。
- 输入两个字符串,从第一字符串中删除第二个字符串中所有的字符
- 输入两个字符串,从第一字符串中删除第二个字符串中所有的字符
- 输入两个字符串,从第一字符串中删除第二个字符串中所有的字符
- php-输入两个字符串,从第一字符串中删除第二个字符串中所有的字符
- 输入两个字符串,从第一字符串中删除第二个字符串中所有的字符
- c#--编写一个名称为MyClass一个类,在该类中编写一个方法,名称为CountChar,返回值为整型,参数两个,第一个参数可以是字符串、整数、单精度、双精度,第二个参数为字符,方法功能返回第二个参
- 1.编写一个名称为MyClass一个类,在该类中编写一个方法,名称为CountChar,返回值为整型,参数两个,第一个参数可以是字符串、整数、单精度、双精度,第二个参数为字符,方法功能返回第二个参数在
- 输入两个字符串,从第一个字符串中删除第二个字符串中出现过的所有字符
- 给定一个有空格的字符串,求倒数第二个单词的长度
- 求两个字符串第一个不等的字符位置(长度)
- 【华为OJ】字符串解析运用-将字符串分解为多个整数,求各整数之和
- 简单编程(一) 新手入门作业 [要求用户输入两个整数,判断第一个整数是否是第二个整数的倍数(使用运算符%)。]
- ASP.NET最常用的页面生命周期事件
- C++递归
- 78. Subset
- Android Disign Support Library
- ios AVAssetExportSession的使用完整版
- 华为机试题:输入两个超长整型构成的字符串,其间使用一个空格分隔,每个字符串最大长度为100个字符。求第一个整数除以第二个整数以后的余数。。
- HDU 5452 Minimum Cut(贪心)
- C++ 分割字符串-Split
- Android手势和shape
- 查新功能的一些bug点还有开发需要注意的事项
- POJ 1988 Cube Stacking 并查集
- Android面试题(1)
- 【PAT乙级真题及训练集】1002. 写出这个数 (20)
- mysql行转列、列转行