算法学习--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.将原始数据中的每一个样本用向量表示,把所有样本组合起来构成一个矩 ...
随机推荐
- httpclient4 模拟访问网页 模拟登录 简单例子
JAVA后台模拟登录一个网站,获得一定权限后进一步操作. 所用的工具: Apache HttpComponents client 4.3版本 以下为代码: import org.apache.http ...
- jquery插件pagination实现分页
1.效果 2.HTML代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind=" ...
- 关于mybatis的学习笔记
配置文件 贴出mybatis的配置文件,这里mybatis还未与spring做整合: <?xml version="1.0" encoding="UTF-8&quo ...
- Vue实例和方法
github地址:https://github.com/manlili/vue_learn里面的lesson03 一 实例 每个 Vue 实例都会代理其 data 对象里所有的属性,改变data,vu ...
- codeforces 的 Codeforces Round #273 (Div. 2) --C Table Decorations
C. Table Decorations time limit per test 1 second memory limit per test 256 megabytes input standard ...
- WebDriver API——浏览器控制暨如何学习webdriver API
在测试过程中我们可能需要对浏览器进行控制,大小控制啊,刷新页面啊,前进后退等等,最常用的两个接口是window和Navigation. 我们最常用的就是这4个,那么你是否感兴趣它们后面是什么,它们是怎 ...
- div遮罩弹框口
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- 通过Chrome浏览器进行android调试/Remote Debugging on Android with Chrome
The way your web content behaves on mobile can be dramatically different from the desktop experience ...
- tomcat 启动增加参数
linux: JAVA_OPTS="$JAVA_OPTS -Dconfig.type=inte2 -Xms2048m -Xmx2048m -XX:PermSize=128m -XX:Max ...
- codeforces 569D D. Symmetric and Transitive(bell数+dp)
题目链接: D. Symmetric and Transitive time limit per test 1.5 seconds memory limit per test 256 megabyte ...