//vector
cout<<"Pleaseinputvectorelements:
"<vectorva,vb;
for(inti=0;i{
intx,y;
cin>>x>>y;
va.push_back(x);
vb.push_back(y);
}
cout<<"\nvector1:
";
for(inti=0;icout<cout<<"\nvector2:
";
for(inti=0;icout<cout<<"\n两个vector是否相等?
"<<(va==vb)<}
13.编写程序比较两个string类型的字符串,
*然后编写另一个程序比较两个C风格字符串。
*/
#include
#include
usingnamespacestd;
constintLen=128;
intmain()
{
//Cconstchar*
charstr1[Len],str2[Len];
cout<<"PleaseinputCstring1:
"<cin>>str1;
cout<<"PleaseinputCstring2:
"<cin>>str2;
boolequal=true;
for(inti=0;str1[i]||str2[i];i++){
if(str1[i]!
=str2[i]){
equal=false;
break;
}
}
cout<<"两个C风格字符串相等吗?
\t"<//C++string
stringstring1,string2;
cout<<"PleaseinputC++string1:
"<cin>>string1;
cout<<"PleaseinputC++string2:
"<cin>>string2;
cout<<"两个string相等吗?
\t"<}
14.编写程序将读入的浮点数值转换为中文金额的大写格式。
*/
#include
#include
#include
usingnamespacestd;
stringCH[]={"","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
stringUnit[]={"元","拾","佰","仟","万",
"拾","佰","仟","亿",
"拾","佰","仟","万","兆"};
stringjiao="角";
stringfen="分";
stringzheng="整";
stringzero="零";
stringyuan="元";
stringwanyuan="万";
stringyiyuan="亿";
intmain()
{
intintegeral,decimal;
chardc1,dc2;
charpoint;
cout<<"请输入金额数值:
";
cin>>integeral;
cin.get(point);
if(point=='.'){
cin.get(dc1);
cin.get(dc2);
if(dc1>='0'&&dc1<='9'){
decimal=(dc1-'0')*10;
if(dc2>='0'&&dc2<='9')
decimal+=dc2-'0';
}
}
else
decimal=0;
cout<stringipart(""),dpart("");
if(decimal==0)
{
dpart=zheng;
}
else
{
if(decimal<10){
dpart+=CH[decimal];
dpart+=fen;
}
else{
dpart+=CH[decimal/10];
dpart+=jiao;
if(decimal%10!
=0){
dpart+=CH[decimal%10];
dpart+=fen;
}
}
}
intn=integeral;
vectornumber;
while(n){
number.push_back(n%10);
n/=10;
}
intpos=0;
while(number[pos]==0)
pos++;
for(inti=number.size()-1;i>=pos;--i){
if(number[i]){
ipart+=CH[number[i]];
ipart+=Unit[i];
}
else{
ipart+=zero;
}
}
if(pos>=8)
ipart+=yiyuan;
if(pos>4&&pos<8)
ipart+=wanyuan;
if(pos>0)
ipart+=yuan;
cout<<"中文金额:
"<<"\t"<}
14.编写程序将读入的浮点数值转换为中文金额的大写格式。
*/
#include
#include
#include
usingnamespacestd;
stringCH[]={"","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
stringUnit[]={"元","拾","佰","仟","万",
"拾","佰","仟","亿",
"拾","佰","仟","万","兆"};
stringjiao="角";
stringfen="分";
stringzheng="整";
stringzero="零";
stringyuan="元";
stringwanyuan="万";
stringyiyuan="亿";
intmain()
{
intintegeral,decimal;
chardc1,dc2;
charpoint;
cout<<"请输入金额数值:
";
cin>>integeral;
cin.get(point);
if(point=='.'){
cin.get(dc1);
cin.get(dc2);
if(dc1>='0'&&dc1<='9'){
decimal=(dc1-'0')*10;
if(dc2>='0'&&dc2<='9')
decimal+=dc2-'0';
}
}
else
decimal=0;
cout<stringipart(""),dpart("");
if(decimal==0)
{
dpart=zheng;
}
else
{
if(decimal<10){
dpart+=CH[decimal];
dpart+=fen;
}
else{
dpart+=CH[decimal/10];
dpart+=jiao;
if(decimal%10!
=0){
dpart+=CH[decimal%10];
dpart+=fen;
}
}
}
intn=integeral;
vectornumber;
while(n){
number.push_back(n%10);
n/=10;
}
intpos=0;
while(number[pos]==0)
pos++;
for(inti=number.size()-1;i>=pos;--i){
if(number[i]){
ipart+=CH[number[i]];
ipart+=Unit[i];
}
else{
ipart+=zero;
}
}
if(pos>=8)
ipart+=yiyuan;
if(pos>4&&pos<8)
ipart+=wanyuan;
if(pos>0)
ipart+=yuan;
cout<<"中文金额:
"<<"\t"<}
15.将100元钱兑换为20元、10元、5元、1元,编写程序求不同的兑法。
要求每种兑
法中都要有20元、10元、5元和1元。
*/
#include
usingnamespacestd;
intmain()
{
intnum=0;
for(inta=1;a<5;a++)
for(intb=1;b<8;b++)
for(intc=1;c<14;c++)
for(intd=1;d<=65;d++)
if((a*20+b*10+c*5+d)==100)
{
cout<<
<<num++;
}
cout<<"共"<}
14.将1~100共一百个自然数,放入一个有99个元素的数组a[99]中,编写程序找出
没有被放入数组中的数。
*/
#include
usingnamespacestd;
boolflag[101];
intmain()
{
inta[99];
//....
for(inti=0;i<99;i++)
flag[a[i]]=true;
for(inti=1;i<=100;i++)
if(!
flag[i])
cout<
}
15.8,64,256都是2的阶次方数(如64是2的6次方),
*用两种方法编写程序判断一个整数是不是2的阶次方数,
*并说明哪种方法更好。
*/
#include
usingnamespacestd;
intmain()
{
intnum;
cout<<"Pleaseinputanumber:
"<cin>>num;
//algorithm1
inti=num;
do{
if(i%2==0)
i/=2;
else
break;
}while(i);
if(i==1)
cout<<"yes"<else
cout<<"no"<//algorithm2
i=num;
while(i){
if(!
(i&1))
i>>=1;
else
break;
}
if(i==1)
cout<<"yes"<else
cout<<"no"<}
18.structA构成了一个单向链表,编写程序,检测单向链表是否形成环。
*
*/
#include
usingnamespacestd;
structA{
structA*next;
};
boolcycle(A*head)
{
if(head==0)
returnfalse;
A*p=head->next;
while(p){
if(p==head)
returntrue;
else
p=p->next;
}
returnfalse;
}
intmain()
{
A*h1=0;
A*h2=newA;
h2->next=0;
A*h3=newA;
h3->next=h3;
A*h4=newA;
h4->next=newA;
h4->next->next=h4;
cout<<"h1"<cout<<"h2"<cout<<"h3"<cout<<"h4"<delete