当前位置:网站首页>Openjudge noi 1.13 46: octal to decimal
Openjudge noi 1.13 46: octal to decimal
2022-06-22 02:26:00 【Junyi_ noip】
【 Topic link 】
OpenJudge NOI 1.13 46: Octal to decimal
【 Topic test site 】
1. Number system
2. Input and output x Base string
- Use scanf or printf
| Base number | Format control specifier |
|---|---|
| Decimal system | %d |
| octal | %o |
| Hexadecimal | %x |
example : Enter octal digits to ascanf("%o", &a);
- Use cin or cout
| Base number | specifier |
|---|---|
| Decimal system | dec |
| octal | oct |
| Hexadecimal | hex |
example : Enter octal digits to acin >> oct >> a
【 Their thinking 】
solution 1: Number conversion
Basic number system conversion , To convert an original number into a value through a combination of numbers , Then the value is divided into digits under the target base , Get the digit string of the target base .
solution 2:( Clever ) Input and output x Base string
To borrow scanf/printf or cin/cout Specific methods in , Can handle decimal 、 octal 、 Conversion of hexadecimal digits .
This is an ingenious method , Not a general method . Just to understand .
【 Solution code 】
solution 1:
- C++ style
#include <bits/stdc++.h>
using namespace std;
int main()
{
int val = 0;
string s;
cin >> s;
for(int i = 0; i < s.length(); ++i)
val = val * 8 + s[i] - '0';
cout << val;
return 0;
}
- C style
#include <bits/stdc++.h>
using namespace std;
int toVal(char s[], int k)//k Base string s Convert to value
{
int len = strlen(s), val = 0;
for(int i = 0; i < len; ++i)
val = val * k + s[i] - '0';
return val;
}
int main()
{
char s[20];
scanf("%s", s);
printf("%d", toVal(s, 8));// Direct output value , It's the decimal system
return 0;
}
solution 2: Input and output x Base string
- Use scanf/printf
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a;
scanf("%o", &a);
printf("%d", a);
return 0;
}
- Use cin/cout
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a;
cin >> oct >> a;
cout << a;
return 0;
}
边栏推荐
- GoLand启动时闪退
- A short video costs hundreds of thousands of yuan, and the virtual digital man makes a circle with "strength"
- Chapter 18 build a general video processing tool based on GUI matlab application GUI implementation
- With the acceleration of industry wide digital transformation, what kind of storage will be more popular?
- [essay] the Expo that studied the RN ecology for one day yesterday is really awesome. It works well from development and construction to deployment.
- Atguigu---- filter
- WinForm project console debugging mode
- OpenJudge NOI 1.13 46:八进制到十进制
- rt_thread线程管理
- Wechat applet film and television comment exchange platform system graduation design completion (6) opening defense ppt
猜你喜欢

On Monday, I asked the meaning of the | -leaf attribute?

Review of mathematical knowledge: triple integral

Android使用SQL数据库进行登录功能时报错Attempt to invoke virtual method ' ' on a null object reference

Efficient packet processing system based on dpdk

Completion of graduation design of wechat small program film and television review and exchange platform system (4) opening report

What is a neural network

1277_ Implementation analysis of vtaskdelay in FreeRTOS

The "cloud" end of the 6th world intelligence conference will be held soon

An error occurs when Android uses the SQL database to login. Attempt to invoke virtual method '' on a null object reference

EMC Radiation Emission rectification - principle Case Analysis
随机推荐
Wechat applet film and television comment exchange platform system graduation design (3) background function
[essay] the Expo that studied the RN ecology for one day yesterday is really awesome. It works well from development and construction to deployment.
In the era of industrial Internet, there is no real center
atguigu----收集表单数据
What does the maturity and redemption time of financial products mean?
微信小程序影視評論交流平臺系統畢業設計畢設(4)開題報告
Array common methods
2022钎焊考试模拟100题及答案
Wechat applet film and television review and exchange platform system graduation design (1) development outline
Paper notes: multi label learning ackel
EMC rectification tips
How to obtain the comment information of tables and columns in gbase8a database?
idea----bookmark
postgresql根据时间字段的大小来取数
Chapter 21 design of pavement crack detection and identification system -- matlab deep learning practice
EMC Radiation Emission rectification - principle Case Analysis
Chrome browser cancel input box to record form input history
[Chapter 26 medical impact segmentation system based on minimum error method and region growth -- matlab deep learning practical GUI project]
Leetcode 41 - 45 dynamic planning topic
微信小程序影视评论交流平台系统毕业设计毕设(8)毕业设计论文模板