【HDU】【简单数字问题集锦】


2031进制转换

一【题目描述】

二【题目样例】

三【解题思路】

1.如果是负数则变为正数,再加负号

2.逆序输出时一定注意是是i--。不要笔误

四【代码实现】

#include<iostream>
#include<cstdio>
#include<string.h>
#include<string>
#include<math.h>
using namespace std;
int main(){
	int n,r;
	char z[1000];
	while(~scanf("%d %d",&n,&r)){
		int temp=0;
		int num=0;
		int flag=0;
		if(n<0){
			n=abs(n);
			flag=1;
			}	
		do{
			temp=n%r; 
			if(temp<=9)z[num++]=temp+'0';	
			else if(temp==10) z[num++]='A';
			else if(temp==11) z[num++]='B';
			else if(temp==12) z[num++]='C';
			else if(temp==13) z[num++]='D'; 
			else if(temp==14) z[num++]='E';
			else if(temp==15) z[num++]='F';
	  		n=n/r;		
	       }while(n!=0)	;
	    if(flag==1) printf("-");
		for(int i=num-1;i>=0;i--) printf("%c",z[i]);
		printf("\n"); 
}
	
}

2033 A+B

2070  Fibbonacci Number

一【题目描述】

二【题目样例】

三【解题思路】

本题应该用数组一步步存储各个数的值,这样可以避免超时运算。常规计算40以下的数可以很快得出结果,但是上了50的数计算容易超时,导致通过不了

四【代码实现】

#include<cstdio>
int main(){
	long long f[51];
	f[0]=0;f[1]=1;
	for(int i=2;i<51;i++){
		f[i]=f[i-1]+f[i-2]; 
	} 
	int n;
	while(~scanf("%d",&n)&&n!=-1){
		printf("%d\n",f[n]);
	}

	
}

2089 不要62

一【题目描述】

二【题目样例】

三【解题思路】

说明:
1.itoa()函数有三个参数,第一个参数是要转换的数字,第二个参数是要写入转换结果的目标字符串,第三个参数是转移数字时所用的基数。
2.strstr(str1,str2)函数  用于char[ ]
str1: 被查找目标
str2: 要查找对象
返回值:若str2是str1的子串,则返回str2在str1的首次出现的地址;如果str2不是str1的子串,则返回NULL。
 

四【代码实现】

自己写的,由于这几天上不去OJ,提交不了,但肯定因为用遍历超时了。

#include<cstdio>
#include<iostream>
#include<string>
using namespace std;
//字符串处理 
int main(){
	int n,m;
	while(cin>>n>>m){
		if(n==0&&m==0) break;
		int sum=0;
		for(int i=n;i<=m;i++){
			string temp=to_string(i);
			int flag=0;
			for(int j=0;j<temp.size();j++){
				string s="";
				s+=temp[j];
				s+=temp[j+1];
				if(temp[j]=='4'||stoi(s)==62) {
					flag=1; break;
				}
			}
			if(flag==0) sum++;
		}
		cout<<sum<<endl; 
	}
	
	
	
	
	
	
	
	
	
}
正确的写法如下:
 

#include<cstdio>
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
//字符串处理 
int main(){
	int n,m;
	while(cin>>n>>m){
		if(n==0&&m==0) break;
		int sum=0;
		for(int i=n;i<=m;i++){
			char temp[10];
			itoa(i,temp,10); 
			if(strstr(temp,"62")==NULL&&strstr(temp,"4")==NULL) sum++; 
		}
		cout<<sum<<endl; 
	}
	
	
	
	
	
	
	
	
	
}

2097 Sky数

一【题目描述】

 二【题目样例】

 三【解题思路】

  • 自己做的时候,用的是字符串,且先输出数字,在进行加和,这样比较麻烦,需要进行B,A等字母的转换

  • 这里不用做A,B转换 不用输出该进制的形式,只需要单纯做和即可

  • 另外如果数字不是特别大的时候不用考虑字符串处理,字符串处理一般用在大数运算+进制转换时,这里我们就是简单的数学问题

四【代码实现】

#include<cstdio>
int jinzhi(int x,int k){
	int sum=0;
	do{
		sum+=x%k;
		x=x/k;
	}while(x!=0);
	return sum;
}
int main(){
	int n,s1,s2,s3;
	while(~scanf("%d",&n)&&n!=0){
		s1=jinzhi(n,10);
		s2=jinzhi(n,12);
		s3=jinzhi(n,16);
		if(s1==s2&&s2==s3)printf("%d is a Sky Number.\n",n);
        else  printf("%d is not a Sky Number.\n",n);
	}
	
	
} 

2099  整数的尾数

一【题目描述】

二【题目样例】

三【解题思路】

“%02d”  不足两位0补位的知识点忘记了

四【代码实现】

#include<iostream>
#include<cstdio>
using namespace std;
int main(){
	int a,b;
	while(cin>>a>>b){
		if(a==0&&b==0) break;
		int count=0;
		for(int i=a*100;i<=a*100+99;i++){
			if(i%b==0){
				int temp=i%100;
				if(count==0) {
					printf("%02d",temp);
					count++; 
				}
				else printf("% 02d",temp);
			}	
		}
		printf("\n");
	}
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值