题目描述

实现一个加法器,使其能够输出a+b的值。

输入描述:

  1. 输入包括两个数ab,其中ab的位数不超过1000位。

输出描述:

  1. 可能有多组测试数据,对于每组数据,
  2. 输出a+b的值。
示例1

输入

  1. 2 6
  2. 10000000000000000000 10000000000000000000000000000000

输出

  1. 8
  2. 10000000000010000000000000000000
  1. #include "stdio.h"
  2. #include "iostream"
  3. #include "string.h"
  4. using namespace std;
  5.  
  6. struct bigInteger{
  7. int digit[];
  8. //to save the numbers
  9. int size;
  10. //to save the array's size
  11. void init(){
  12. for (int i = ; i < ; ++i) {
  13. digit[i] = ;
  14. }
  15. size = ;
  16. }
  17. //this is an init function.
  18.  
  19. void set(char str[]){ //to put a string into our array.
  20. init();
  21. int L = strlen(str);
  22. int ans=;
  23. int times=;
  24. int c=;
  25. //we use the c to multiple 1、10、.....100000....
  26. for (int i = L -; i >= ; i--) {
  27. ans+=(str[i]-'')*c;
  28. times++;
  29. c*=;
  30. if(times== || i==){
  31. //i=0 to avoid the final array not being saved.
  32. digit[size++] = ans;
  33. times = ;
  34. c = ;
  35. ans = ;
  36. }
  37. }
  38. }
  39.  
  40. bigInteger operator + (const bigInteger &A) const {
  41. bigInteger ret;
  42. ret.init();
  43. int nextMove = ;
  44.  
  45. for (int i = ; i <size|| i<A.size ; ++i) {
  46. int tmp = digit[i]+ A.digit[i] +nextMove;
  47. nextMove = tmp /;
  48. ret.digit[ret.size++] = tmp%;
  49. }
  50. if(nextMove!=){
  51. ret.digit[ret.size++] = nextMove;
  52. }
  53. return ret;
  54. };
  55.  
  56. void output(){
  57. for (int i = size-; i >= ; i--) {
  58. if(i!=size-) printf("%04d",digit[i]);
  59. else{
  60. printf("%d",digit[i]);
  61. }
  62.  
  63. }
  64. cout<<endl;
  65. }
  66. }a,b,c;
  67.  
  68. int main(){
  69. char str1[],str2[];
  70. while (scanf("%s%s",str1,str2)!=EOF){
  71. a.set(str1);
  72. b.set(str2);
  73. c = a+b;
  74. c.output();
  75.  
  76. }
  77.  
  78. return ;
  79. }

以前写密码学设计的时候就一直听说有高精度的设计,今天终于也实现了一把高精度的加法。因为从前没有接触过,所以也把思想放这里,方便以后查阅。

高精度的算法实现的思想大致为:

①创建一个结构体,在结构体中定义数组,将很大的数分成几个部分装到这个数组中。

②之后重构运算符,将两个数字一一对应的部分相加,并考虑进位的情况。

③定义输入、输出函数,将字符串数组输入并处理到结构体的数组中。

不过结构体中的细节要考虑一些,毕竟算法比较严谨。

之后我将近期写的部分代码放上来。

题目描述

求正整数N(N>1)的质因数的个数。 相同的质因数需要重复计算。如120=2*2*2*3*5,共有5个质因数。

输入描述:

  1. 可能有多组测试数据,每组测试数据的输入是一个正整数N,(1<N<10^9)。

输出描述:

  1. 对于每组数据,输出N的质因数的个数。
示例1

输入

  1. 120

输出

  1. 5
  1. #include "stdio.h"
  2. #include "iostream"
  3. using namespace std;
  4. int prime[];
  5. int flag[];
  6. int f=;
  7. int init(){
  8. for (int i = ; i < ; ++i) {
  9. flag[i] = ;
  10. }
  11. for (int j = ; j < ; ++j) {
  12. if(flag[j]==) continue;
  13. prime[f++] = j;
  14. for (int i = j*j; i < ; i+=j) {
  15. flag[i] = ;
  16. }
  17. }
  18. return ;
  19. }
  20.  
  21. int main(){
  22. init();
  23. int n;
  24. while (scanf("%d",&n)!=EOF){
  25. int primeCup[];
  26. int perCount[];
  27. int count=;
  28. for (int i = ; i < f; ++i) {
  29. if(n%prime[i] == ){
  30. primeCup[count] = prime[i];
  31. perCount[count] = ;
  32. while (n % prime[i] == ){
  33. perCount[count]++;
  34. n/=prime[i];
  35. }
  36. count++;
  37. if(n==) break;
  38. }
  39.  
  40. }
  41. if(n!=){
  42. primeCup[count] = n;
  43. perCount[count++] = ;
  44. }
  45. int ans=;
  46. for (int j = ; j < count; ++j) {
  47. ans+=perCount[j];
  48. }
  49. cout<<ans<<endl;
  50. }
  51.  
  52. return ;
  53. }

题目描述

Output the k-th prime number.

输入描述:

  1. k10000

输出描述:

  1. The k-th prime number.
示例1

输入

  1. 3
  2. 7

输出

  1. 5
  2. 17
  1. //Prime Number
  2.  
  3. #include "stdio.h"
  4. #include "iostream"
  5. #include "math.h"
  6. using namespace std;
  7. int prime[];
  8. int f=;
  9. int init(){
  10. int flag[];
  11. for (int i = ; i < ; ++i) {
  12. flag[i]=;
  13. }
  14. for (int j = ; j <= ; ++j) {
  15. if(flag[j]==) continue;
  16. else{
  17. prime[f++] = j;
  18. for (int i = j*j; i <= ; i+=j) {
  19. flag[i] = ;
  20. }
  21. }
  22. }
  23. return ;
  24. }
  25. int main(){
  26. init();
  27. int n ;
  28. while (scanf("%d",&n)!=EOF){
  29. cout<<prime[n-]<<endl;
  30. }
  31. return ;
  32. }

这是素数的处理方法,使用预处理先行处理之后就很方便得到了。

题目描述

输入两个正整数,求其最大公约数。

输入描述:

  1. 测试数据有多组,每组输入两个正整数。

输出描述:

  1. 对于每组输入,请输出其最大公约数。
示例1

输入

  1. 49 14

输出

  1. 7
  1. //最大公约数
  2. #include "stdio.h"
  3. #include "iostream"
  4. using namespace std;
  5. int init(int a, int b){
  6. if(b==){ return a;}
  7. else{
  8. return init(b,a%b);
  9. }
  10. }
  11.  
  12. int main(){
  13. int a,b;
  14. while (scanf("%d%d",&a,&b)!=EOF){
  15. a>=b?cout<<init(a,b)<<endl:cout<<init(b,a)<<endl;
  16. }
  17. }

题目描述

输入一个整数,将其转换成八进制数输出。

输入描述:

  1. 输入包括一个整数N(0<=N<=100000)。

输出描述:

  1. 可能有多组测试数据,对于每组数据,
  2. 输出N的八进制表示数。
示例1

输入

  1. 7
  2. 8
  3. 9

输出

  1. 7
  2. 10
  3. 11
  1. //八进制
  2.  
  3. #include "stdio.h"
  4. #include "iostream"
  5. using namespace std;
  6.  
  7. int main(){
  8. int n;
  9. while (scanf("%d",&n)!=EOF){
  10. int ans=,flag=;
  11. int fin[];
  12.  
  13. while (n!=){
  14. ans=n%;
  15. n/=;
  16. fin[flag++]=ans;
  17. }
  18. for (int i = flag-; i >= ; i--) {
  19. cout<<fin[i];
  20. }
  21. cout<<endl;
  22. }
  23. }

这是进制转换类型的题目,这种题目比较基础,但是考的也挺多。

所以下面我放上去通用的题目,输入任意进制 转化为任意进制。

例如:15 Aab3 7

将15进制转换为7进制并输出。

  1. //数值转换
  2.  
  3. #include "iostream"
  4. #include "string.h"
  5. #include "stdio.h"
  6. using namespace std;
  7.  
  8. int main(){
  9. int n,m;
  10. char input[];
  11. while (scanf("%d%s%d",&n,input,&m)!=EOF){
  12. int length = strlen(input);
  13. int first=;
  14. int ans = ;
  15. for (int i = length-; i >=; i--) {
  16. int x;
  17. if(input[i]>=''&&input[i]<='') {
  18. x = input[i] - '';
  19. }
  20. if(input[i]>='a'&&input[i]<='z'){
  21. x = input[i] - 'a' +;
  22. }
  23. if(input[i]>='A'&&input[i]<='Z'){
  24. x = input[i] - 'A'+;
  25. }
  26. ans+=x*first;
  27. first=first*n;
  28.  
  29. }
  30. char output[];
  31. int flag=;
  32. do{
  33. int y =ans % m;
  34. if (y>=) {output[flag] = (y-)+'A';}
  35.  
  36. else {output[flag] = y+'';}
  37.  
  38. flag ++;
  39. ans/=m;
  40. }while (ans);
  41. for (int j = flag-; j >= ; j--) {
  42. cout<<output[j];
  43. }
  44. cout<<endl;
  45. }
  46. }

题目描述

输入两个不超过整型定义的非负10进制整数A和B(<=231-1),输出A+B的m (1 < m <10)进制数。

输入描述:

  1. 输入格式:测试输入包含若干测试用例。每个测试用例占一行,给出mAB的值。
  2. m0时输入结束。

输出描述:

  1. 输出格式:每个测试用例的输出占一行,输出A+Bm进制数。
示例1

输入

  1. 8 1300 48
  2. 2 1 7
  3. 0

输出

  1. 2504
  2. 1000
  1. //
  2. // Created by 陈平 on 2018/4/22.
  3. //又一板 A+B
  4. #include "stdio.h"
  5. #include "iostream"
  6. #include "math.h"
  7. using namespace std;
  8. long long a,b;
  9. int mFunction(int m, long long a){
  10. int con[];
  11. int flag = ;
  12.  
  13. while (a!=){
  14. con[flag] = a%m;
  15. a/=m;
  16. flag++;
  17. }
  18. for (int i = flag-; i >= ; i--) {
  19. cout<<con[i];
  20. }
  21. cout<<endl;
  22. return ;
  23. }
  24. int main(){
  25. int m;
  26. while (scanf("%d",&m)!=EOF&&m!=){
  27. scanf("%lld%lld",&a,&b);
  28. long long fin = a+b;
  29. if (a==&b==) cout<<<<endl;
  30. else mFunction(m,fin);
  31.  
  32. }
  33. return ;
  34. }

算法学习--Day6的更多相关文章

  1. DSP算法学习-过采样技术

    DSP算法学习-过采样技术 彭会锋 2015-04-27 23:23:47 参考论文: 1 http://wr.lib.tsinghua.edu.cn/sites/default/files/1207 ...

  2. 算法学习之C语言基础

    算法学习,先熟悉一下C语言哈!!! #include <conio.h> #include<stdio.h> int main(){ printf(+); getch(); ; ...

  3. Python之路,Day21 - 常用算法学习

    Python之路,Day21 - 常用算法学习   本节内容 算法定义 时间复杂度 空间复杂度 常用算法实例 1.算法定义 算法(Algorithm)是指解题方案的准确而完整的描述,是一系列解决问题的 ...

  4. C / C++算法学习笔记(8)-SHELL排序

    原始地址:C / C++算法学习笔记(8)-SHELL排序 基本思想 先取一个小于n的整数d1作为第一个增量(gap),把文件的全部记录分成d1个组.所有距离为dl的倍数的记录放在同一个组中.先在各组 ...

  5. 算法学习之BFS、DFS入门

    算法学习之BFS.DFS入门 0x1 问题描述 迷宫的最短路径 给定一个大小为N*M的迷宫.迷宫由通道和墙壁组成,每一步可以向相邻的上下左右四格的通道移动.请求出从起点到终点所需的最小步数.如果不能到 ...

  6. 二次剩余Cipolla算法学习笔记

    对于同余式 \[x^2 \equiv n \pmod p\] 若对于给定的\(n, P\),存在\(x\)满足上面的式子,则乘\(n\)在模\(p\)意义下是二次剩余,否则为非二次剩余 我们需要计算的 ...

  7. Manacher算法学习笔记 | LeetCode#5

    Manacher算法学习笔记 DECLARATION 引用来源:https://www.cnblogs.com/grandyang/p/4475985.html CONTENT 用途:寻找一个字符串的 ...

  8. 第四百一十五节,python常用排序算法学习

    第四百一十五节,python常用排序算法学习 常用排序 名称 复杂度 说明 备注 冒泡排序Bubble Sort O(N*N) 将待排序的元素看作是竖着排列的“气泡”,较小的元素比较轻,从而要往上浮 ...

  9. PCA算法学习(Matlab实现)

    PCA(主成分分析)算法,主要用于数据降维,保留了数据集中对方差贡献最大的若干个特征来达到简化数据集的目的. 实现数据降维的步骤: 1.将原始数据中的每一个样本用向量表示,把所有样本组合起来构成一个矩 ...

随机推荐

  1. JMeter中使用Put请求方式请求接口

    前言 现在有如下接口,是以PUT的方式请求的: 请求URL:IP+Port+/api/v1/apps/{appId} 请求参数: 参数名 必选 类型 nameCn 是 string nameEn 是 ...

  2. linux下如何安装软件(转载)

    来源:http://zhidao.baidu.com/link?url=5oR8WxygPvVMhSZvXQahYKm01JPTmQnEUjbQF562Yxgd3r6bYpki1ZPcHAsij6E4 ...

  3. php判断某字符串是否不以数字或其他特殊字符开头

    if(preg_match("/^[^\d-.,:]/",$addr)){ echo $addr.'不是数字或其他特殊字符开头'; }

  4. PHP收邮件receiveMail

    用PHP来发邮件,相信大家都不陌生,但读取收件箱的话,接触就少了,这次总结下自己的经验,希望能够帮助大家. 注意:1.PHP读取收件箱主要是利用imap扩展,所以在使用下面方法前,必须开启imap扩展 ...

  5. 解决对象不支持“getElementsByClassName”属性或方法 ie兼容性

      解决 IE 或者兼容模式不支持 document.getElementsByClassName() 的方法 自已实现document.getElementsByClassName():   网页错 ...

  6. 九度OJ 1093:WERTYU (翻译)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1563 解决:609 题目描述: A common typing error is to place the hands on the ke ...

  7. 基于springboot的Dubbo的常规总结

    1.引入jar包: <!-- Spring Boot Dubbo 依赖 --> <dependency> <groupId>com.alibaba.spring.b ...

  8. uva 401 Palindromes 解题报告

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. codeforces B. Fox and Cross 解题报告

    题目链接:http://codeforces.com/problemset/problem/389/B 题目意思:给出一个由n行n列组成的board,其中'#'表示的一片地方恰好能画满十字架,画满的意 ...

  10. hdu-5776 sum(同余)

    题目链接: sum Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 131072/131072 K (Java/Others) Pro ...