2018_2_1_If We Were a Child Again_模拟除、取余

本文提供了一个解决UVA-10494编程问题的代码示例,该问题涉及字符串操作和除法运算。文章通过C++实现了解决方案,详细展示了如何处理大整数除法,并特别注意了对于小数位数处理的特殊情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://vjudge.net/problem/UVA-10494

这个题我之前的bug是,对于第二个数是个位数的时候

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cctype>
#include<cstring>
#include<fstream>
#include<map>

using namespace std;
#define ll unsigned long long
const int N=100000;
char x[N];
int ans[N];
ll y;
int main(){
	char op[3];
	while(scanf("%s%s%lld",x,&op,&y)!=EOF){
		ll ret=0;
		memset(ans,0,sizeof(ans));
		for(int i=0;i<strlen(x);i++){
			ret=ret*10+x[i]-'0';
			ans[++ans[0]]=ret/y;
			ret%=y;
		}
		if(op[0]=='/'){
			int k=1;
			while(!ans[k]&&k<=ans[0])k++;
			if(k>ans[0])
				printf("0");
			else while(k<=ans[0])
				printf("%d",ans[k]);
			
		}else 
			cout<<ret;
		puts("");
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值