1、求0~n的自守数。
自守数:如果某个数的平方的末尾几位数等于这个数,那么就称这个数为自守数
如0^2=0
1^1=1
5^2=25;
输入一串字符,对各英文字符(大小写分开),数字和空格进行统计,并按照统计个数
由多到少输出,如果统计个数相同,则按ASCII码由小到大排序输出。如果有其他字符,这对这些字符不用统计
思路:最多统计字符有63个,建立一个数组hash[63],i=0~62
根据对应位置 i=0~9 对应'0'~'9'
i=10~35 对应'A'~'Z'
i=36~61 对应'a'~'z'
i=62 对应' '
统计字符出现的次数。
最后建立对组容器vector< pair<char,int> >char_int,对第二个参数进行由大到小排序。
知道自定义排序方式
长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
首先输入一个整数,为要输入的字符串个数。
例如:
输入:2
abc
12345789
输出:abc00000
12345678
90000000
详细描述:
输入某年某月某日,判断这一天是这一年的第几天?
Return Value : 成功返回0,失败返回-1(如:数据错误)
要求:
实现如下2个通配符:
*:匹配0个或以上的字符(字符由英文字母和数字0-9组成,不区分大小写。下同)
?:匹配1个字符
输入:
通配符表达式;
一组字符串。
输出:
返回匹配的结果,正确输出true,错误输出false
输入例子:
te?t*.*
txt12.xls
输出例子:
false
自守数:如果某个数的平方的末尾几位数等于这个数,那么就称这个数为自守数
如0^2=0
1^1=1
5^2=25;
76^2=5776
#include<iostream>
#include<string>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
bool solve(int num)
{
char str1[20];
char str2[20];
int i,j;
sprintf(str1,"%d",num);
sprintf(str2,"%d",num*num);
for(i=strlen(str1)-1,j=strlen(str2)-1;i>=0;i--,j--)
{
if(str1[i]!=str2[j])
return false;
}
return true;
}
int main()
{
int n,i,num;
while(cin>>n)
{
num=0;
for(i=0;i<=n;i++)
{
if(solve(i))
{
num++;
cout<<i<<endl;
}
}
cout<<num<<endl;
}
return 0;
}
2、字符排序
输入一串字符,对各英文字符(大小写分开),数字和空格进行统计,并按照统计个数
由多到少输出,如果统计个数相同,则按ASCII码由小到大排序输出。如果有其他字符,这对这些字符不用统计
思路:最多统计字符有63个,建立一个数组hash[63],i=0~62
根据对应位置 i=0~9 对应'0'~'9'
i=10~35 对应'A'~'Z'
i=36~61 对应'a'~'z'
i=62 对应' '
统计字符出现的次数。
最后建立对组容器vector< pair<char,int> >char_int,对第二个参数进行由大到小排序。
#include<iostream>
#include<string>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
bool compare(const pair<char, int>& a, const pair<char, int>& b)
{
return a.second > b.second;
}
int main()
{
string str;
int i;
while(cin>>str)
{
int hash[63]={0};
for(i=0;i<str.length();i++)
{
if(str[i]>='0'&&str[i]<='9')
{
hash[str[i]-'0']++;
}
else if(str[i]>='A'&&str[i]<='Z')
{
hash[str[i]-'A'+10]++;
}
else if(str[i]>='a'&&str[i]<='z')
{
hash[str[i]-'a'+36]++;
}
else if(str[i]==' ')
hash[62]++;
}
vector< pair<char,int> >char_int; //vc6.0中>>之间要有空格
for(i=0;i<63;i++)
{
if(hash[i]!=0)
{
if(i>=0&&i<=9)
{
char_int.push_back(make_pair(i+'0',hash[i]));
}
else if(i>=10&&i<=35)
{
char_int.push_back(make_pair(i+'A'-10,hash[i]));
}
else if(i>=36&&i<=62)
{
char_int.push_back(make_pair(i+'a'-36,hash[i]));
}
else
{
char_int.push_back(make_pair(' ',hash[i]));
}
}
}
sort(char_int.begin(),char_int.end(),compare);
char *output=(char *)malloc(char_int.size());
for(i=0;i<char_int.size();i++)
cout<<char_int[i].first;
cout<<endl;
}
return 0;
}
3、输入整型数组和排序标识,对其元素按照升序或降序进行排序
知道自定义排序方式
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
bool compare_up(const int &a,const int &b);
bool compare_down(const int &a,const int &b);
bool compare_up(const int &a,const int &b)//升序
{
return a<b;
}
bool compare_down(const int &a,const int &b)//降序
{
return a>b;
}
int main()
{
int num,flag,n;
int i;
while(cin>>num)
{
vector<int> data;
for(i=0;i<num;i++)
{
cin>>n;
data.push_back(n);
}
cin>>flag;
if(flag==0)
{
// cout<<"升序"<<endl;
sort(data.begin(),data.end(),compare_up);
}
else if(flag==1)
{
// cout<<"降序"<<endl;
sort(data.begin(),data.end(),compare_down);
}
cout<<data[0];
for(i=1;i<num;i++)
cout<<" "<<data[i];
cout<<endl;
vector<int>().swap(data);
cin.ignore();
}
}
4、连续输入字符串(输出次数为N,字符串长度小于100),请按长度为8拆分每个字符串后输出到新的字符串数组,
长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
首先输入一个整数,为要输入的字符串个数。
例如:
输入:2
abc
12345789
输出:abc00000
12345678
90000000
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
int num,i,j;
string str;
while(cin>>num)
{
vector<string>s;
for(i=0;i<num;i++)
{
cin>>str;
s.push_back(str);
}
for(i=0;i<num;i++)
{
int len=s[i].length();//计算每一个s[i]的长度
int L;
if((len%8)==0)//长度为8的倍数、不是8的倍数相应输出长度L
L=len;
else
L=(len/8+1)*8;
for(j=0;j<L;j++)
{
if((j%8)==0&&(j>0))
cout<<endl;
if(j<len)
cout<<s[i][j];
else
cout<<"0";
}
cout<<endl;
}
}
return 0;
}
5、根据输入的日期,计算是这一年的第几天。。
详细描述:
输入某年某月某日,判断这一天是这一年的第几天?
Return Value : 成功返回0,失败返回-1(如:数据错误)
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
int solve(int year,int month,int day)
{
if(month>12||day>31)
return -1;
int feb_day;
if((year%4==0&&year%100!=0)||year%400==0)
{
feb_day=29;//year是闰年
if(month==2&&month>feb_day)
return -1;
}
else
{
feb_day=28;//平年
if(month==2&&month>feb_day)
return -1;
}
int month_day[12]={31,feb_day,31,30,31,30,31,31,30,31,30,31};
int outday=0;
for(int i=0;i<month-1;i++)
{
outday+=month_day[i];
}
outday+=day;
return outday;
}
int main()
{
int y,m,d;
while(cin>>y>>m>>d)
{
int outday=0;
outday=solve(y,m,d);
if(outday!=-1)
cout<<outday<<endl;
else
cout<<"error"<<endl;
}
return 0;
}
6、问题描述:在计算机中,通配符一种特殊语法,广泛应用于文件搜索、数据库、正则表达式等领域。现要求各位实现字符串通配符的算法。
要求:
实现如下2个通配符:
*:匹配0个或以上的字符(字符由英文字母和数字0-9组成,不区分大小写。下同)
?:匹配1个字符
输入:
通配符表达式;
一组字符串。
输出:
返回匹配的结果,正确输出true,错误输出false
输入例子:
te?t*.*
txt12.xls
输出例子:
false
#include<iostream>
#include<string>
using namespace std;
bool solve(string str1,string str2)
{
if(str1.length()>str2.length())
return false;
int i,j;
for(i=0,j=0;i<str1.length(),j<str2.length();)
{
if(str1[i]=='?')
{
i++;
j++;
}
else if(str1[i]=='*')
{
if(i==str1.length()-1)
return true;
else if(i<str1.length()-1)
{
i++;j++;
}
}
else if(str1[i]!=str2[j])
return false;
else
{
i++;j++;
}
}
return true;
}
int main()
{
string str1,str2;
while(cin>>str1>>str2)
{
if(solve(str1,str2))
cout<<"true"<<endl;
else
cout<<"false"<<endl;
}
return 0;
}