当前位置:网站首页>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;
}
边栏推荐
- 虽然不一定最优秀,但一定是最努力的!
- Thrift threadmanager and three monitors
- Metal organic framework MOFs loaded with non steroidal anti-inflammatory drugs | zif-8 wrapped Prussian blue loaded quercetin (preparation method)
- 小项目(servelt+jsp+mysql+EL+JSTL)完成一个登录功能的Servlet,具有增删改查的操作。实现登录身份验证,防止非法登录,防止多点登录,记住用户名密码功能。
- Uniapp tips - set background music
- Doxorubicin loaded on metal organic framework MIL-88 DOX | folic acid modified uio-66-nh2 doxorubicin loaded [email
- 金属有机骨架MOFs装载非甾体类抗炎药物|ZIF-8包裹普鲁士蓝负载槲皮素(制备方法)
- Exercise 6-2 using functions to sum special A-string sequences
- 【吉林大学】考研初试复试资料分享
- How to delete an attribute or method of an object
猜你喜欢
Spring cup eight school league
[email "/>
Doxorubicin loaded on metal organic framework MIL-88 DOX | folic acid modified uio-66-nh2 doxorubicin loaded [email
Interface for querying IP home
7-9 find a small ball with a balance
JVM object lifecycle
7-10 calculate salary
Fabric. JS document
关于回溯问题中的排列问题的思考(LeetCode46题与47题)
Multi person collaborative data annotation based on Baidu brain easydata from scratch
Qt学习25 布局管理器(四)
随机推荐
Cross linked cyclodextrin metal organic framework loaded methotrexate slow-release particles | metal organic porous material uio-66 loaded with flavonoid glycosides | Qiyue
信创产业现状、分析与预测
Metal organic framework MOFs loaded with non steroidal anti-inflammatory drugs | zif-8 wrapped Prussian blue loaded quercetin (preparation method)
JS shift operators (< <,> > and > > >)
Qt学习18 登录对话框实例分析
JS new challenges
simpleParallax. JS (create poor visual effects for website pictures)
Invalid Z-index problem
The small project (servlet+jsp+mysql+el+jstl) completes a servlet with login function, with the operation of adding, deleting, modifying and querying. Realize login authentication, prevent illegal log
Thrift threadmanager and three monitors
Redis: operation command of string type data
Installation impression notes
交联环糊精金属有机骨架负载甲氨蝶呤缓释微粒|金属-有机多孔材料UiO-66负载黄酮苷类药物|齐岳
Uio-66-cooh loaded bendamostine | hydroxyapatite (HA) coated MIL-53 (FE) nanoparticles | baicalin loaded manganese based metal organic skeleton material
Current situation, analysis and prediction of information and innovation industry
concat和concat_ws()区别及group_concat()和repeat()函数的使用
Reflection -- basic usage
MySQL 数据增删改查综合案例
Message subscription and publishing
Solve MySQL 1045 access denied for user 'root' @ 'localhost' (using password: yes)