1.编号:a,b,c,.....z,aa,ab,ac...az,ba...zz,aaa,aab,aac....等,其对应数字编号是1,2,3.....26,27,28,29.......实现输入字符转换成相应数字编号。
如输入:aa
输出:27
#include <stdio.h>
#include <string.h>
int main()
{
char column[100];
int i;
int number = 0;
int base = 1;
scanf("%s",column);
for(i=strlen(column)-1;i>=0;--i,base *= 26)
{
number +=(column[i]-'a'+1)*base;
}
printf("%d\n",number);
return 0;
}
2.输入一组正整数,从小到大排序,排序后有连续数时,只输出连续数中最小和最大的两个数,输入时字符串,其最大长度为128.
输入:以“,”分隔
输入:1,4,3,11,2,9,7
输出:1,4,7,9,11
#include<stdio.h>
void sort(int str[],int LENGTH);
int main()
{
int column[100];
int i=0,str_length=0;
char c=',';
//create column[] and cal length of array
while(c!='\n'&&str_length<128)
{
scanf("%d%c",&column[i++],&c);
str_length++;
}
sort(column,str_length);
int *test=column;
//output sorted column
for(i=0;i<str_length;i++)
{
printf("%d,",*(test+i));
}
printf("\n");
//output final column
printf("%d,",column[0]);
for(i=1;i<str_length-1;i++)
{
if( (column[i-1]!=column[i]-1) || (column[i]!=column[i+1]-1) )
printf("%d,",column[i]);
}
printf("%d",column[str_length-1]);
return 0;
}
void sort(int str[],int LENGTH)
{
int i,j,temp;
for(i=0;i<LENGTH; i++)
for(j=i+1;j<LENGTH;j++)
{
if(str[i]>str[j])
{
temp=str[j];
str[j]=str[i];
str[i]=temp;
}
}
}
/*
void sort(int str[],int LENGTH)
{
int i,j,temp;
for(i=0;i< LENGTH-1 ; i++)
for(j=0;j<LENGTH-1-i;j++)
{
if(str[j]>str[j+1])
{
temp=str[j];
str[j]=str[j+1];
str[j+1]=temp;
}
}
}
*/