首页 > 其他分享 >uva 465(高精度)

uva 465(高精度)

时间:2023-06-28 19:32:51浏览次数:54  
标签:10 return integer 高精度 465 ++ int ans uva


题目:

Write a program that reads an expression consisting of two non-negative integer and an operator. Determine if either integer or the result of the expression is too large to be represented as a ``normal'' signed integer (type integer if you are working Pascal, type int

input

An unspecified number of lines. Each line will contain an integer, one of the two operators + or *, and another integer.

output

For each line of input, print the input followed by 0-3 lines containing as many of these three messages as are appropriate: ``first number too big'', ``second number too big'', ``result too big''.

sample input

300 + 3
9999999999999999999999 + 11

sample output

300 + 3
9999999999999999999999 + 11
first number too big
result too big

题解:用正常做法,将两个数提取,将他们的结果也提取,判断是否大于int,,注意 0 * 这个陷阱。巧妙的方法有将两个数所在的字符串用atof()函数转成double类型,和int比大小。。。


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

const char c[10] = {'2', '1', '4', '7', '4', '8', '3', '6', '4', '7'};

int k, flag;
int judge(string s) {
	int len = s.size(), i;
	if (len > 10) { 
		for (i = 0; i < len; i++)
			if (s[i] != '0' && (len - i - 1) >= 10)
				return 1;
	}
	else if (len == 10 ) 
		for (i = 0; i < len; i++) {
			if (s[i] < c[i])
				break;
			if (s[i] > c[i])
				return 1;
		}
	return 0;
}
int result(string s1, string s2) {
	int ans[500] = {0}, a[500] = {0}, b[500] = {0};
	string anss = "";
	int i, j, q;
	int len1 = s1.size();
	int len2 = s2.size();
	if (flag == 1) {
		for (i = 0; i < len1; i++) 
			a[i] = s1[len1 - 1 - i] - '0';
		for (i = 0; i < len2; i++) 
			b[i] = s2[len2 - 1 - i] - '0';
		int lenx = len1 > len2 ? len1 : len2;
		for (i = 0; i < lenx; i++) {
			ans[i] = a[i] + b[i] + ans[i];
			ans[i + 1] = ans[i] / 10;
			ans[i] = ans[i] % 10;
		}
		if (ans[lenx])
			lenx++;
		for (j = lenx - 1; j >= 0; j--) 
			anss += ans[j] + '0';
//		cout << anss << endl;
		int a = judge(anss);
		if (a == 1)
			return 1;
		else
		 	return 0;
	}
	if (flag == 2) {
		int ans[500] = {0};
		for (i = len1 - 1; i >= 0; i--)
			for (j = len2 - 1, q = len1 - 1 - i; j >= 0; j--)
				ans[q++] += (s1[i] - '0') * (s2[j] - '0');
		for (i = 0; i < 500; i++) {
			ans[i + 1] += ans[i] / 10;
			ans[i] %= 10;
		}
		for (i = 499; i >=0; i--)
			if (ans[i] != 0)
				break;
		if (i == -1)
			return 0;
		else
			for (q = i; q >= 0; q--)
				anss += ans[q] + '0';
		int a = judge(anss);
		if (a == 1)
			return 1;
		else
			return 0;
	}
}

int main(){
	string str, str1, str2;
	while(getline(cin, str)) {
		k = 0;
		int length = str.size();
		str1 = "";
		str2 = "";
		flag = 0;
		for (int i = 0; i < length; i++)
			if(str[i] >= '0' && str[i] <= '9' && flag == 0)
				str1 += str[i];
			else if (str[i] == '+')
				flag = 1;
			else if (str[i] == '*')
				flag = 2;
			else if (str[i] >= '0' && str[i] <= '9' && flag != 0)
				str2 += str[i];
		cout << str << endl;
		int k1 = judge(str1);
		int k2 = judge(str2);
		if (k1 == 1) 
			cout << "first number too big" << endl;
		if (k2 == 1) 
			cout << "second number too big" << endl;
		if (flag == 2) {  //判断0 * ? 结果不会大于int
			int i, j;
			if (k1 == 1) {
				int len2 = str2.size();
				for (i = 0; i < len2; i++)
					if (str2[i] != '0')
						break;
				if (i == len2)
					continue;
			}
			else if (k2 == 1) {
				int len1 = str1.size();
				for (j = 0; j < len1; j++)
					if (str1[j] != '0')
						break;
				if (j == len1)
					continue;
			}
		}
		if (k1 == 1 || k2 == 1)
			cout << "result too big" << endl;
		else
			if (result(str1, str2) == 1)
				cout << "result too big" << endl;
	}
	return 0;
}



标签:10,return,integer,高精度,465,++,int,ans,uva
From: https://blog.51cto.com/u_10729926/6575699

相关文章

  • uva 123(排序、检索)
    题目:Searchingandsortingarepartofthetheoryandpracticeofcomputerscience.Forexample,binarysearchprovidesagoodexampleofaneasy-to-understandalgorithmwithsub-linearcomplexity.Quicksortisanefficient[averagecase]comparisonbased......
  • uva 10034(最小生成树)
    题目:InanepisodeoftheDickVanDykeshow,littleRichieconnectsthefrecklesonhisDad'sbacktoformapictureoftheLibertyBell.Alas,oneofthefrecklesturnsouttobeascar,sohisRipley'sengagementfallsthrough.ConsiderDick......
  • uva 10878(字符串)
    题目:"Machinestakemebysurprisewithgreatfrequency."AlanTuringYourbosshasjustunearthedarollofoldcomputertapes.Thetapeshaveholesinthemandmightcontainsomesortofusefulinformation.Itfallstoyoutofigureoutwhatisw......
  • uva 113(数学)
    题目:Currentworkincryptographyinvolves(amongotherthings)largeprimenumbersandcomputingpowersofnumbersmodulofunctionsoftheseprimes.Workinthisareahasresultedinthepracticaluseofresultsfromnumbertheoryandotherbranchesofmat......
  • 应用案例分享 | 基于高精度三维机器视觉的汽车轮胎装配系统应用
    Part.1 行业背景汽车轮胎装配是汽车制造和维修过程中的关键环节,随着汽车产量的增加和市场竞争的加剧,汽车制造商对轮胎装配的自动化需求也越来越高。如今,汽车制造商们也正努力积极的提升其工艺技术水平,以应对不断增长的市场需求,希望采用更先进、更灵活、更智能的装配技术来提高汽车......
  • UVA12222 Mountain Road 山路 题解 dp
    UVA12222山路题意:--一个山路只有一条车道,因此不能有两辆方向相反的车同时在车道内。同时,为了保证安全,车道内不能超车,且同向行驶的车间距必须大于10分钟。现在给你n辆车,三个参数依次表示行驶方向,到达时刻,行驶时间。问如何安排能使最后一个通过的车通过时的时刻最小,输出这个值......
  • 高精度离线免费 的C#文字识别PaddleOCR库
    随便打开一个MicrosoftVisualStudio,新建一个WinForms项目,从下面列表中随便选择一个NET框架。目标平台要设置成X64,该OCR仅支持64位。 net35;net40;net45;net451;net452;net46;net461;net462;net47;net471;net472;net48;netstandard2.0;netcoreapp3.1;net5.0;net6.0;net7.0......
  • UVA11090 Going in Cycle!!题解
    题目大意给定一个N个点M条边的带权有向图,求平均值最小的回路。解法看到这种题目,喜欢打暴力的我一下就想到:遍历整个图,找到每一个环,然后算出它们的平均值,最后比较出最小值。然而,呃...,会T飞...既然我们不能暴力找最小值,那还有什么别的办法吗?我们只需要输出一个最小值就行了,既然......
  • 基于高精度三维机器视觉的汽车曲轴无序抓取系统应用
    Part.1 行业背景汽车产业的高速发展,对零部件自动化生产提出了更高要求。随着汽车销量的水涨船高,传统的手工生产模式已经难以满足大批量生产的需求,自动化生产是必然趋势。曲轴是汽车发动机的关键组件之一,生产过程复杂,自动化生成相对较低。曲轴起到将往复运动转化为旋转运动的作用,通......
  • UVA12716 GCD等于XOR GCD XOR
    UVA12716GCD等于XORGCDXOR一道数学题。 首先,我们可以知道,a-b>=gcd(a,b)=c;其次,a-b<=axorb=c;综上,可得a-b=c,即a-b=axorb.由于范围不大,直接枚举。第一层枚举c(因为c较少),第二层枚举a,(b=a-c) 再判断c是否等于a^b即可。#include<bits/stdc++.h>usingnamespacestd;......