Uva.202 Repeating Decimals
来源:互联网 发布:双十一淘宝销售额 编辑:程序博客网 时间:2024/06/05 04:17
//模拟人脑计算有理数的循环节#include<iostream>#include<cstdio>using namespace std;const int maxn = 10000;int decimal[maxn];struct node {int a, b;};node mark[maxn];int visited(int a, int b, int len);int main() {//freopen("Text.txt", "r", stdin);int a, b;while (scanf("%d %d", &a, &b)!=EOF) {int i, j, x, cnt=1;for (i = 0; i < maxn; i++) {//初始化decimal[i] = -1;mark[i].b = b;}decimal[0] = a / b;//计算整数部分x = a%b * 10;while (1) {decimal[cnt] = x / b;mark[cnt].a = x;if (visited(x, b, cnt))break;x = x%b * 10;cnt++;}//for (i = 0; i <= cnt; i++)printf("%d", decimal[i]);//cout << endl;int begin = visited(mark[cnt].a, mark[cnt].b, cnt);//printf("begin=%d\n", begin);//for (i = 1; i <= cnt; i++) printf("a = %d,b = %d\n", mark[i].a, mark[i].b);cnt--;//输出部分if (cnt > 50) {printf("%d/%d = %d.", a, b, decimal[0]);for (i = 1; i < begin; i++)printf("%d", decimal[i]);printf("(");for (i = begin; i <= 50; i++)printf("%d", decimal[i]);printf("...)\n");printf(" %d = number of digits in repeating cycle\n\n", cnt-begin+1);}else {printf("%d/%d = %d.", a, b, decimal[0]);for (i = 1; i < begin; i++)printf("%d", decimal[i]);printf("(");for (i = begin; i <= cnt; i++)printf("%d", decimal[i]);printf(")\n");printf(" %d = number of digits in repeating cycle\n\n", cnt-begin+1);}}return 0;}int visited(int a, int b, int len) {//利用分子分母相同计算循环节的开始for (int i = 1; i < len; i++) {if (mark[i].a == a&&mark[i].b == b)return i;}return 0;}
题目链接:
https://vjudge.net/problem/UVA-202
阅读全文
0 0
- UVa 202 - Repeating Decimals
- UVa 202 - Repeating Decimals
- UVa 202 - Repeating Decimals
- UVa 202 Repeating Decimals
- Uva 202 Repeating Decimals
- UVA - 202 Repeating Decimals
- UVa 202 Repeating Decimals
- UVa-202 - Repeating Decimals
- UVa 202 Repeating Decimals
- UVa 202 Repeating Decimals
- UVA - 202 Repeating Decimals
- Uva - 202 - Repeating Decimals
- UVA - 202 Repeating Decimals
- [UVA 202]Repeating Decimals
- UVa 202 Repeating Decimals
- uva 202 Repeating Decimals
- UVa 202 - Repeating Decimals
- UVa 202, Repeating Decimals
- 这是我想要学习的记录而已
- TeamViewer远程唤醒主机实战教程(多图)
- STL容器迭代器失效浅析
- Golang单元测试之httptest使用
- K均值聚类(K-means)
- Uva.202 Repeating Decimals
- springMVC设置不拦截静态资源
- 快速移动、淡入效果fadeIn()未完成就无法进行display的none操作
- 查看CentOS版本
- 行为型模式之解析器模式实现
- vi设置 打开文件直接跳到文件关闭时光标的位置
- django 用户认证 user对象
- 移动端与PHP服务端接口通信流程设计(基础版)
- list_for_each_entry