今天做啦一个进制转换的题,改来改去最终倒是过啦,本来挺开心的,然后去翻啦一下题解,瞬间就有小情绪啦,哎,人家的代码辣么辣么短,实在是不开心,不过谁让咱是小渣渣呢,在此总结一下strtol 函数。

先来看看把我整不开心的这道题

Hrbust1896 http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1896

题目简单,只要足够细心就能AC的

看我自己的代码

 #include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<stack>
#include<math.h>
using namespace std;
int main()
{ int n,m;
char s[];
while(~scanf("%d%s",&n,&s)){
int t=;int ss[];
int len=strlen(s);
for(int i=;i<len;i++){
if(s[i]=='a'||s[i]=='A')ss[t++]=;
else if(s[i]=='b'||s[i]=='B')ss[t++]=;
else if(s[i]=='c'||s[i]=='C')ss[t++]=;
else if(s[i]=='d'||s[i]=='D')ss[t++]=;
else if(s[i]=='e'||s[i]=='E')ss[t++]=;
else if(s[i]=='f'||s[i]=='F')ss[t++]=;
else {ss[t++]=s[i]-''; } } int sum=;int j=;
for(int i=t-;i>=;i--){
sum+=ss[i]*pow(n,j++);
} cout<<sum<<endl; }
}

至于我发现的很牛很短的代码是这个样子的

 #include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
int main()
{
int m;//base(代表进制)
char n[];//char*nptr
while(~scanf("%d %s",&m,&n))
{
printf("%d\n",strtol(n,NULL,m));
//必须设定endptr为NULL 否则返回非法字符串
}
}

里面的注释是大神打上去的。。。

嘎嘎嘎,罪魁祸首就是strtol这个函数,不过他真的太方便啦嘿嘿

long int strtol(const char *nptr, char **endptr, int base)

*nptr指向的是在不同的进制下数的表示的字符串,base则为进制,char **endptr一般为空。

strtol函数的更多相关文章

  1. strtol函数 将字符串转换为相应进制的整数

    转自http://hi.baidu.com/qwpsmile/blog/item/9bc44efa4f41018a9f514637.html +----------------+|      strt ...

  2. POJ1331 Multiply(strtol函数练习)

    题目链接:http://poj.org/problem?id=1331 主要介绍strtol函数: long int strtol(const char *nptr,char **endptr,int ...

  3. 浅析C语言中strtol()函数与strtoul()函数的用法

    转自:http://www.jb51.net/article/71463.htm C语言strtol()函数:将字符串转换成long(长整型数) 头文件: ? 1 #include <stdli ...

  4. strtol 函数用法

    strtol是一个C语言函数,作用就是将一个字符串转换为长整型long,其函数原型为: long int strtol (const char* str, char** endptr, int bas ...

  5. strtol()函数

    #include <stdlib.h>#include <stdio.h> int main(){ char a[] = "100"; char b[] = ...

  6. strtol函数的用法——字符串转长整形

    /* strtol example */ #include <stdio.h> /* printf */ #include <stdlib.h> /* strtol */ in ...

  7. string.h文件中函数用法

    下面为string.h文件中函数的详细用法: strcpy函数名:strcpy功 能: 拷贝一个字符串到另一个用 法: char *strcpy(char *destin, char *source) ...

  8. C字符串处理函数

    部分参考百科. C常用字符串函数:字符串输入函数,字符串输出函数,字符串处理函数,标准输入输出流 字符串处理函数: 1.字符串长度:strlen(str),返回字符串实际长度,不包括'\0',返回值类 ...

  9. strtol详解

    文章装载自:http://blog.csdn.net/happylife1527/article/details/8441799,大家看到这篇文章想转载的请注明出处,尊重原作者劳动成果. 今天,在re ...

随机推荐

  1. c#语音报时(含完整的声音资源文件).rar

    private void btnBaoshi_Click(object sender, EventArgs e) { try { System.Threading.Thread thread = ne ...

  2. RSA加密工具包

    主要参考: http://www.blogjava.net/icewee/archive/2012/05/19/378570.html http://snowolf.iteye.com/ 基于以上代码 ...

  3. CryptoAPI与openssl RSA非对称加密解密(PKCS1 PADDING)交互

    (以下代码中都只做测试用,有些地方没有释放内存...这个自己解决下) 1.RSA非对称的,首先提供一个供测试用的证书和私钥的数据 1)pem格式的证书和私钥(公私钥是对应的)的base64编码 voi ...

  4. Add LUN to ASM in Linux

    # Create new LUN for Linux in the AMS2100 # echo "- - -" >/sys/class/scsi_host/host3/sc ...

  5. MySQL命名、设计及使用规范--------来自标点符的《MySQL命名、设计及使用规范》

    原文地址:http://www.biaodianfu.com/mysql-best-practices.html 最近在看MySQL相关的内容,整理如下规范,作为一名刚刚学习MySQL的菜鸟,整理的内 ...

  6. UNIX网络编程

    UNIX网络编程--socket的keep http://www.68idc.cn/help/opersys/unixbsd/20150731471448.html

  7. WCF Windows Service Using TopShelf and ServiceModelEx z

    http://lourenco.co.za/blog/2013/08/wcf-windows-service-using-topshelf-and-servicemodelex/ There are ...

  8. fatal error C1853: '*.pch' is not a precompiled header file created with this compile

    在菜单下选择Build--->点击Rebuild All,如果不起作用,你可以先把Debug或者Release中的文件删除,然后再试.

  9. SqlServer中的更新锁(UPDLOCK)

    UPDLOCK.UPDLOCK 的优点是允许您读取数据(不阻塞其它事务)并在以后更新数据,同时确保自从上次读取数据后数据没有被更改.当我们用UPDLOCK来读取记录时可以对取到的记录加上更新锁,从而加 ...

  10. mysql 如何用root 登录

    mysql -uroot -p 如果没有密码,按两下回车就进去了