zoukankan      html  css  js  c++  java
  • c字符数组转整型【c语言复习1】

    //对A=1,B=2...Z=26,AA=27,AA=28,ZZ...
    //将字符串转为相应整数
    //c语言实现
    
    #include <stdio.h>
    int countA(char a[])//普通方法
    {
    	int all=0;
    	int i=0;
    	while(a[i]!='\0')
    	{
    		all = all * 26 + a[i]-'A' +1 ;
    		i++;
    	}
    	return all;
    }
    
    int countB(char a[])//指针方法
    {
    	int all=0;
    	char *p=a;
    	while(*p!='\0')
    	{
    		all = all * 26 + *p-'A' +1 ;
    		p++;
    	}
    	return all;
    }
    int main()
    {
    	char s[10];
    	char t;
    	while(1)
    	{
    		int i=0;
    		while((t=getchar())!='\n')
    		{	
    			if(t==EOF)
    				return 0 ;
    			s[i]=t;
    			i++;
    		}
    		s[i]='\0'; //字符数组最后要加上!
    		int c = countA(s);
    		int d = countB(s);
    		printf("%d\t%d\n",c,d);
    	}
    
    	return 0;
    }
    

      (2013腾讯广研创新班笔试题目)

  • 相关阅读:
    cookie和session。
    K3cloud Web API对接---单据保存接口(有源单)
    K3 wise kis 防火墙设置
    新单序时簿插件
    mssqlserver中排序规则冲突的问题解决
    读取金蝶图片
    金蝶wise委外订单关闭简述
    存储过程加锁
    判断存储过程是否存在
    解除死锁
  • 原文地址:https://www.cnblogs.com/liaopr/p/2972277.html
Copyright © 2011-2022 走看看