首页 > 试题广场 >

找整除

[编程题]找整除
  • 热度指数:3565 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
牛牛想在[a, b]区间内找到一些数满足可以被一个整数c整除,现在你需要帮助牛牛统计区间内一共有多少个这样的数满足条件?

输入描述:
首先输入两个整数a,b,(-5*10^8 ≤ a ≤ b ≤ 5*10^8) 接着是一个正整数c(1 <= c <= 1000)


输出描述:
输出一个整数表示个数。
示例1

输入

0 14 5

输出

3
python3--这种题,只想说呵呵,有意思么
a,b,c=map(int,input().split())
if a%c==0:
    print((b-a)//c+1)
else:
    print((b-(a//c+1)*c)//c+1)


发表于 2017-12-06 23:51:11 回复(0)
#include <iostream>
//#include <string>
//#include <vector>
using namespace std;

int Countnum(int a, int b, int c)
{
    int ans=0;
    if(a%c==0)
        ans++;
    if(b%c==0)
        ans++;
    a++;
    b--;
    if(b%c==0)
        ans++;
    int tmp=b-a;
    ans+=tmp/c;
    return ans;
}

int main()
{
    int a,b,c;
    cin>>a>>b>>c;
    cout<<Countnum(a,b,c);
    return 0;
}


发表于 2017-03-31 20:54:07 回复(1)
importjava.util.Scanner;
//在[a, b]区间内找到一些数满足可以被一个整数c整除,
publicclassMain {
    publicstaticvoidmain(String[] args){
        Scanner in = newScanner(System.in);
        while(in.hasNext()){
            intbegin = in.nextInt();
            intend = in.nextInt();
            intnum = in.nextInt();
            //int count = 0;
            //for(int i = begin; i <= end; i++){
            //  if(i % num == 0){
                //  count++;
                //}
            //}
            //System.out.println(count);
            while(begin % num != 0){
                begin++;
            }
            if(begin > end){
                System.out.println(0);
            }else{
                System.out.println((end - begin) / num + 1);
            }
        }
    }
}
分析:调整a到一个可以被c整除的位置,然后以c为步长计算就好了
发表于 2017-03-24 16:10:57 回复(4)
importjava.util.Scanner;
 
/**
 * Created by Administrator on 2017/6/12.
 */
publicclassMain {
 
    publicstaticvoidmain(String[] args) {
        Scanner sc = newScanner(System.in);
        String arr[] = sc.nextLine().split(" ");
        inta = Integer.parseInt(arr[0]);
        intb = Integer.parseInt(arr[1]);
        intc = Integer.parseInt(arr[2]);
        intcount = 0;
        for(;a <= b;a++){
            if(a % c == 0) {
                count = 1;
                break;
            }
        }
        System.out.println(count + (b-a)/c);
    }
}
发表于 2017-06-13 10:45:36 回复(0)
#include<iostream>
using namespace std;
int main(){
    int a,b,c;
    cin>>a>>b>>c;
    int t= b/c - a/c;
    if ( (a<=0&&b>=0)||(a%c==0&&a>=0)||(b%c==0&&b<=0) ) t++;
    cout<<t;
    return 0;      
}
编辑于 2017-07-25 17:12:58 回复(3)
import sys
 
try:
    while True:
        a,b,c = input().split()
        a = int(a)
        b = int(b)
        c = int(c)
        i0 = 0
        for i in range(a,b+1):
            if i%c == 0:
                i0 = i
                break
        n = 1 + (b-i0)//c
        print(n)
except:
    pass

发表于 2020-03-19 22:32:03 回复(0)
#include <iostream>
using namespace std;
int main()
{
    int a,b,c;
    cin>>a>>b>>c;
    b -= b%c;
    cout<<((b - a)/c + 1);
    return 0;
}
b -= b%c 是要处理 -3 12 5 这种场景,不掐头去尾的话,会多算一个

发表于 2019-12-23 16:24:44 回复(0)
import java.util.Scanner;
 public class Main
 {
 public static void main(String [] args){
 Scanner scanner = new Scanner(System.in);
 int min=scanner.nextInt();
 int max=scanner.nextInt();
 int div=scanner.nextInt();
 int count=0;
 //for(int i=min;i<=max;i++){
 //    if(i%div==0){
 //        count=(max-i)/div+1;
 //       break;
 //   }
 //}
 int b=min;
 while(b<=max){
 if(b%div==0){
 count=(max-b)/div+1;
 break;
 }
 b++;
 }
 System.out.println(count);
 scanner.close();
 }
 }

编辑于 2018-12-04 18:17:49 回复(0)
a,b,c=map(int,input().split())
suml,flag=0,0
ford inrange(a,b+1):
    ifd%c==0:
        flag=d
        break
print(1+(b-d)//c)
发表于 2018-07-19 00:51:38 回复(0)
#include<iostream>
using namespace std;

int find(int a, int b, int c)
{  if (b <= 0)  return find(-1 * b, -1 * a, c);  else  {  if (a < 0)  return find(0, b, c) + find(1, -1 * a, c);  else  {  int result = 0;  if (a == 0 || (a%c == 0 && b%c == 0))  result++;  if (c > b)  return result;  int tmp1 = a / c;  int tmp2 = b / c;  result += tmp2 - tmp1;  return result;  }  }
}

int main()
{  int a, b, c;  cin >> a >> b >> c;  cout << find(a, b, c);
    return 0;
}


编辑于 2018-06-08 19:53:11 回复(0)
//没有循环的方法,通过所有测试用例
//如果区间包含0需要单独加1
#include<iostream>
using namespace std;
int main(){
    int a, b, c;
    cin >> a >> b >> c;
    int count = b / c - a / c;
    if (b - a >= b) count++;
    cout << count << endl;
}
发表于 2017-11-03 10:22:01 回复(0)
//找到第一个,后面的也就都确定了,不用一直循环下去
#include<iostream>
using namespace std;
 
intmain()
{
    inta,b,c;
    while(cin>>a>>b>>c)
    {
        intcont=0;
        for(inti=a;i<=b;i++)
        {
            if(i%c==0)
            {
                cont=(b-i)/c + 1;
                break;
            }
        }
        cout<<cont<<endl;
    }
    return0;
}
发表于 2017-07-01 23:52:23 回复(0)
#include<iostream>
int main()
{
using namespace std;
int a, b;
cout << "Enter a section" << endl;
cin >> a >> b;
int c;
cout << "Enter a divisor" << endl;
cin >> c;
int count = 0;
for (int i = a; i <= b; i++)
{
if ((i%c) == 0)
count++;
}
cout << count << " numbers can be divided.";
system("pause");
}

发表于 2017-06-13 20:02:31 回复(0)
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>

int main(){
	using namespace std;
	int a, b, c;
	while(cin >> a >> b >> c){
		int ans = 0;
		int first;
		if(a % c)
			first = c - a % c + a;
		else 
			first = a;
		for(int i = first; i <= b; i = i + c)
			ans ++;
		cout << ans << endl;
	}
	return 0;
}

发表于 2017-04-14 11:26:28 回复(0)
#-*-coding:utf-8-*-
arr = map(int, raw_input().split(' '))
n = 0
for m in range(arr[0], (arr[1]+1)):
    if arr[2] != 0 and m % arr[2] == 0:
        n = n + 1
print n

您的代码已保存
内存超限:您的程序使用了超过限制的内存
case通过率为50.00%

本地测试妥妥的啊
编辑于 2017-04-11 11:30:38 回复(2)
importjava.util.*;
 
publicclassMain{
    publicstaticvoidmain(String[] args){
        Scanner sc =newScanner(System.in);
        longa = sc.nextInt();
        longb = sc.nextInt();
        longc = sc.nextInt();
        longres;
  //判断ab是否同号,分别解答
        if(a*b<=0) res = Math.abs(a)/c+Math.abs(b)/c+1;
        elseres = b/c-a/c;
        System.out.print(res);
    }
 

发表于 2017-04-09 01:40:07 回复(0)
1、找到左边的第一个可以整除的数
2、找到右边的第一个可以整除的数
3、sum = (右边的数减去左边的数)/c +1

intmain()
{
    inta,b,c;
    cin>>a>>b>>c;
    intt1=0;
    for(inti=a;i<=b;i++)
    {
        if(i%c ==0)
        {
            t1 = i/c;
            break;
        }
    }
    intt2=0;
    for(inti = b;i>=a;i--)
    {
        if(i%c ==0)
        {
            t2 = i/c;
            break;
        }
    }
 
    cout<<(t2-t1)+1<<endl;
     
    return0;
}

发表于 2017-04-08 19:42:06 回复(0)
#include<iostream>
using namespace std;
void getExactDivision(long long int a, long long int b, long int c)
{
 int num_an = 0;
 int num_bn = 0;
 num_bn = b / c;//求出倍数    
 num_an = a / c;
 if (a != 0)//判断是否为0    
 {
  if (b / a < 0)//说明 异号        
  {
   cout << (num_bn - num_an + 1) << endl;
  }
  else
   cout << (num_bn - num_an) << endl;;
 }
 else if (a == 0)
 {//等于0        
  cout << (num_bn - num_an + 1) << endl;
 }
}
 int main()
{
 long long int min_an, max_bn;
 cin >> min_an >> max_bn;//输入范围    
 long int num_cn;
 cin >> num_cn;
 //输入c    
 getExactDivision(min_an, max_bn, num_cn);
 return 0;
} 

发表于 2017-04-05 11:55:39 回复(0)
#include <iostream>
using namespace std;

int main()
{
	int n1, n2,n3;
	int count = 0;
	cin >> n1 >> n2 >> n3;
	if (n1 < 0 && n2 < 0)
		count = n2 / n3 - n1 / n3;
	if (n1 <= 0&&n2>=0)
		count = n2 / n3 - n1 / n3 + 1;
	if (n1 > 0 && n2 > 0)
		count = n2 / n3 - n1 / n3;
	cout << count;
	return 0;
}

发表于 2017-03-27 17:32:26 回复(0)
#include "stdio.h"
intmain()
{
   inta,b,c;
   while(scanf("%d%d%d",&a,&b,&c)!=EOF)
   {
      intnum1,num2,num;
      if(a<=0)
      {
          num1=(-a/c);
          num2=b/c;
          num=num1+num2+1;
      }
      else
      {
          num1=a/c;
          num2=b/c;
          num=num2-num1;
      }
       printf("%d\n",num);
       
   }
}

发表于 2017-03-25 12:46:31 回复(0)

热门推荐

通过挑战的用户

找整除