CodeForces 9C -Hexadecimal's Numbers-构造
2017-08-02 23:34
483 查看
问题描述:
One beautiful July morning a terrible thing happened in Mainframe: a mean virus Megabyte somehow got access to the memory of his not less mean sister Hexadecimal. He loaded there a huge amount of n different natural numbers from 1 to n to obtain total control over her energy.
But his plan failed. The reason for this was very simple: Hexadecimal didn’t perceive any information, apart from numbers written in binary format. This means that if a number in a decimal representation contained characters apart from 0 and 1, it was not stored in the memory. Now Megabyte wants to know, how many numbers were loaded successfully.
输入:
Input data contains the only number n (1 ≤ n ≤ 109).
输出:
Output the only number — answer to the problem.
样例:
Input
10
Output
2
题目大意:
一个整数n,求1到n这n个数中有几个数是由1和0组成的
AC代码:
解决方法:
由0和1想到二进制,于是构造一个二进制数,如10001来代表十进制数一万零一,从一开始,这个二进制数每加1所得到的新二进制数都是满足题意的。
又想到利用高精度的思想去实现这个二进制数。
PS:在C语言某个程序当中需要把文本16进制转换成对应的16进制数,比如字符串”0x1a”转换成10进制的26,可以用以下函数来实现
相关函数: atof, atoi, atol, strtod, strtoul
表头文件: #include
输出:
x = 100
y = 4
z = 17
One beautiful July morning a terrible thing happened in Mainframe: a mean virus Megabyte somehow got access to the memory of his not less mean sister Hexadecimal. He loaded there a huge amount of n different natural numbers from 1 to n to obtain total control over her energy.
But his plan failed. The reason for this was very simple: Hexadecimal didn’t perceive any information, apart from numbers written in binary format. This means that if a number in a decimal representation contained characters apart from 0 and 1, it was not stored in the memory. Now Megabyte wants to know, how many numbers were loaded successfully.
输入:
Input data contains the only number n (1 ≤ n ≤ 109).
输出:
Output the only number — answer to the problem.
样例:
Input
10
Output
2
题目大意:
一个整数n,求1到n这n个数中有几个数是由1和0组成的
AC代码:
int bit[100];//用整形数组存放二进制数 char bitt[100]; int main() { int n,i,j,sum=1,len; scanf("%d",&n);//输入 bit[0]=1;//初始化,从1开始,bit[0]储存这个二进制数的位数 bit[1]=1; while(1) { bit[1]++;//给这个二进制数加1 sum++;//情况数加1 //维护这个二进制数 for(i=1;i<=bit[0];i++) if(bit[i]==2) { bit[i]=0; bit[i+1]++; } else break; //求出当前二进制数的位数 for(i=20;i>=1;i--)//倒着循环 if(bit[i]==1) { bit[0]=i; break; } len=int(log10(n))+1;//len为十进制数n的位数 j=0;//数组下标 //如果二进制的位数大于了n的位数-1,就开始判断是否得出了解 if(bit[0]>=len-1) { for(i=bit[0];i>=1;i--)//将二进制数从int数组转为char数组,因为需要使用strtol函数 { bitt[j]=bit[i]+'0'; j++; } if(strtol(bitt,NULL,10)>n)//如果当前的二进制数大于了n { sum--;//情况数减1 printf("%d\n",sum);//打印 return 0; } } } }
解决方法:
由0和1想到二进制,于是构造一个二进制数,如10001来代表十进制数一万零一,从一开始,这个二进制数每加1所得到的新二进制数都是满足题意的。
又想到利用高精度的思想去实现这个二进制数。
PS:在C语言某个程序当中需要把文本16进制转换成对应的16进制数,比如字符串”0x1a”转换成10进制的26,可以用以下函数来实现
相关函数: atof, atoi, atol, strtod, strtoul
表头文件: #include
int main() { char a[] = "100"; char b[] = "100"; char c[] = "0x11"; int x, y, z; x = strtol( a, NULL, 10 ); y = strtol( b, NULL, 2 ); z = strtol( c, NULL, 16 ); printf( "x = %d\n", x ); printf( "y = %d\n", y ); printf( "z = %d\n", z ); }
输出:
x = 100
y = 4
z = 17
相关文章推荐
- CodeForces 9C - Hexadecimal's Numbers
- 【打CF,学算法——三星级】Codeforces 9C Hexadecimal's Numbers (解法汇总)
- CodeForces 9C Hexadecimal's Numbers(DFS)
- CF 9C Hexadecimal's Numbers
- Codeforces 9C Hexadecimal‘s number (DFS)
- codeforces 9C (DFS)之 Hexadecimal's Numbers
- CodeForces 9C Hexadecimal's Numbers
- 9C Hexadecimal's Numbers
- Codeforces-9C-Hexadecimal's Numbers
- codeforce 9C Hexadecimal's Numbers
- Codeforces 827B. High Load 【构造】
- CodeForces 828C【构造】
- Codeforces 246A Buggy Sorting 【构造】
- CodeForces 22C System Administrator 小水怡情 图论+构造
- Codeforces 715A. Plus and Square Root[数学构造]
- Codeforces 276D Little Girl and Maximum XOR 贪心+构造
- Codeforces 453C Little Pony and Summer Sun Celebration(构造)
- Codeforces 550D. Regular Bridge 构造
- codeforces 549B B. Looksery Party(构造)
- codeforce 9C(Hexadecimal's Numbers)