首页 > 试题广场 >

A除以B (20)

[编程题]A除以B (20)
  • 热度指数:29230 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数。你需要输出商数Q和余数R,使得A = B * Q + R成立。

输入描述:
输入在1行中依次给出A和B,中间以1空格分隔。


输出描述:
在1行中依次输出Q和R,中间以1空格分隔。
示例1

输入

123456789050987654321 7

输出

17636684150141093474 3
#include<iostream>
using namespace std;

class Bign
{
public:
	int A[1000];
	int len = 0;
};

//高精度与低精度的除法(rem:余数)
Bign divide(Bign& bign, int& B, int& rem)
{
	Bign res;

	for (int i = 0; i < bign.len; i++)
	{
		rem = rem * 10 + bign.A[i];
		res.A[i] = rem / B;
		res.len++;
		rem %= B;
	}
	return res;
}

int main()
{
	string A;
	int B;
	Bign bign;
	cin >> A >> B;
	for (int i = 0; i < A.size(); i++)
	{
		bign.A[i] = A.at(i) - '0';
	}
	bign.len = A.size();

	Bign res;
	int rem = 0;
	res = divide(bign, B, rem);

	if (res.A[0] == 0 && res.len > 1)
	{
		for (int i = 1; i < res.len; i++)
		{
			cout << res.A[i];
		}
	}
	else
	{
		for (int i = 0; i < res.len; i++)
		{
			cout << res.A[i];
		}
	}

	cout << " " << rem;
}

发表于 2020-09-28 12:07:57 回复(0)
更多回答
#include<iostream>
#include<string>
using namespace std;
int main(){
    string a;
    int b;
    cin>>a>>b;
    int yu = a[0] - '0';
    for(int i = 1;i<a.size();i++){
        int tem = yu*10+ (a[i] - '0');
        cout<<tem/b;
        yu = tem %b;
    }
    cout<<" "<<yu;
    return 0;
}

编辑于 2019-10-08 21:58:39 回复(3)
#include<iostream>
#include<string>
using namespace std;
int main(){
    string str,ans;
    int n,d = 0; 
    cin >> str >> n;
    for(int i = 0; i <= str.size()-1; i++){
        int current = d * 10 + (str[i]-'0');
        ans += (current / n+'0');
        d = current % n;
    }
    for(int i = (ans[0] == '0' && ans.size()!=1)?1:0; i < ans.size(); i++)
        cout << ans[i];
    cout << " " << d;
    return 0;
}
比一比谁的短:)

发表于 2015-09-20 20:40:55 回复(12)
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String A = in.next();
		int B = in.nextInt();
		StringBuilder Q = new StringBuilder();
		int r = 0;
		for(int i = 0;i<A.length();i++){
			int num = r*10+A.charAt(i)-'0';
			int result = 0;
			if(num>=B||i+1==A.length()){
				result = num/B;
				r = num%B;
			}else{
				if(i!=0)
					Q.append(0);
				num = num*10+A.charAt(++i)-'0';
				result = num/B;
				r = num%B;
			}
			Q.append(result);
		}
		System.out.println(Q.toString()+" "+r);
	}
}

发表于 2016-06-07 22:36:55 回复(0)
//使用java自带的大数类BigInteger =_=

import java.math.BigInteger;
import java.util.Scanner;

public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
BigInteger bi1 = input.nextBigInteger();
BigInteger bi2 = input.nextBigInteger();
System.out.println (bi1.divide(bi2)+" "+bi1.remainder(bi2));  
}
}

发表于 2016-08-02 06:45:51 回复(2)
基本思想是把除法转换为求商过程的逆转,比如 100/2 等价于 1/2 商为0,余数为1,商为0的情况下不能输输出, 然后余数和下一位 即0 组合为1*10+0=10,就变成10/2 商为5余数为0,此时输出商数。然后0和下一位0组合为0*10+0=0 0/2余数为0,此时运算完毕。
代码如下所示:
  static void Main(string[] args)
        {
            string line = null;
            /*基本思想是把除法转换为求商过程的逆转,比如 100/2 等价于 1/2 商为0,余数为1,商为0的情况下不能输输出, 然后余数和下一位 即0 组合为1*10+0=10,就变成10/2 商为5余数为0,此时输出商数。然后0和下一位0组合为0*10+0=0 0/2余数为0,
此时运算完毕  */
            while ((line = Console.ReadLine()) != null)
            {
                int quotient = 0;//商数
                int remainder = 0;//余数
                string[] tokens = line.Split();
                string a = tokens[0];
                int b = int.Parse(tokens[1]);
                for (int i = 0; i < a.Length; i++)
                {
                    remainder = remainder * 10 + a[i] - 48;
                    if (remainder >= b)
                    {
                        Console.Write(remainder / b);
                        quotient = 1;
                    }
                    else if (quotient == 1)
                        Console.Write(0);
                    remainder = remainder % b;
                }
                if (quotient == 0)
                    Console.Write(0);
                Console.Write(" " + remainder);
            }
}

发表于 2015-05-28 09:07:20 回复(2)
tmp = raw_input().split(' ')
a = int(tmp[0])
b = int(tmp[1])
q = a/b
r = a%b
print q,r
发表于 2017-04-07 23:40:55 回复(0)
#include<stdio.h>
#include<string.h>
int main(){
 char a[1000];
 int c,b,d,i,e[1000],t;
 scanf("%s\t%d",a,&b);
 t=strlen(a);
 for(i=0;i<t;i++){
  
   c=((a[i]-'0')*10+(a[i+1]-'0'))/b;
   d=((a[i]-'0')*10+(a[i+1]-'0'))%b;
   e[i]=c;
   a[i+1]=d+'0';
   if(i==t-2)
   break;
 }
 for(i=0;i<t-1;i++)
 printf("%d",e[i]);
 printf(" ");
 printf("%d\n",d);
 return 0;
}
发表于 2016-12-24 22:05:00 回复(0)
Python:来来来,都让开,我要装逼了
try:
    while True:
       t = [int(i) for i in raw_input().split()]
       print t[0]/t[1], t[0]%t[1]
except EOFError:
    pass
人生苦短,我用Python
发表于 2016-10-29 20:53:46 回复(1)
#include <cstdio>
#include <cstring>

int main()
{
    char A[1001],B[1001];
    int a,b=0,c=0,j=0;
    scanf("%s %d",A,&a);
    for(int i=0;A[i]!='\0';i++){
        c = b*10;
        B[j] = (A[i]-48+c)/a+48;
        b = (A[i]-48+c)%a;
        j++;
    }
    B[j] = '\0';
    if(B[0]=='0')
        printf("%s %d",B+1,b);
    else
        printf("%s %d",B,b);

    return 0;
}
很惭愧 
发表于 2018-02-21 21:37:51 回复(0)
#include<iostream>
#include<string>
using namespace std;
//PAT乙级真题A除以B (20)
intmain()
{
    string A,Q;
    intB,R;
    cin>>A>>B;
    intlength = A.length();
    //special case A<B
    if(length==1&& (A[0]-'0')<B)
        cout<<"0 "<<A[0]-'0';
    //normal case caculate the divide by using the string
    inti,tmp = A[0]-'0';
    if(tmp>B)//the first bit
        Q.push_back(tmp/B+'0');
    for(i=1;i<length;i++)
    {
        R = tmp%B;
        tmp = R*10+(A[i]-'0');
        Q.push_back(tmp/B+'0');
    }
    R = tmp%B;
    cout<<Q<<" "<<R;
}

发表于 2017-01-15 16:57:43 回复(0)
#include<iostream>

using namespace std;

int main()
{
	string s;
	cin>>s;
	int n;
	cin>>n;
	int len = s.size();
	int t = 0;
	for(int i = 0; i < len; i++)
	{
		t = t*10 + s[i] - '0'; 
		if(t >= n)
		{
			cout<<t/n;
			t = t-n*(t/n);
		}
		else if(i != 0)
		{
			cout<<"0";
		}
	}
	cout<<" "<<t;
} 

发表于 2021-09-02 13:09:53 回复(0)
#include<iostream>
#include<string>
using namespace std;

int main(){
	string stra;
	int b;
	cin >> stra >> b;
	int mode=0;
	for(int i=0;i<stra.size();i++){
                //从高位到低位计算每一位除法所得结果
		int temp = stra[i] - '0' + 10 * mode;
                //避免第一位所得为0
		if(i==0 && temp/b == 0) mode = temp;
		else {
			cout << temp/b;
			mode = temp % b;
		}
	}
	cout << " " <<mode; 
	return 0;
}
编辑于 2020-08-10 21:40:10 回复(0)
#include<iostream>
using namespace std;
int main()
{
	string a;
	int b;
	cin>>a>>b;
	int len=a.length();
	//做除法是从高位开始计算
	int t=0;
	string res;
	for(int i=0;i<len;i++){
		int x=a[i]-'0';
		t=t*10+x;
		if(i!=0||t/b!=0)
			res+=to_string(t/b);
		t%=b;
	}
	cout<<res<<" "<<t;
	return 0;
}

发表于 2020-05-16 10:35:10 回复(0)
  • 当余数为零,应该输出0 R;商的第一位为0不用输出。
    /*
    * app=PAT-Basic lang=c++
    * https://pintia.cn/problem-sets/994805260223102976/problems/994805305181847552
    */
    #include <cstdio>
    #include <cstring>
    using namespace std;
    const int maxn = 1010;
    int A[maxn] = {};
    int Q[maxn] = {};
    int main()
    {
      int B, R = 0;
      char ch[1010];
      scanf("%s%d",ch,&B);
      int len = strlen(ch);
      for (int i = 0; i < len; i++){
          A[i] = ch[i] - '0';
      }
      int j = 0;
      for (int i = 0; i < len; i++){
          R *= 10;
          R += A[i];
          Q[j++] = R / B;
          R %= B;
      }
      if (j == 1 && Q[j] == 0){
          printf("0 %d",R);
          return 0;
      }
      bool flag = false;
      for (int i = 0; i < j; i++){
          if (!flag && Q[i] == 0){
              continue;
          }
          else{
              printf("%d", Q[i]);
              flag = true;
          }
      }
      printf(" %d",R);
      return 0;
    }
发表于 2019-12-03 15:50:10 回复(0)
即用我们生活的除法用程序来模拟计算
// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#pragma warning(disable:4996);
#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<stack>
using namespace std;
int main()
{
	char a[1005];
	int b;
	cin >> a >> b;
	int len = strlen(a);
	int c = 10;//权值;
	int ans = 0;//保存前一位留下的数,用作后面的除法
	char q[1000];
	int t = 0;//用来存放商
	int x;//用来记录每次读取的数;
	if (strcmp(a, "0") == 0) cout << "0";//除数为0
	for (int i = 0;i < len;i++)
	{
		 x = a[i] - '0';
		ans += x;
		if(i!=0||(i==0&& (ans/b)!=0)) q[t++] = (ans/b) + '0';
		if (ans % b == 0) { ans = 0;continue; }
		else {
			ans = (ans % b) * c;
		}
	}
	for (int i = 0;i < t;i++) cout << q[i];
	cout << " " << ans/c;
	return 0;
}





发表于 2019-11-11 16:06:11 回复(0)
java中math包中BigInteger类即可
import java.math.BigInteger;
import java.util.*;

public class Mian
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        BigInteger num1=sc.nextBigInteger();
        BigInteger num2=sc.nextBigInteger();
        System.out.print(num1.divide(num2)+" "+num1.mod(num2));
    }
}


发表于 2019-10-21 19:59:58 回复(0)
import java.util.Scanner;
public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);
        String b= sc.next();//被除数
        int c=sc.nextInt();//除数
        int bcs[]=new int[b.length()] ;//接收与新被除数
        String s="";
        for (int i = 0; i < b.length(); i++)
        {
         bcs [i]= Integer.parseInt(b.charAt(i)+"");
        }
        int sang[]=new int[b.length()];//手算法中的商
        int y=0;//余数
        int xbc=bcs[0];//新被除数
        for(int i=0,j=1;i<b.length();i++)
        {
            
                sang[i]=xbc/c;
                y=xbc%c;
                if(j<b.length())
                {
                xbc=y*10+bcs[j];
                j++;
                }
        }
        if(sang[0]==0)
        {
            for(int i=1;i<b.length();i++)
            {
                s+=String.valueOf(sang[i]);
            }
        }
        else
        {
            for(int i=0;i<b.length();i++)
            {
                s+=String.valueOf(sang[i]);
            }
        }
        System.out.print(s+" "+y);
        
    }

}

发表于 2019-06-07 13:14:28 回复(0)
//123456789050987654321 7
//17636684150141093474 3
#include<iostream>
#include<sstream>
#include<cstring>
using namespace std;
int main()
{
    
    int k;//一位 
    int tmp;
    string s;
    string str;
    string chu="";
    char yu;
    cin>>s;cin>>k;
    
    if(s.size()<=2)
    {
        stringstream ss;
        ss<<s;
        ss>>tmp;
        cout<<tmp/k<<tmp%k<<endl;
        return 0;
    }
    int i;
    if(s[0]<k+'0')
        {
        str=s.substr(0,2);
        i=2;
        }
    else 
    {
        str=s[0];
        i=1;
    }
    for(;i<=s.size();)
    {
        stringstream ss;
        ss<<str;
        ss>>tmp;
        
        chu+=(tmp/k)+'0';
        yu=(tmp%k)+'0';
        //cout<<yu<<endl;
        str=yu;
        str+=s[i];
        i++;
    }
    cout<<chu<<' '<<yu<<endl;
    
}

发表于 2019-03-25 20:54:58 回复(0)
很坑的一点是,牛客的C++编译器不认识<String>头文件,只认识<string>头文件,导致提交错了两次。
#include <iostream>
#include <string>

using namespace std;

int main(){     string dividends = " ", ans = "";     int divisor = 1, length = 0, subDividends = 0;     int i = 0;          cin >> dividends >> divisor;          length = dividends.length();          int *array = new int[length];          for(i = 0;i < length;i++){         array[i] = dividends[i]-'0';     }          int quotient = 0, remainder = 0;     for(i = 0;i < length;i++){         subDividends = remainder*10 + array[i];         quotient = subDividends/divisor;         if(!(i==0&&quotient==0))             ans += quotient + '0';         remainder = subDividends%divisor;     }          cout << ans << " " << remainder <<endl;     delete array;     return 0;
} 

发表于 2017-12-10 21:17:13 回复(0)
//用正常的手算步骤代码跑一遍就可以啦
#include<iostream>
using namespace std;
int main()
{
    string A;
    int B,Y = 0;//Y为余数
    cin >> A >> B;
    int temp = 10*(A[0]-'0')+(A[1]-'0');//先用前两位去除
    for(int i = 0; i < A.size()-1; i++)
    {
        int S = temp/B;//S为商
        Y = temp%B;//Y为余数
        temp = 10*Y + (A[i+2]-'0');//temp看演算纸
        cout << S;
    }
    cout << " " << Y;
    return 0;
}
编辑于 2017-05-15 16:48:55 回复(3)

问题信息

难度:
168条回答 25008浏览

热门推荐

通过挑战的用户