算法1:1020. Big Integer

来源:互联网 发布:爱情是如何产生的知乎 编辑:程序博客网 时间:2024/04/30 18:31

题目链接http://soj.sysu.edu.cn/1020点击打开链接

解释:超出位数的大数的存储

已知要求条件:
1) 1 < bi <= 1000 (1 <= i <= n),
2) gcd(bi,bj) = 1 (1 <= i,j <= n, i ≠ j). gcd表示两个的最大公约数为多少
大整数X:非负且小于M (其中M = b1*b2*...*bn)
x表示为(x mod b1,x mod b2,x mod b。。。。,)

输入:多个Test测试组 T(使用循环)
            Line1:n=?
line2: b1=?;b2=?...
line3:所存储的X
输出:(r1,r2,...,rn)其中rn=x mod bn
--------------------------------------------------------------------------

代码分析:C++

vector(向量):一种数据结构,类,相当于一个动态数组,即当无法确定数组规模时,用其来解决问题可达到的最大节约空间。用该数据结构可节省空间,以达到要求

用法:

          1.文件包含:     

           首先在程序开头处加上#include<vector>以包含所需要的类文件vector

          还有一定要加上using namespace std;

使用:一位数组:vector <int> a;

二维数组:vector <int *> a.

具体:点击打开链接
push_back   在数组的最后添加一个数据
2.pop_back    去掉数组的最后一个数据 
3.at                得到编号位置的数据
4.begin           得到数组头的指针
5.end             得到数组的最后一个单元+1的指针
6.front        得到数组头的引用
7.back            得到数组的最后一个单元的引用
8.max_size     得到vector最大可以是多大
9.capacity       当前vector分配的大小
10.size           当前使用数据的大小
11.resize         改变当前使用数据的大小,如果它比当前使用的大,者填充默认值
12.reserve      改变当前vecotr所分配空间的大小
13.erase         删除指针指向的数据项
14.clear          清空当前的vector
15.rbegin        将vector反转后的开始指针返回(其实就是原来的end-1)
16.rend          将vector反转构的结束指针返回(其实就是原来的begin-1)
17.empty        判断vector是否为空
18.swap         与另一个vector交换数据
#include<iostream>#include<string>#include<vector>//简单的高精度摸除,模除过程从个位开始,算当前位有//个指针指向下一位using namespace std;//模除函数int high_mod(string a, int b){string result;int temp = a[0] - '0';int s = 1;go_on:while (temp<b&&s<a.size()){temp = temp * 10 + a[s++] - '0';if (temp<b&&s<a.size())result += '0';}result += temp / b + '0';temp = temp%b;if (s <a.size())goto go_on;elsereturn temp;}int main(){int n;cin >> n;while (n--){int i;cin >> i; int b; string x; vector<int> bn;vector<int>::iterator p, q;while (i--){cin >> b;bn.push_back(b);}cin >> x;p = bn.begin();cout << "(";while (p != bn.end()){q = p;q++;if (q != bn.end())cout << high_mod(x, *p) << ',';else cout << high_mod(x, *p) << ')' << endl;p++;}}return 0;}





0 0
原创粉丝点击