算法学习--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.将原始数据中的每一个样本用向量表示,把所有样本组合起来构成一个矩 ...
随机推荐
- PHP后台批量删除数据
html <form action="" method="post"> <div><input type="submit ...
- Webkit JNI
WebCoreFrameBridge.cpp BrowserFrame通过jni传下来的调用都会调用到WebCoreFrameBridge.cpp中的对应函数中,其他webkit的模块想回调信息给Br ...
- UITableView基础入门
新建一个Single View Application 添加一个空类如下: using System; using UIKit; using Foundation; namespace BasicTa ...
- Linux就该这么学--命令集合5(用户与组管理命令)
1.useradd命令用于创建新用户:(useradd [选项] 用户名) 附录: -d 指定用户的家目录 -D 展示默认值 -e 账号有效截止日期,格式:YYY-MM-DD -g 指定一个初始用户组 ...
- MongoDB学习笔记(1):MongoDB的安装和说明
MongoDB学习笔记(1):MongoDB的安装和说明 快速开始 下载地址 官网下载: https://www.mongodb.com/download-center?jmp=nav#communi ...
- 数组/字符串/ Math / 方法示例
数组 Array concat 数组的合并 <script> var north = ["北京","上海","深圳"]; va ...
- PAT 天梯赛 L1-054. 福到了 【字符串】
题目链接 https://www.patest.cn/contests/gplt/L1-054 思路 可以先将字符串用字符串数组 输入 然后用另一个字符串数组 从 n - 1 -> 0 保存 其 ...
- 锁定xcode api 文档
1, 打开终端2, 前往Xcode.app, 命令: cd /Applications/Xcode.app 3, 把头文件修改为只读, 命令: sudo chown -hR root:wheel Co ...
- Codeforces Round #394 (Div. 2) D. Dasha and Very Difficult Problem —— 贪心
题目链接:http://codeforces.com/contest/761/problem/D D. Dasha and Very Difficult Problem time limit per ...
- CISCO-从路由器上下载IOS
准备工作:一台装有TFTP服务器的PC,一台带有IOS的路由器,并用网线连接上 设置路由器接口和计算机网卡的IP地址在同一网段,并且互相能ping通. 1,安装Cisco TFTP Server 2, ...