当前位置:网站首页>6-9 statistics of single digits (15 points)
6-9 statistics of single digits (15 points)
2022-07-03 14:10:00 【Big fish】
6-9 Statistics are in single digits (15 branch )
This question requests to realize a function , It can count the number of occurrences of a bit in any integer . for example -21252 in ,2 There is 3 Time , The function should return 3.
Function interface definition :
int Count_Digit ( const int N, const int D );
among N and D All parameters passed in by the user .N The value of is not more than int The scope of the ;
D yes [0, 9] Single digit in interval . Function must return N in D Number of occurrences .
sample input :
-21252 2
sample output :
3
Ideas
Pay attention to find the absolute value first , Otherwise, the remainder function cannot be calculated correctly
Sample referee test procedure :
#include <stdio.h>
int Count_Digit ( const int N, const int D );
int main()
{
int N, D;
scanf("%d %d", &N, &D);
printf("%d\n", Count_Digit(N, D));
return 0;
}
/* Your code will be embedded here */
Test point Tips result Time consuming Memory
0 sample, Numbers <0 And appear discontinuously The answer right 2 ms 384 KB
1 Numbers >0, Continuous occurrence The answer right 2 ms 256 KB
2 N=0, And output 1 The answer right 2 ms 280 KB
3 Output is 0 The answer right 2 ms 364 KB
4 Statistics 0 The number of times , There may be more cycles 1 The answer right 2 ms 256 KB
Before optimization
int Count_Digit ( const int N, const int D ){
int tmp;
int res;
int arr[10]={0};
if(N>=0){
res=N;
}else{
res=N*(-1);
}
if(res==D)return 1;// Handle N=0 And D=0 In special circumstances , By the way |N|==D The situation of
while(res!=0){
tmp=res%10;
if(D==tmp){
arr[tmp]++;
}
res=res/10;
}
for(int i=0;i<10;i++){
if(arr[i]>=1){
tmp=arr[i];
return tmp;
}
}
return 0;
}After optimization
int Count_Digit ( const int N, const int D ){
int temp=N;
if(N<0)temp*=-1;
if(temp==D)return 1;// Handle N=0 And D=0 In special circumstances , By the way |N|==D The situation of
int times=0;// Number of occurrences
for(int i=temp;i>0;i/=10){
if(i%10==D)times++;
}
return times;
}边栏推荐
猜你喜欢

FPGA测试方法以Mentor工具为例

全局事件总线

JS matrix zero

Scroll detection of the navigation bar enables the navigation bar to slide and fix with no content

Go: send the get request and parse the return JSON (go1.16.4)

28:第三章:开发通行证服务:11:在配置文件中定义属性,然后在代码中去获取;

信创产业现状、分析与预测

Using registered classes to realize specific type matching function template

小项目(servelt+jsp+mysql+EL+JSTL)完成一个登录功能的Servlet,具有增删改查的操作。实现登录身份验证,防止非法登录,防止多点登录,记住用户名密码功能。

Common network state detection and analysis tools
随机推荐
Rasp implementation of PHP
Failure of vector insertion element iterator in STL
JS new challenges
Why don't I have a rookie medal
FPGA test method takes mentor tool as an example
Go language web development series 29: Gin framework uses gin contrib / sessions library to manage sessions (based on cookies)
QT learning 22 layout manager (I)
selenium 浏览器(1)
【吉林大学】考研初试复试资料分享
js . Find the first palindrome string in the array
Qt学习22 布局管理器(一)
Exercise 6-2 using functions to sum special A-string sequences
Redis: commandes d'action pour les données de type chaîne
Collection of mobile adaptation related articles
关于回溯问题中的排列问题的思考(LeetCode46题与47题)
RocksDB LRUCache
Go: send the get request and parse the return JSON (go1.16.4)
Exercise 8-7 string sorting
jvm-对象生命周期
Message subscription and publishing