USACO Fractions to Decimals,有重大疑问

有两种方法,参考http://jijiwaiwai163.blog.163.com/blog/static/186296211201262911916893/

我用的笨办法,长除法

题目真立马贱的要死,思路简单,数据诡异到爆,特别是他妈的最后要 76 个字符 一排输出,怎么控制,日~~

借鉴的别人的代码,为什么在输入“(”的时候不需要k++,我百思不得其解,要烦死了,一道破题浪费我6个小时

/* 
ID: wangxin12 
PROG: fracdec
LANG: C++ 
*/ 
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;

int N, D;
int cyclePoint;
bool isCycle = false;
int result[100001];
bool remainder[100001];
int pos[100001];

int main() {
	ifstream fin("fracdec.in");
	ofstream fout("fracdec.out");

	fin>>N>>D;

	int remain, ans;
	int i = 0; //记载短除法的次数,从第0次开始
	while(1) {
		ans = N / D;
		result[i] = ans;
	
		remain = N % D;
		if(remain == 0)
			break;

		if( !remainder[remain]) {
			remainder[remain] = true;
			pos[remain] = i;  //remain第一次出现时为第i次短除
		} else {
			cyclePoint = pos[remain];  
			isCycle = true;
			break;
		}
		i++;
		N = remain * 10;
	}

	int k = 0;   //记载位数,因为整数部分result[0]可能不止一位
	int temp = result[0];
	if(temp == 0) k++;
	while(temp > 0) {
		k++;
		temp /= 10;;
	}
	k++;  //小数点位数

	//output
	for(int j = 0; j <= i; j++, k++) {
		if(k % 76 == 0)
			fout<<endl;
		if(isCycle && j == cyclePoint + 1) {
			fout<<'(';
			//k++;    //为什么这里多输入了一个 ( ,但是字符计数k不用加1 ?????????
		}
		fout<<result[j];
		if(j == 0)
			fout<<'.';
	}
	if(i < 1)
		fout<<'0';
	if(isCycle)
		fout<<')'<<endl;
	else 
		fout<<endl;

	return 0;
}






我的第一版代码,操他妈的就是通过不去,日!!!
/* 
ID: wangxin12 
PROG: fracdec
LANG: C++ 
*/ 
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

int numerator, denominator;
vector<int> answer;
vector<int> remainder;
int cycleStart;

bool searchRemain(int main);
bool cycle = false;

int main() {
	ifstream fin("fracdec.in");
	ofstream fout("fracdec.out");

	int i,j;

	int ans, remain;
	fin>>numerator>>denominator;
	ans = numerator / denominator;
	remain = numerator % denominator;

	answer.push_back(ans);
	remainder.push_back(remain);

	while(remain != 0) {
		ans = remain * 10 / denominator;
		answer.push_back(ans);
		remain = remain * 10 % denominator;

		if(searchRemain(remain))
			break;
		remainder.push_back(remain);
	}
	
	//output
	stringstream result;
	if(answer.size() == 1) {
		fout<<answer[0]<<'.'<<0<<endl;
	} else {
		for(j = 0; j < answer.size(); j++) {
			if(j == 1) result<<'.';
			if(cycle && j == cycleStart + 1) {
				result<<'(';
				cycle = true;
			}
			result<<answer[j];
		}
		if(cycle) result<<')';
	
		string final = result.str();
		for(int k = 0; k < final.size(); k++) {
			if(k != 0 && k % 76 ==0 )
				fout<<endl;
			fout<<final[k];
		}
		fout<<endl;
	}

	fin.close();
	fout.close();
	return 0;
}

//线性搜索,到test 9就超时,非常郁闷
bool searchRemain(int main) {
	for(int i = 0; i < remainder.size(); i++) {
		if(remainder[i] == main) {
			cycleStart = i;
			cycle = true;
			return true;
		}
	}
	return false;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值