算法学习--Day6
题目描述
输入描述:
- 输入包括两个数a和b,其中a和b的位数不超过1000位。
输出描述:
- 可能有多组测试数据,对于每组数据,
- 输出a+b的值。
输入
- 2 6
- 10000000000000000000 10000000000000000000000000000000
输出
- 8
- 10000000000010000000000000000000
- #include "stdio.h"
- #include "iostream"
- #include "string.h"
- using namespace std;
- struct bigInteger{
- int digit[];
- //to save the numbers
- int size;
- //to save the array's size
- void init(){
- for (int i = ; i < ; ++i) {
- digit[i] = ;
- }
- size = ;
- }
- //this is an init function.
- void set(char str[]){ //to put a string into our array.
- init();
- int L = strlen(str);
- int ans=;
- int times=;
- int c=;
- //we use the c to multiple 1、10、.....100000....
- for (int i = L -; i >= ; i--) {
- ans+=(str[i]-'')*c;
- times++;
- c*=;
- if(times== || i==){
- //i=0 to avoid the final array not being saved.
- digit[size++] = ans;
- times = ;
- c = ;
- ans = ;
- }
- }
- }
- bigInteger operator + (const bigInteger &A) const {
- bigInteger ret;
- ret.init();
- int nextMove = ;
- for (int i = ; i <size|| i<A.size ; ++i) {
- int tmp = digit[i]+ A.digit[i] +nextMove;
- nextMove = tmp /;
- ret.digit[ret.size++] = tmp%;
- }
- if(nextMove!=){
- ret.digit[ret.size++] = nextMove;
- }
- return ret;
- };
- void output(){
- for (int i = size-; i >= ; i--) {
- if(i!=size-) printf("%04d",digit[i]);
- else{
- printf("%d",digit[i]);
- }
- }
- cout<<endl;
- }
- }a,b,c;
- int main(){
- char str1[],str2[];
- while (scanf("%s%s",str1,str2)!=EOF){
- a.set(str1);
- b.set(str2);
- c = a+b;
- c.output();
- }
- return ;
- }
以前写密码学设计的时候就一直听说有高精度的设计,今天终于也实现了一把高精度的加法。因为从前没有接触过,所以也把思想放这里,方便以后查阅。
高精度的算法实现的思想大致为:
①创建一个结构体,在结构体中定义数组,将很大的数分成几个部分装到这个数组中。
②之后重构运算符,将两个数字一一对应的部分相加,并考虑进位的情况。
③定义输入、输出函数,将字符串数组输入并处理到结构体的数组中。
不过结构体中的细节要考虑一些,毕竟算法比较严谨。
之后我将近期写的部分代码放上来。
题目描述
输入描述:
- 可能有多组测试数据,每组测试数据的输入是一个正整数N,(1<N<10^9)。
输出描述:
- 对于每组数据,输出N的质因数的个数。
输入
- 120
输出
- 5
- #include "stdio.h"
- #include "iostream"
- using namespace std;
- int prime[];
- int flag[];
- int f=;
- int init(){
- for (int i = ; i < ; ++i) {
- flag[i] = ;
- }
- for (int j = ; j < ; ++j) {
- if(flag[j]==) continue;
- prime[f++] = j;
- for (int i = j*j; i < ; i+=j) {
- flag[i] = ;
- }
- }
- return ;
- }
- int main(){
- init();
- int n;
- while (scanf("%d",&n)!=EOF){
- int primeCup[];
- int perCount[];
- int count=;
- for (int i = ; i < f; ++i) {
- if(n%prime[i] == ){
- primeCup[count] = prime[i];
- perCount[count] = ;
- while (n % prime[i] == ){
- perCount[count]++;
- n/=prime[i];
- }
- count++;
- if(n==) break;
- }
- }
- if(n!=){
- primeCup[count] = n;
- perCount[count++] = ;
- }
- int ans=;
- for (int j = ; j < count; ++j) {
- ans+=perCount[j];
- }
- cout<<ans<<endl;
- }
- return ;
- }
题目描述
输入描述:
- k≤10000
输出描述:
- The k-th prime number.
输入
- 3
- 7
输出
- 5
- 17
- //Prime Number
- #include "stdio.h"
- #include "iostream"
- #include "math.h"
- using namespace std;
- int prime[];
- int f=;
- int init(){
- int flag[];
- for (int i = ; i < ; ++i) {
- flag[i]=;
- }
- for (int j = ; j <= ; ++j) {
- if(flag[j]==) continue;
- else{
- prime[f++] = j;
- for (int i = j*j; i <= ; i+=j) {
- flag[i] = ;
- }
- }
- }
- return ;
- }
- int main(){
- init();
- int n ;
- while (scanf("%d",&n)!=EOF){
- cout<<prime[n-]<<endl;
- }
- return ;
- }
这是素数的处理方法,使用预处理先行处理之后就很方便得到了。
题目描述
输入描述:
- 测试数据有多组,每组输入两个正整数。
输出描述:
- 对于每组输入,请输出其最大公约数。
输入
- 49 14
输出
- 7
- //最大公约数
- #include "stdio.h"
- #include "iostream"
- using namespace std;
- int init(int a, int b){
- if(b==){ return a;}
- else{
- return init(b,a%b);
- }
- }
- int main(){
- int a,b;
- while (scanf("%d%d",&a,&b)!=EOF){
- a>=b?cout<<init(a,b)<<endl:cout<<init(b,a)<<endl;
- }
- }
题目描述
输入描述:
- 输入包括一个整数N(0<=N<=100000)。
输出描述:
- 可能有多组测试数据,对于每组数据,
- 输出N的八进制表示数。
输入
- 7
- 8
- 9
输出
- 7
- 10
- 11
- //八进制
- #include "stdio.h"
- #include "iostream"
- using namespace std;
- int main(){
- int n;
- while (scanf("%d",&n)!=EOF){
- int ans=,flag=;
- int fin[];
- while (n!=){
- ans=n%;
- n/=;
- fin[flag++]=ans;
- }
- for (int i = flag-; i >= ; i--) {
- cout<<fin[i];
- }
- cout<<endl;
- }
- }
这是进制转换类型的题目,这种题目比较基础,但是考的也挺多。
所以下面我放上去通用的题目,输入任意进制 转化为任意进制。
例如:15 Aab3 7
将15进制转换为7进制并输出。
- //数值转换
- #include "iostream"
- #include "string.h"
- #include "stdio.h"
- using namespace std;
- int main(){
- int n,m;
- char input[];
- while (scanf("%d%s%d",&n,input,&m)!=EOF){
- int length = strlen(input);
- int first=;
- int ans = ;
- for (int i = length-; i >=; i--) {
- int x;
- if(input[i]>=''&&input[i]<='') {
- x = input[i] - '';
- }
- if(input[i]>='a'&&input[i]<='z'){
- x = input[i] - 'a' +;
- }
- if(input[i]>='A'&&input[i]<='Z'){
- x = input[i] - 'A'+;
- }
- ans+=x*first;
- first=first*n;
- }
- char output[];
- int flag=;
- do{
- int y =ans % m;
- if (y>=) {output[flag] = (y-)+'A';}
- else {output[flag] = y+'';}
- flag ++;
- ans/=m;
- }while (ans);
- for (int j = flag-; j >= ; j--) {
- cout<<output[j];
- }
- cout<<endl;
- }
- }
题目描述
输入描述:
- 输入格式:测试输入包含若干测试用例。每个测试用例占一行,给出m和A,B的值。
- 当m为0时输入结束。
输出描述:
- 输出格式:每个测试用例的输出占一行,输出A+B的m进制数。
输入
- 8 1300 48
- 2 1 7
- 0
输出
- 2504
- 1000
- //
- // Created by 陈平 on 2018/4/22.
- //又一板 A+B
- #include "stdio.h"
- #include "iostream"
- #include "math.h"
- using namespace std;
- long long a,b;
- int mFunction(int m, long long a){
- int con[];
- int flag = ;
- while (a!=){
- con[flag] = a%m;
- a/=m;
- flag++;
- }
- for (int i = flag-; i >= ; i--) {
- cout<<con[i];
- }
- cout<<endl;
- return ;
- }
- int main(){
- int m;
- while (scanf("%d",&m)!=EOF&&m!=){
- scanf("%lld%lld",&a,&b);
- long long fin = a+b;
- if (a==&b==) cout<<<<endl;
- else mFunction(m,fin);
- }
- return ;
- }
算法学习--Day6的更多相关文章
- DSP算法学习-过采样技术
DSP算法学习-过采样技术 彭会锋 2015-04-27 23:23:47 参考论文: 1 http://wr.lib.tsinghua.edu.cn/sites/default/files/1207 ...
- 算法学习之C语言基础
算法学习,先熟悉一下C语言哈!!! #include <conio.h> #include<stdio.h> int main(){ printf(+); getch(); ; ...
- Python之路,Day21 - 常用算法学习
Python之路,Day21 - 常用算法学习 本节内容 算法定义 时间复杂度 空间复杂度 常用算法实例 1.算法定义 算法(Algorithm)是指解题方案的准确而完整的描述,是一系列解决问题的 ...
- C / C++算法学习笔记(8)-SHELL排序
原始地址:C / C++算法学习笔记(8)-SHELL排序 基本思想 先取一个小于n的整数d1作为第一个增量(gap),把文件的全部记录分成d1个组.所有距离为dl的倍数的记录放在同一个组中.先在各组 ...
- 算法学习之BFS、DFS入门
算法学习之BFS.DFS入门 0x1 问题描述 迷宫的最短路径 给定一个大小为N*M的迷宫.迷宫由通道和墙壁组成,每一步可以向相邻的上下左右四格的通道移动.请求出从起点到终点所需的最小步数.如果不能到 ...
- 二次剩余Cipolla算法学习笔记
对于同余式 \[x^2 \equiv n \pmod p\] 若对于给定的\(n, P\),存在\(x\)满足上面的式子,则乘\(n\)在模\(p\)意义下是二次剩余,否则为非二次剩余 我们需要计算的 ...
- Manacher算法学习笔记 | LeetCode#5
Manacher算法学习笔记 DECLARATION 引用来源:https://www.cnblogs.com/grandyang/p/4475985.html CONTENT 用途:寻找一个字符串的 ...
- 第四百一十五节,python常用排序算法学习
第四百一十五节,python常用排序算法学习 常用排序 名称 复杂度 说明 备注 冒泡排序Bubble Sort O(N*N) 将待排序的元素看作是竖着排列的“气泡”,较小的元素比较轻,从而要往上浮 ...
- PCA算法学习(Matlab实现)
PCA(主成分分析)算法,主要用于数据降维,保留了数据集中对方差贡献最大的若干个特征来达到简化数据集的目的. 实现数据降维的步骤: 1.将原始数据中的每一个样本用向量表示,把所有样本组合起来构成一个矩 ...
随机推荐
- JMeter中使用Put请求方式请求接口
前言 现在有如下接口,是以PUT的方式请求的: 请求URL:IP+Port+/api/v1/apps/{appId} 请求参数: 参数名 必选 类型 nameCn 是 string nameEn 是 ...
- linux下如何安装软件(转载)
来源:http://zhidao.baidu.com/link?url=5oR8WxygPvVMhSZvXQahYKm01JPTmQnEUjbQF562Yxgd3r6bYpki1ZPcHAsij6E4 ...
- php判断某字符串是否不以数字或其他特殊字符开头
if(preg_match("/^[^\d-.,:]/",$addr)){ echo $addr.'不是数字或其他特殊字符开头'; }
- PHP收邮件receiveMail
用PHP来发邮件,相信大家都不陌生,但读取收件箱的话,接触就少了,这次总结下自己的经验,希望能够帮助大家. 注意:1.PHP读取收件箱主要是利用imap扩展,所以在使用下面方法前,必须开启imap扩展 ...
- 解决对象不支持“getElementsByClassName”属性或方法 ie兼容性
解决 IE 或者兼容模式不支持 document.getElementsByClassName() 的方法 自已实现document.getElementsByClassName(): 网页错 ...
- 九度OJ 1093:WERTYU (翻译)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1563 解决:609 题目描述: A common typing error is to place the hands on the ke ...
- 基于springboot的Dubbo的常规总结
1.引入jar包: <!-- Spring Boot Dubbo 依赖 --> <dependency> <groupId>com.alibaba.spring.b ...
- uva 401 Palindromes 解题报告
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- codeforces B. Fox and Cross 解题报告
题目链接:http://codeforces.com/problemset/problem/389/B 题目意思:给出一个由n行n列组成的board,其中'#'表示的一片地方恰好能画满十字架,画满的意 ...
- hdu-5776 sum(同余)
题目链接: sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Pro ...