5-1 字串轉換成十進位制整數 (15分) (這是補上的7月6號)
阿新 • • 發佈:2019-01-07
5-1 字串轉換成十進位制整數 (15分)
#include <stdio.h>
#include <algorithm>
#include <math.h>
char a[100000];
char b[140000];
int zhuan(char s,int i,int len)
{
if(s == '0')
return 0*pow(16,(len-i-1));
if(s == '1')
return 1*pow(16,(len-i-1));
if(s == '2')
return 2*pow(16,(len-i-1));
if(s == '3')
return 3*pow(16,(len-i-1));
if(s == '4')
return 4*pow(16,(len-i-1));
if(s == '5')
return 5*pow(16,(len-i-1));
if(s == '6')
return 6*pow(16,(len-i-1));
if(s == '7')
return 7*pow(16,(len-i-1));
if(s == '8')
return 8*pow(16,(len-i-1));
if(s == '9')
return 9*pow(16,(len-i-1));
if(s == 'a'||s == 'A')
return 10*pow(16,(len-i-1));
if(s == 'b'||s == 'B')
return 11*pow(16,(len-i-1));
if(s == 'c'||s == 'C')
return 12*pow(16,(len-i-1));
if(s == 'd'||s == 'D')
return 13*pow(16,(len-i-1));
if(s == 'e'||s == 'E')
return 14*pow(16,(len-i-1));
if(s == 'f'||s == 'F')
return 15*pow(16,(len-i-1));
}
int main()
{
int i,j;
int temp = 0;
scanf("%s",a);
int len = strlen(a);
i = 0;
j = 0;
while(i < len)
{
if(temp == 0&&j == 0&&a[i] == '-')
{
temp = 1;
i++;
}
if(a[i] >= '0'&&a[i] <= '9')
{
b[j] = a[i];
j++;
i++;
}
else if(a[i] >= 'a'&&a[i] <= 'f'||a[i] >= 'A'&&a[i] <= 'F')
{
b[j] = a[i];
j++;
i++;
}
else
i++;
}
int sum = 0;
len = strlen(b);
i = 0;
int m;
while(i < len)
{
m = zhuan(b[i],i,len);
sum = sum+m;
i++;
}
if(temp == 1&&sum!=0)
printf("-");
printf("%d\n",sum);
return 0;
輸入一個以#結束的字串,本題要求濾去所有的非十六進位制字元(不分大小寫),組成一個新的表示十六進位制數字的字串,然後將其轉換為十進位制數後輸出。如果在第一個十六進位制字元之前存在字元“-”,則代表該數是負數。
輸入格式:
輸入在一行中給出一個以#結束的非空字串。
輸出格式:
在一行中輸出轉換後的十進位制數。題目保證輸出在長整型範圍內。
輸入樣例:
+-P-xf4+-1!#
輸出樣例:
-3905
- 時間限制:400ms
- 記憶體限制:64MB
- 程式碼長度限制:16kB
- 判題程式:系統預設
- 作者:張彤彧
- 單位:浙江大學
#include <string.h>
#include <stdlib.h>#include <stdio.h>
#include <algorithm>
#include <math.h>
char a[100000];
char b[140000];
int zhuan(char s,int i,int len)
{
if(s == '0')
return 0*pow(16,(len-i-1));
if(s == '1')
return 1*pow(16,(len-i-1));
if(s == '2')
return 2*pow(16,(len-i-1));
if(s == '3')
return 3*pow(16,(len-i-1));
if(s == '4')
return 4*pow(16,(len-i-1));
if(s == '5')
return 5*pow(16,(len-i-1));
if(s == '6')
return 6*pow(16,(len-i-1));
if(s == '7')
return 7*pow(16,(len-i-1));
if(s == '8')
return 8*pow(16,(len-i-1));
if(s == '9')
return 9*pow(16,(len-i-1));
if(s == 'a'||s == 'A')
return 10*pow(16,(len-i-1));
if(s == 'b'||s == 'B')
return 11*pow(16,(len-i-1));
if(s == 'c'||s == 'C')
return 12*pow(16,(len-i-1));
if(s == 'd'||s == 'D')
return 13*pow(16,(len-i-1));
if(s == 'e'||s == 'E')
return 14*pow(16,(len-i-1));
if(s == 'f'||s == 'F')
return 15*pow(16,(len-i-1));
}
int main()
{
int i,j;
int temp = 0;
scanf("%s",a);
int len = strlen(a);
i = 0;
j = 0;
while(i < len)
{
if(temp == 0&&j == 0&&a[i] == '-')
{
temp = 1;
i++;
}
if(a[i] >= '0'&&a[i] <= '9')
{
b[j] = a[i];
j++;
i++;
}
else if(a[i] >= 'a'&&a[i] <= 'f'||a[i] >= 'A'&&a[i] <= 'F')
{
b[j] = a[i];
j++;
i++;
}
else
i++;
}
int sum = 0;
len = strlen(b);
i = 0;
int m;
while(i < len)
{
m = zhuan(b[i],i,len);
sum = sum+m;
i++;
}
if(temp == 1&&sum!=0)
printf("-");
printf("%d\n",sum);
return 0;
}
程式碼菜鳥,如有錯誤,請多包涵!!!