进制转换
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int main()
{
string s;
while(cin>>s)
{
int bit=0;
int ans =0;
for(int i=s.length()-1;i>1;i--)
{
if(s[i]>='0'&&s[i]<='9')
ans+=(s[i]-'0')*pow(16,bit++);
else if(s[i]>='A'&&s[i]<='F')
ans+=(s[i]-'A'+10)*pow(16,bit++);
}
cout<<ans<<endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int i;
scanf("%x", &i);
cout << i;
}
#include<iostream>
#include<string>
using namespace std;
int main(){
string str;
while(cin>>str){
cout << stoi(str,0,16) << endl;
}
}
整数与IP地址间的转换
#include <iostream>
using namespace std;
int main()
{
long long int a,b,c,d;
long long int num;
while(scanf("%lld.%lld.%lld.%lld",&a,&b,&c,&d)!=EOF){
cin>>num;
cout<<(a<<24)+(b<<16)+(c<<8)+d<<endl;
a = num>>24;//向右移动 24 位意味着将该数除以2
//的24次方并向下取整,即丢弃其低 24 位
num = num-(a<<24);
b = num>>16;
num = num-(b<<16);
c = num>>8;
d = num-(c<<8);
cout<<a<<"."<<b<<"."<<c<<"."<<d<<endl;
}
}
迷宫问题
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> visited;
void dfs(int i, int j,vector<vector<int>> &board,vector<vector<int>> &visited,vector<vector<int>> &arr) {
if (i < 0 || i >= board.size() || j < 0 || j >= board[0].size() || board[i][j] == 1 ||
visited[i][j] == 1) return ;
visited[i][j] = 1;
arr.push_back({i, j});
if (i == board.size() - 1 && j == board[0].size() - 1){
for(int i=0;i<arr.size();i++){
cout<<'('<<arr[i][0]<<','<<arr[i][1]<<')'<<endl;
}
return;
}
dfs(i, j + 1,board,visited,arr);
dfs(i + 1, j,board,visited,arr);
dfs(i, j - 1,board,visited,arr);
dfs(i - 1, j,board,visited,arr);
//visited[i][j] = 0;
arr.pop_back();
return;
}
int main() {
int a, b;//a行,b列
cin >> a >> b;
vector<vector<int>> board(a, vector<int>(b, 0));
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
cin >> board[i][j];
}
}
vector<vector<int>> visited(a, vector<int>(b, 0));
vector<vector<int>> arr;
dfs(0,0,board,visited,arr);
// for(int i=0;i<arr.size();i++){
// cout<<'('<<arr[i][0]<<','<<arr[i][0]<<')'<<endl;
// }
}
// 64 位输出请用 printf("%lld")