当前位置:网站首页>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;
}边栏推荐
- 金属有机骨架MOFs装载非甾体类抗炎药物|ZIF-8包裹普鲁士蓝负载槲皮素(制备方法)
- Scroll detection of the navigation bar enables the navigation bar to slide and fix with no content
- 信创产业现状、分析与预测
- QT learning 20 standard dialog box in QT (middle)
- Qt学习21 Qt 中的标准对话框(下)
- Exercise 6-1 classify and count the number of characters
- Vite project commissioning
- Uniapp skills - dom display and hiding
- page owner特性浅析
- [combinatorics] permutation and combination (two counting principles, examples of set permutation | examples of set permutation and circle permutation)
猜你喜欢

Qt学习24 布局管理器(三)

JVM class loading

Redis: operation command of string type data

Qt学习18 登录对话框实例分析

QT learning 19 standard dialog box in QT (top)

Solve MySQL 1045 access denied for user 'root' @ 'localhost' (using password: yes)

jvm-对象生命周期

Installation impression notes

Message subscription and publishing

关于回溯问题中的排列问题的思考(LeetCode46题与47题)
随机推荐
Metal organic framework (MOFs) antitumor drug carrier | pcn-223 loaded with metronidazole | uio-66 loaded with ciprofloxacin hydrochloride(
Global event bus
Exercise 9-3 plane vector addition
Installation impression notes
信创产业现状、分析与预测
Canvas utility library fabric JS user manual
使用vscode查看Hex或UTF-8编码
Metal organic framework material zif-8 containing curcumin( [email protected] Nanoparticles) | nano metal organic framework carry
QT learning 24 layout manager (III)
28:第三章:开发通行证服务:11:在配置文件中定义属性,然后在代码中去获取;
全局事件总线
How to bold text in AI
JS get DPI, PX to cm, cm to PX
jvm-运行时数据区
Exercise 8-7 string sorting
虽然不一定最优秀,但一定是最努力的!
Exercise 8-2 calculate the sum and difference of two numbers
Uio-66-cooh loaded bendamostine | hydroxyapatite (HA) coated MIL-53 (FE) nanoparticles | baicalin loaded manganese based metal organic skeleton material
Use and design of Muduo buffer class
Current situation, analysis and prediction of information and innovation industry