Daily summary --2019 / 7/18

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_43271086/article/details/96465090
#include <stdio.h>
#include<iostream>
using namespace std;
int main()
{
	int a[20001];//储存每一位所得到的数
	int temp,digit,n,i,j=0;//temp每次的得数   digit每次得数的位数
  while(cin>>n){
	a[0]=1;//从1开始乘
	digit=1;//位数从第一位开始
	for(i=2;i<=n;i++)
	{
		int num=0;
		for(j=0;j<digit;j++)
		{
			temp=a[j]*i+num;//将一个数的每一位数都分别乘以i,
			a[j]=temp%10;//将一个数的每一位数利用数组进行储存
			num=temp/10;
		}
		while(num)//判断退出循环后,num的值是否为0
		{
			a[digit]=num%10;//继续储存
			num=num/10;
			digit++;
		}
	}
	for(i=digit-1;i>=0;i--)//倒序输出每一位
		cout<<a[i];
	  cout<<endl;
}
	return 0;
}

Addition of large numbers (AC)

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int a[5000007], b[5000007], c[5000007], m, n;
int getm(char *str1, char *str2) {
  m = strlen(str1), n = strlen(str2);
  return m > n ? m : n;
}
int zhuanhua(char *str1, char *str2, int max) {
  memset(a, 0, sizeof(a));
  memset(b, 0, sizeof(b));
  memset(c, 0, sizeof(c));
  for(int i = 0; i < m; ++i)  a[m-i-1] = str1[i] - '0';
  for(int i = 0; i < n; ++i)  b[n-i-1] = str2[i] - '0';
}
void jiajia(int max) {
  for(int i = 0; i < max; ++i)
  c[i] = a[i] + b[i];
    for(int i = 0; i <=max; ++i) {
      c[i+1] += c[i] / 10;
          c[i] %= 10;
    }
}
char str1[5000007], str2[5000007];
int main() {
    while(cin>>str1>>str2){
    int max = getm(str1, str2);
    zhuanhua(str1, str2, max);
    jiajia(max);
    if(c[max] != 0) {
      for(int i = max; i >= 0; --i) cout<<c[i];
    }
    else {
      for(int i = max - 1;i >= 0; --i) cout<<c[i];
    }
    cout<<endl;
  }
}

Tarsus sword (AC)

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=100009;
char m[maxn],n[maxn];
int a[maxn],b[maxn],c[maxn];
int main(int argc, char const *argv[]) {
  while(cin>>m>>n){
    memset(a, 0, sizeof a);
    memset(b, 0, sizeof b);
    memset(c, 0, sizeof c);
    int k=0;
    int n1=0;
    int flag=0;//标记一下
    int length1=strlen(m);
    int length2=strlen(n);
    //最大位数
    if(length1>length2){
      k=length1;
    }
    else{
      k=length2;
    }
    //判断m,n的大小
    if(length1>length2){
      n1=1;
    }
    else if(length1==length2){
      n1=strcmp(m,n);
    }
    else{
      n1=-1;
    }
    //倒序输入到两个char数组中
    for(int i=length1-1, j=0;i>=0;i--,j++){
      a[j]=m[i]-'0';
    }
    for(int i=length2-1,j=0;i>=0;i--,j++){
      b[j]=n[i]-'0';
    }
    //开始模拟计算
    for(int i=0;i<=k;i++){
      if(n1>=0){
        if(a[i]-b[i]>=0)
          c[i]=a[i]-b[i];
        else{
          c[i]=a[i]+10-b[i];
          int j = i;
          while(a[j+1] == 0){
            a[j+1] = 9;
            j++;
          }
          a[j+1]--;
        }
      }
      else{
        if(b[i]-a[i]>=0){
          c[i]=b[i]-a[i];
        }
        else{
          c[i]=b[i]+10-a[i];
          int j = i;
          while(b[j+1] == 0){
            b[j+1] = 9;
            j++;
          }
          b[j+1]--;
        }
      }
    }
    if(n1<0){
      cout<<'-';
    }
    for(int i=k-1;i>=0;i--){
      if(c[i]){
        flag=1;
      }
      if(flag||i==0){
        cout<<c[i];
      }
    }
    cout<<endl;
  }
  return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_43271086/article/details/96465090
Recommended