列表

详情


NC25108. [USACO 2006 Ope l]Round Numbers

描述

Humans use the decimal system (some say because they have ten fingers), and when writing out large numbers, they arrange digits in triples, as in 1,234,567 instead of 1234567. Many humans prefer rounded numbers like 1,234,000 or 1,000,000, which end in a number of zeros that is a multiple of three.
Cows use the binary system (some say because they have two front hooves), and when writing out large numbers, they arrange digits in fours, as in 1,0101,1001 instead of 101011001. Many cows prefer rounded numbers like 1,0101,0000 or 1,0000,0000, which end in a number of zeros that is a multiple of four.
FJ, a human wise in the ways of cows, wants to assign each of his cows a number that is pleasing to both him and the cows. Given an input number N in the range 1..2,000,000,000, compute both the number of trailing decimal zeros and the number of trailing binary zeros.

输入描述

Line 1: A single integer, N

输出描述

Line 1: Two space-separated integers: the number of trailing decimal zeros and the number of trailing binary zeros.

示例1

输入:

8000

输出:

3 6

说明:

The number is 8,000 decimal which is 1,1111,0100,0000 in cow-binary.
The number 8,000 has three trailing zeros in 8,000. The binary representation has six trailing zeros.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C(clang 3.9) 解法, 执行用时: 2ms, 内存消耗: 372K, 提交时间: 2020-09-05 18:45:46

#include<stdio.h>
int main()
{
    int N,b=0,i=0,a;
    scanf("%d",&N);
    a=N;
   
    while(a%10==0){
        a=a/10;
        i++;
    }
     printf("%d ",i);
     while(N%2==0){
        b++;
        N=N/2;
    }
    printf("%d",b);
    
}

Python3 解法, 执行用时: 17ms, 内存消耗: 2820K, 提交时间: 2021-05-19 15:11:29

n=int(input())
m=n
ans1=ans2=0
while(n):
    if(n%10!=0):
        break
    n//=10
    ans1+=1
while(m):
    if(m%2!=0):
        break
    m//=2
    ans2+=1
print(ans1,ans2)

C++11(clang++ 3.9) 解法, 执行用时: 3ms, 内存消耗: 376K, 提交时间: 2020-09-05 18:20:20

#include<stdio.h>
int main()
{
	int N,i,j;
	scanf("%d",&N);
	for(i=0;N%10==0;i++)
	{
		N=N/10;
	}
	for(j=i;N%2==0;j++)
	{
		N=N/2;
	}
	printf("%d %d",i,j);
	
}

上一题