1.实现一个栈,包含栈的基本操作
s.pop
s.push
s.top
s.size
#include<iostream>
using namespace std;
class stack
{
private:
int date[100];
int size;
public:
stack():size(){}
~stack(){}
void pop()
{
if(size==0)throw "empty";
size--;
}
void push(const int &d)
{
if(size==100)throw "full";
date[size++] = d;
}
int top()
{
return date[size-1];
}
int size()
{
return size-1;
}
};
2.给定一个任意的正整数,求比这个数大且最小的“不重复数”,“不重复数”的含义是相邻两位不相同,例如1101重复,1212是不重复数。
#include<stdio.h>
int getMin(int d)
{
int i;
for(i = d+1;;i++)
{
int tmp = i;
int flag = 1;
if(i/10 == 0) return i;
int num1 = i%10;
int num2;
tmp = tmp/10;
while(tmp != 0)
{
num2 = tmp%10;
if(num1 == num2){ flag = 0;break;}
num1 = num2;
tmp = tmp/10;
}
if(flag) return i;
}
}
int main()
{
int d;
scanf("%d",&d);
int result = getMin(d);
printf("%d\n",result);
}
3 给出一个数组A= {a1,a2,a3......an} 打印出数值元素的所有组合。
CSDN
4.实现一个函数把小于1亿的int型转换成中文标识
17:一十七
110:一百一十
1010:一万零一百零一
#include<stdio.h>
#include<string.h>
void printnum(char a)
{
switch(a)
{
case '0':printf("零");break;
case '1':printf("一");break;
case '2':printf("二");break;
case '3':printf("三");break;
case '4':printf("四");break;
case '5':printf("五");break;
case '6':printf("六");break;
case '7':printf("七");break;
case '8':printf("八");break;
case '9':printf("九");break;
}
}
void printdanwei(int num)
{
switch(num)
{
case 5:printf("万");break;
case 4:printf("千");break;
case 3:printf("百");break;
case 2:printf("十");break;
}
}
void change(char *a)
{
int leng = strlen(a);
int highsit = 0;
int lowsit = 0;
int i =0;
if(leng >= 5)
{
highsit = leng - 4;
for(i = 0;i<highsit;i++)
{
printnum(a[i]);
printdanwei(highsit - i);
}
printdanwei(5);
}
for(i;i<leng;i++)
{
printnum(a[i]);
printdanwei(leng - i);
}
printf("\n");
}
int main()
{
char a[9];
scanf("%s",a);
printf("%s",a);
printf("-----------------\n");
change(a);
}
5,ip库查找问题
已经存在一个IP库,原始格式为,ip.txt实例内容如下:
222.12.23.0-222.12.23.25:北京
222.12.24.0-222.12.26.255:天津
a)编写一个函数将每一个IP转化为整形数字
unsigned int ip2int(string ip)
b)设计一个函数最快速判断出一个用户的IP属于那个城市
getcitybyip(ip)
inet_addr实现原理
输入是点分的IP地址格式(如A.B.C.D)的字符串,从该字符串中提取出每一部分,转换为ULONG,假设得到4个ULONG型的A,B,C,D,ulAddress(ULONG型)是转换后的结果,ulAddress = D<<24 + C<<16 + B<<8 + A(网络
字节序
),即inet_addr(const char *)的返回结果另外,我们也可以得到把该IP转换为主机序的结果,转换方法一样A<<24 + B<<16 + C<<8 + D我认为它把A,B,C,D都存储在静态的变量里了。所以,最好先把获取的结果存起来。long ip1 = inet_addr("11.11.11.11");long ip2 = inet_addr("22.22.22.22");然后再使用ip1和ip2
#include<stdio.h>
#include<stdlib.h>
#include<arpa/inet.h>
#include<string.h>
#define FILE_NAME "ip.txt"
unsigned int ip2int(char* ip)
{
char tmpip[16];
strcpy(tmpip,ip);
char *p = ip;
char *top = ip;
unsigned int num;
unsigned int sum = 0;
while(*p != '\0')
{
if(*p == '.')
{
*p = '\0';
p++;
num = atoi(top);
sum = sum << 8;
sum += num;
top = p;
continue;
}
p++;
}
num = atoi(top);
sum = sum <<8;
sum += num;
printf("%s = %u\n",tmpip,sum);
//unsigned int intip = inet_addr("1.1.1.1");
//printf("%s = %u\n",ip,intip);
return sum;
}
int ifbelong(char * buf,unsigned int tmp)
{
char *p = buf;
char *top = buf;
unsigned int head = 0;
unsigned int tail = 0;
while(*p != '\0')
{
if(*p == '-')
{
*p = '\0';
head = ip2int(top);
p++;
top = p;
break;
}
p++;
}
while(*p != '\0')
{
if(*p == ':')
{
*p = '\0';
tail = ip2int(top);
p++;
top = p;
break;
}
p++;
}
if((tmp >= head)&&(tmp <= tail))
{
printf("%s",top);
return 1;
}
else return 0;
}
void belongcity(unsigned int tmp)
{
FILE *fp;
char buf[100];
fp = fopen(FILE_NAME,"r");
if(fp == NULL)
{
printf("open file %s err\n",FILE_NAME);
return;
}
while(fgets(buf,100,fp) != NULL)
{
int len = strlen(buf);
int flag = ifbelong(buf,tmp);
if(flag == 1)
{
return;
}
}
printf("no belong\n");
}
int main()
{
char ip[16];
scanf("%s",ip);
printf("-------------------\n");
unsigned int tmp = ip2int(ip);
belongcity( tmp );
}