当前位置:网站首页>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;
}边栏推荐
- #微信小程序# 在小程序里面退出退出小程序(navigator以及API--wx.exitMiniProgram)
- TP5 distinct method paging problem
- Performance test comparison between PHP framework jsnpp and thinkphp6
- redis集群的方式
- [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
- Group by and order by are used together
- PHP has the problem of using strtotime to obtain time in months and months [original]
- SixTool-多功能多合一代挂助手源码
- Navicat connects the pit of shardingsphere sub table and sub library plug-ins
- 2.8 learning summary
猜你喜欢

Motivational skills for achieving goals

Realize video call and interactive live broadcast in the applet

Mysql8.0 configuring my SQL in INI file_ mode=NO_ AUTO_ CREATE_ User can start

Navicat connects the pit of shardingsphere sub table and sub library plug-ins
![Alipay failed to verify the signature (sandbox test indicates fishing risk?) [original]](/img/64/c3bb27a3711a6f0cc7b281d1a961af.jpg)
Alipay failed to verify the signature (sandbox test indicates fishing risk?) [original]

PHP small factory moves bricks for three years - interview series - my programming life

NVM installation and use and NPM package installation failure record

NPM installation tutorial

Construction of art NFT trading platform | NFT mall

1.17 learning summary
随机推荐
What are the advantages and risks of paper gold investment
Physical design of database design (2)
redis集群的方式
1.16 learning summary
2020-12-18
[Qunhui] Internet access + custom port
Database design (3): database maintenance and optimization
I like you!
Laravel framework Alipay payment fails to receive asynchronous callback request [original]
[Qunhui] command line acme SH automatically apply for domain name certificate
Mobile terminal pull-down loading pull-down loading data
Nightmare
2021/11/6-burpsuit packet capturing and web page source code modification
08_SpingBoot 集成Redis
Review of number theory
08_ Spingboot integrated redis
Microsoft prohibits Russian users from downloading and installing win10/11
Resolve PHP is not an internal or external command
Construction of art NFT trading platform | NFT mall
Install SVN in Pagoda and build SVN version Library