华为机试—整数相除

两个整数相除,将结果用字符串返回。如果是循环小数,将循环的位用括号括起来。

函数原型为 void div(const int a,const int b,char *str)

输入:1 3

输出:0.(3)

#include<iostream>
#include<string>
using namespace std;

int maxn = 100; //设置字符串的最大位数
int reminder_exist[10000];
int reminder_pos[10000];

void div(const int a, const int b, char *str)
{
	int numerator,denominator,quotient, reminder, outcnt = 0;
	int flag = 0; //负数标志

	memset(reminder_exist, 0, sizeof(reminder_exist));
	memset(reminder_pos, 0, sizeof(reminder_pos));

	numerator = a; //定义const int,由于要改变分子分母,所以我们通过中间变量转化
	denominator = b;

	if(a*b < 0)
		flag = 1;//有负数

	//将分子和分母变成整数
	numerator = numerator < 0 ? -numerator:numerator;
	denominator = denominator < 0 ? -denominator:denominator;

	quotient = numerator/denominator;
	reminder = numerator%denominator;
	int integer = quotient;

	//找出循环
	int cycle_pos = maxn; //循环的位置
	int i = 0;
	for(i = 0; i <= maxn; i++)
	{
		//找出余数相等的情况,求出循环周期
		if(reminder_exist[reminder])
		{
			cycle_pos = reminder_pos[reminder];
			break;
		}
		else
		{
			reminder_exist[reminder] = 1;
			reminder_pos[reminder] = i;
		}

		numerator = reminder *10;
		quotient = numerator/denominator;
		reminder = numerator%denominator; 

		str[outcnt++] = quotient + '0'; //将更新的商存入字符串中
	}

	str[outcnt++] = '\0';

	if(!flag)
		cout << integer << "." ;
	else
		cout << "-" << integer << ".";

	for(int j = 0; j < cycle_pos; j++)
		cout << str[j];

	cout << "(";

	for(j = cycle_pos; j < i;j++)
		cout <<  str[j];

	cout << ")" << endl;
}

int main()
{
	int a,b;
	char s[100];
	while(cin >> a >> b){
		div(a,b,s);
	}
	cout<<endl;
	return 0;
}

测试结果,可能想的不周全,欢迎查漏补缺:

时间: 2024-08-05 02:54:02

华为机试—整数相除的相关文章

华为机试—大数相减

题目:大数相减 输入两行字符串正整数,第一行是被减数,第二行是减数,输出第一行减去第二行的结果. 备注:1.两个整数都是正整数,被减数大于减数 示例: 输入:1000000000000001       1 输出:1000000000000000 #include <stdio.h> #include <string.h> #define MAX 100 //100位大数相减 int bigNumSub(char a[],char b[],char sub[]) { int i=0

华为机试—整数数组排序(字符串输入输出)

#include<stdio.h> /* 编写一个程序,实现排序算法,用户输入证书序列,程序将所有证书从小到大排序并输出 输入:数字序列,长度小于1024,两个数字之间以逗号分隔,所有数字均为非负整数,整数个数小于128 输出:排序后的整数序列,两个数字之间以逗号分隔 */ int main(int argc, char *argv[]) { int a[129]; int i=0; scanf("%d",&a[i++]); char c; while(scanf(

2015华为机试——整数分割

题目描述: 一个整数可以拆分为2的幂的和,例如: 7 = 1+ 2 + 4 7 = 1 + 2 + 2 + 2 7 = 1 + 1 + 1 + 4 7 = 1 + 1 + 1 + 2 + 2 7 = 1 + 1 + 1 + 1 + 1 + 2 7 = 1 + 1 + 1 + 1 + 1 + 1 + 1 总共有六种不同的拆分方式 再比如: 4可以拆分成: 4 = 4, 4 = 1+1+1+1, 4 = 2+2, 4 = 1+1+2. 用f(n)表示n的不同拆分的种数,例如f(7) = 6. 要求读

[华为机试真题]69.姓名的夫妻相

题目 在中国,形容夫妻恩爱的词汇中,大家用的比较多的就是"夫妻相".所谓"夫妻相",就是两个人看上去比较般配,长相.身材等某些方面有一定的相似度. 本题则另辟蹊径,从人的姓名维度,以字母重复个数来寻找最具"夫妻相"的人. 题目中预先给定一组女士的姓名拼音.输入男士的姓名拼音(拼音中间可以有空格,字母全部小写),依预先给定姓名拼音的先后遍历所有姓名,输出字母重复数最多的女士姓名. 规则1:如果字母重复数最多的女士有多位相同,则以最先匹配的女士做为最

2014华为机试西安地区B组试题

2014华为机试西安地区B组试题 题目一.亮着点灯的盏数 一条长廊里依次装有n(1≤n≤65535)盏电灯,从头到尾编号1.2.3.-n-1.n.每盏电灯由一个拉线开关控制.开始,电灯全部关着. 有n个学生从长廊穿过.第一个学生把号码凡是1的倍数的电灯的开关拉一下:接着第二个学生把号码凡是2的倍数的电灯的开关拉一下:接着第三个学生把号码凡是3的倍数的电灯的开关拉一下:如此继续下去,最后第n个学生把号码凡是n的倍数的电灯的开关拉一下.n个学生按此规定走完后,长廊里电灯有几盏亮着. 注:电灯数和学生

华为机试在线训练(4)

华为机试在线训练:字符串分隔 题目描述 ?连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组:?长度不是8整数倍的字符串请在后面补数字0,空字符串不处理. 输入描述: 连续输入字符串(输入2次,每个字符串长度小于100) 输出描述: 输出到长度为8的新字符串数组 输入例子: abc123456789 输出例子: abc000001234567890000000 代码: 1 import java.util.Arrays; 2 import java.util.Scanner; 3

2014深圳华为机试剖析

题一: (1)给出一个整数(负数使用其绝对值),输出这个整数中的两种递减数(1.最大递减数:2.递减数中各位数之和最大的数)之和. 递减数:一个数字的递减数是指相邻的数位从大到小排列的数字,不包含相邻的数位大小相同的情况.最大递减数:所输入整数的所有递减数中值最大的一个. 如: 75345323,递减数有:75,753,53,53,532,32.那么最大的递减数为753. 各位数字之和最大的递减数: 如75345323中的各递减数:75各位数之和=12(7+5=12),753各位数之和=15(7

华为机试(6)

初级题描述:10个学生考完期末考试评卷完成后,A老师需要划出及格线,要求如下:  (1) 及格线是10的倍数:   (2) 保证至少有60%的学生及格:  (3) 如果所有的学生都高于60分,则及格线为60分  (4) 及格线越高越好,但最高不能超过60    输入:输入10个整数,取值0~100   输出:输出及格线,10的倍数   输入样例:61 51 49 30 20 10 70 80 90 99  输出样例:50 解题思路:从高到低枚举及格线,输出第一个满足要求的及格线就是答案 #inc

华为机试-公共字串计算

题目描述题目标题:计算两个字符串的最大公共字串的长度,字符不区分大小写详细描述:接口说明原型:int getCommonStrLength(char * pFirstStr, char * pSecondStr);输入参数: char * pFirstStr //第一个字符串 char * pSecondStr//第二个字符串 输入描述:输入两个字符串 输出描述:输出一个整数 输入例子:asdfas werasdfaswer 输出例子:6 效率低的Java程序实现: import java.ut