当前位置:网站首页>Large numbers (C language)
Large numbers (C language)
2022-06-26 04:38:00 【bu_ xiang_ tutou】
Add big numbers
#include<stdio.h>
#include<string.h>
int main()
{// Using string calculation A+B Because A,B It's too long
char a[501],b[501],c[509]="\0";
gets(a);
gets(b);
int x,y,d=0;
for( x=strlen(a)-1,y=strlen(b)-1; x>=0&&y>=0; x--,y--)
c[d++]=a[x]+b[y]-48;//-48( because '0' Of ASCII Code is 48), Minus one 48 Because c It is also a character expression '7'='0'-48+'7'
// c The first digit of the array is the single digit ...
if(x>=0)// At the end of the cycle ,y<0 了 , however x>=0.
//b The character length of the array does not have a Long , namely a It's five digits ,b It's a four digit situation
{
for(; x>=0; x--)
c[d++]=a[x];
}
if(y>=0)// ditto .
{
for(; y>=0; y--)
c[d++]=b[y];
}
for(x=0;x<strlen(c);x++)
if(c[x]>'9')// full 10 Into the 1
{
c[x]-=10;
if(c[x+1]==0)// Judge x+1 Is there an initial value on the bit
c[x+1]='0';
c[x+1]+=1;// Into the 1
}
for(x=strlen(c)-1;x>=0;x--)// Output in reverse order , Output from the highest bit to the first digit .
printf("%c",c[x]);
return 0;
}
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
char s1[100],s2[100];
int a[100],b[100];
int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
int main()
{
scanf("%s",s1);
scanf("%s",s2);
int n,i=0;
for(n=strlen(s1)-1;n>=0;n--)
{
a[i++]=s1[n]-'0';
}
i=0;
for(n=strlen(s2)-1;n>=0;n--)
{
b[i++]=s2[n]-'0';
}
for(n=0;n<max(strlen(s1),strlen(s2));n++)
{
a[n]+=b[n];
}
for(n=0;n<=100;n++)
{
if(a[n]>9)
{
a[n]-=10;
a[n+1]+=1;
}
}
for(n=100;a[n]==0&&n>=0;n--);
if(n>=0)
for(;n>=0;n--)
printf("%d",a[n]);
return 0;
}
Big number subtraction
#include"stdio.h"
#include"string.h"
char s1[405],s2[405];
int s[405];
void jian(char a[],char b[])//a String ratio b Long , Or both
{
int n,m,d=0;
for(n=strlen(a),m=strlen(b);n>=0&&m>=0;n--,m--)
{
s[d++]=a[n]-'0'-(b[m]-'0');
}
if(n>=0)
{
for(;n>=0;n--)
s[d++]=a[n]-'0';
}
for(n=0;n<=405;n++)
{
if(s[n]<0)
{
s[n]+=10;
s[n+1]-=1;
}
}
for(n=405;n>=0&&s[n]==0;n--);
if(n>=0)
for(;n>0;n--)
printf("%d",s[n]);
}
int main()
{
gets(s1),gets(s2);
if(strlen(s1)<strlen(s2)||(strlen(s1)==strlen(s2)&&strcmp(s1,s2)<0))// character string s2>s1
{
printf("-");
jian(s2,s1);
}
else if(strlen(s1)==strlen(s2)&&strcmp(s1,s2)==0)
printf("0");
else
jian(s1,s2);
return 0;
}
Multiplication of large numbers
#include <stdio.h>
#include<string.h>
#include<math.h>
int main() {
char a[1000],b[1000];
scanf("%s",a);
scanf("%s",b);
int len1,len2,n,m,t;
int s1[1000],s2[1000],c[10000];
len1=strlen(a),len2=strlen(b);
memset(s1,0,sizeof(s1));
memset(s2,0,sizeof(s2));
memset(c,0,sizeof(c));// hold s1,s2,c The initial value of the array in 0
for(m=0,n=len1-1; n>=0; n--)
s1[m++]=a[n]-'0';// Invert the array , And replace the characters with numbers , Easy to calculate
for(t=0,n=len2-1; n>=0; n--)
s2[t++]=b[n]-'0';
for(m=0; m<=len1-1; m++) {
for(t=0; t<=len2-1; t++)
c[m+t]+=s1[m]*s2[t];// Multiplication
}
for(m=0; m<=len1+len2; m++) {// full 10 Into the 1
if(c[m]>9) {
c[m+1]+=c[m]/10;
c[m]=c[m]%10;
}
}
for(m=len1+len2; (m>=0)&&(c[m]==0); m--); // hold m>=0 Put it in the front , It can avoid crossing the boundary ,(m=-1), First judge m>=0
// Skip high 0, Finding the first one doesn't mean 0 The location of , Pay attention to the semicolon .
if(m>=0) {
for(; m>=0; m--)
printf("%d",c[m]);}
else
printf("0");
return 0;
}
边栏推荐
- OSS CDN alicloud configuration method
- [geek challenge 2019] rce me
- Simple use of redis in laravel
- Gateway can not connect to tcp://127.0.0.1: Connection refused
- [H5 development] 03- take you hand in hand to improve H5 development - single submission vs batch submission with a common interface
- pip 批量完全卸载包
- Tp6 multi table Association (table a is associated with table B, table B is associated with table C, and table d)
- Numpy general function
- 35 year old programmer fired Luna millions of assets and returned to zero in three days. Netizen: it's the same as gambling
- 1.11 learning summary
猜你喜欢
OSS CDN alicloud configuration method
PIP batch complete uninstall package
Tp6 is easy to tread [original]
1.24 learning summary
A new paradigm for large model application: unified feature representation optimization (UFO)
Simple personal summary of tp6 multi application deployment -- Part I [original]
NPM installation tutorial
Realize video call and interactive live broadcast in the applet
Motivational skills for achieving goals
[H5 development] 01 take you to experience H5 development from a simple page ~ the whole page implementation process from static page to interface adjustment manual teaching
随机推荐
[Qunhui] command line acme SH automatically apply for domain name certificate
MySQL index details
[Qunhui] no port access (reverse proxy + intranet penetration)
Composer version rollback version switching
Laravel framework Alipay payment fails to receive asynchronous callback request [original]
Database design (3): database maintenance and optimization
Zhimeng CMS will file a lawsuit against infringing websites
Resolve PHP is not an internal or external command
PIP batch complete uninstall package
Navicat connects the pit of shardingsphere sub table and sub library plug-ins
Redis cache message queue
Install dbeaver and connect Clickhouse
MySQL enable logbin in Qunhui docker
I like you!
LeetCode 94. Middle order traversal of binary tree
PHP design function getmaxstr to find the longest symmetric string in a string - [original]
Rdkit chemical formula molecular formula search
1064 (42000) error occurred when installing MySQL and modifying root password
PHP has the problem of using strtotime to obtain time in months and months [original]
Yapi cross domain request plug-in installation