大数乘小数

大数乘小数

代码1:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

string mul(string a,int num)
{
  if(num==0)
    return 0;
  string c;
  reverse(a.begin(),a.end());
  int i=0,k=0;
  while(a[i])
  {
    int m=(a[i]-'0')*num+k;
    c+=m%10+'0';
    k=m/10;
    i++;
  }
  while(k!=0)
  {
    c=c+(char)(k%10+'0');
    k/=10;
  }
  reverse(c.begin(),c.end());
  return c;
}

int main()
{
  string c;
  int num;
  cin>>c>>num;
  cout<<mul(c,num)<<endl;
  return 0;
}


代码2:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;

string mul(string a,int k)
{
  if(k==0)
    return 0;
  int len=a.size(),carry=0;
  reverse(a.begin(),a.end());
  for(int i=0;i<len;i++)
  {
    int s=(a[i]-'0')*k+carry;
    a[i]=s%10+'0';
    carry=s/10;
  }
  while(carry!=0)
  {
    a=a+(char)(carry%10+'0');
    carry/=10;
  }
  reverse(a.begin(),a.end());
  return a;
}

int main()
{
  string a;
  int b;
  cin>>a>>b;
  cout<<mul(a,b)<<endl;
  return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于Java语言开发的小学生数学练习程序,支持100以内的加、减、乘、除运算: ```java import java.util.Random; import java.util.Scanner; public class MathPractice { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Random random = new Random(); int correctCount = 0; int totalCount = 0; while (true) { int a = random.nextInt(100) + 1; int b = random.nextInt(100) + 1; int operator = random.nextInt(4); String opStr; int result; switch (operator) { case 0: // 加法 opStr = "+"; result = a + b; break; case 1: // 减法 if (a < b) { int temp = a; a = b; b = temp; } opStr = "-"; result = a - b; break; case 2: // 乘法 opStr = "x"; result = a * b; break; case 3: // 除法 if (b == 1) { continue; // 除数不能为1 } int quotient = a / b; if (quotient * b != a) { continue; // 不能整除 } opStr = "/"; result = quotient; break; default: throw new IllegalStateException("Unexpected value: " + operator); } System.out.printf("%d %s %d = ", a, opStr, b); int answer = scanner.nextInt(); if (answer == result) { System.out.println("回答正确!"); correctCount++; } else { System.out.println("回答错误!"); } totalCount++; System.out.printf("你已经回答了%d题,其中%d题回答正确。\n", totalCount, correctCount); System.out.println("是否继续?(y/n)"); String continueStr = scanner.next(); if (!"y".equalsIgnoreCase(continueStr)) { break; } } System.out.printf("你一共回答了%d题,其中%d题回答正确。", totalCount, correctCount); } } ``` 程序首先通过 `Scanner` 和 `Random` 类创建一个输入和一个随机数生成器。然后程序进入一个循环,每次生成两个随机数和一个运算符,根据运算符计算出正确答案,然后提示用户输入答案,根据用户输入和正确答案进行比较,输出回答正确或回答错误的提示信息,并记录回答正确的题目数和总题目数。然后询问用户是否继续做题,如果用户输入的是 `y`,则继续下一轮循环,否则退出循环,输出总题目数和回答正确的题目数。在程序中,除法运算需要特别处理,保证被除数和除数都在100以内,且能够整除,除数不能为1。减法运算需要保证大数小数,因此如果a小于b,需要交换a和b的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值