- 用流提取运算符和重载运算符
#include<iostream>
#include<stdlib.h>
using namespace std;
class PhoneNumber
{
friend ostream & operator << (ostream &output,PhoneNumber &);
friend istream & operator >> (ostream &input, PhoneNumber &);
public:
char phone[15];
char area[4];
char exchange[4];
char line[5];
public:
PhoneNumber()
{
phone[ 0 ] = '\0';
area[ 0 ] = '\0';
exchange[ 0 ] = '\0';
line[ 0 ] = '\0';
}
};
ostream & operator<<(ostream &output,PhoneNumber &phone)
{
output<<"("<<phone.area<<")"<<phone.exchange<<"-"<<phone.line<<endl;
return output;
}
istream & operator>>(istream &input,PhoneNumber &phone)
{
input.getline(phone.phone,15);
if(strlen(phone.phone)!=14)
input.clear(ios::failbit);
if(phone.phone[0]!='('||phone.phone[4]!=')'||phone.phone[9]!='-')
input.clear(ios::failbit);
if(!input.fail())
{
int i;
for(i=0;i<=2;i++)
{
phone.area[i]=phone.phone[i+1];
phone.exchange[i]=phone.phone[i+6];
}
phone.area[i]=phone.exchange[i]='\0';
for(i=0;i<=3;i++)
{
phone.line[i]=phone.phone[i+10];
}
phone.line[i]='\0';
}
else
{
cerr<<"Invail phone number"<<endl;
exit(1);
}
}
int main()
{
PhoneNumber pn;
cout << "Enter a phone number in the form (123) 456-7890:\n";
cin>>pn;
cout << "Now enter an valid phone number:\n";
cout<<pn;
return 0;
}
2.读取并答应一个顺序文件
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<cstdlib>
using namespace std;
void outputline(int,const string &, double);
int main()
{
ifstream infile("D:\\mytest11.txt",ios::in);
if(!infile.is_open())
{
cerr<<"file failed"<<endl;
exit(EXIT_FAILURE);
}
int account;
string name;
double balance;
cout<<left<<setw(10)<<"Account"<<setw(10)<<"Name"<<setw(10)<<"Balance"<<fixed<<showpoint<<endl;
while(infile>>account>>name>>balance)
{
outputline(account,name,balance);
}
infile.close();
return 0;
}
void outputline(int account,const string &name, double balance)
{
cout<<left<<setw(10)<<account<<setw(10)<<name<<setw(10)<<balance<<fixed<<showpoint<<endl;
}
- 最大子段和
#include<iostream>
using namespace std;
int main()
{
int a[]={2,-4,3,-1,2,-4,3};
int maxsum=a[0];
int currentsum=a[0];
for(int i=1;i<sizeof(a)/sizeof(*a);i++)
{
if(currentsum+a[i]>maxsum)
{
maxsum=currentsum+a[i];
}
if(currentsum+a[i]<0)
{
if(a[i]<0)
currentsum= 0;
else
currentsum=a[i];
}
else
{
currentsum=currentsum+a[i];
}
}
cout<<maxsum<<endl;
return 0;
}
- 连续自然数之和
#include<stdio.h>
#include<math.h>
#include<iostream>
using namespace std;
int main()
{
int n;
scanf("%d",&n);
int left,right,sum;
left=1;
right= (int)sqrt(2.0*n); // 是估计最接近的数值。
sum=(left+right)*(right-left+1)/2; //sum求和不一定等于n的值,
while(left<right)
{
if(sum==n)
{
cout<<left<<" "<<right<<endl;
sum-=left++; // 这一步使得其余情况全部输出
}
else if(sum<n)
{
right++;
sum+=right; // 再加上后面的那个连续数看是否能够满足应该的n
}
else //n<sum
{
sum-=left; // 先减去多去的那个left,在判断sum是否能够满足n
left++; // left再往后移
}
}
cout<<left<<" "<<right<<endl;
return 0;
}
- 丑数
#include<iostream>
#include<vector>
#include<cstdio>
using namespace std;
vector<int> choushu;
bool judge(int number)
{
while(number%2==0)
{
number/=2;
}
while(number%3==0)
{
number/=3;
}
while(number%5==0)
{
number/=5;
}
if(number==1)
return true;
else
return false;
}
void init()
{
for(int i=1;i<1000;i++)
{
if(judge(i))
choushu.push_back(i);
}
}
int main()
{
int n;
init();
while(scanf("%d",&n)!=EOF)
{
if(n!=0)
cout<<choushu[n-1]<<endl;
else
break;
}
return 0;
}
- Hamming Problem POJ2545
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
long long p1,p2,p3,H[100005];
int n,i,a1,a2,a3;
//Sample Input 7 13 19 100
while(scanf("%lld%lld%lld%d",&p1,&p2,&p3,&n)!=EOF)
{
H[0]=1;
a1=a2=a3=0;
for(i=1;i<=n;i++)
{
H[i]=p1*H[a1]<p2*H[a2]?p1*H[a1]:p2*H[a2]; // 在p1,p2中找到组合最小的
if(H[i]>p3*H[a3]) // 如果p3的更小,将值赋给H[i]
H[i]=p3*H[a3];
// a1 a2 a3 实则是记录p1,p2,p3的次数,然后在过程中找到组合的最小值。
if(H[i]==p1*H[a1])
a1++;
if(H[i]==p2*H[a2])
a2++;
if(H[i]==p3*H[a3])
a3++;
}
// 循环在直到n的时候停止。
cout<<H[n]<<endl;
}
return 0;
}
- 归并
#include<iostream>
#include<ctime>
#include<stdlib.h>
using namespace std;
void merge(int *arr,int left,int mid,int right,int *temp)
{
int i=left; // 做序列指针
int j=mid+1;// 右序列指针
int t=0;
while((i<=mid)&&(j<=right))
{
if(arr[i]<arr[j])
temp[t++]=arr[i++]; // 把小的放在temp的数组中
else
temp[t++]=arr[j++];
}
// 这个时候可能会出现i和j只用了一部分,我们还应该将剩余的i,j在数组中的值放入temp中
while(i<=mid) // 你看这时候就是i的没用完
{
temp[t++]=arr[i++];
}
while(j<=right)
{
temp[t++]=arr[j++];
}
t=0;// 将temp中的元素全部拷贝到原数组中
while(left<=right)
{
arr[left++]=temp[t++];
}
}
void MSort(int *arr,int left,int right,int *temp)
{
if(left<right)
{
int mid=(left+right)/2;
MSort(arr,left,mid,temp);// 这里用了递归的方法
MSort(arr,mid+1,right,temp);
merge(arr,left,mid,right,temp); // 到左右都已经分好的时候,就可以一段一段的合并在一起。
}
// 出口其实是在if(left=right)这里, 就直接return了
}
void MergeSort(int *arr,int length)
{
int *temp=new int[length];
MSort(arr,0,length-1,temp);
delete temp;
}
int main()
{
int a[]={3,5,1,9,10,11,8,2};
MergeSort(a,8);
for(int i=0;i<8;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
return 0;
}