标签:style blog http color ar os sp for div
1 #include "../../st.h" 2 3 int main() 4 { 5 vector<double> nums; 6 double t; 7 int n; 8 cout<<"input how many nums: "; 9 cin>>n; 10 cin>>t; 11 nums.push_back(t); 12 int i=0; 13 while(cin>>t) 14 { 15 if(i<n-1&&nums[i]!=t) 16 { 17 nums.push_back(t); 18 i++; 19 } 20 } 21 double sum=0; 22 for(i=0;i<nums.size();i++) 23 { 24 cout<<nums[i]<<" "; 25 sum+=nums[i]; 26 } 27 cout<<endl<<"the count is "<<sum<<endl; 28 keep_window_open(); 29 return 0; 30 }
1 #include "../../st.h" 2 3 //maybe can be better 4 //利用 narrow_cast<> 5 //这个版本没有 6 7 int main() 8 try 9 { 10 int x1=0,x2=1,x=x1+x2; 11 bool b=1; 12 cout<<x1<<endl<<x2<<endl<<x<<endl; 13 while(x>=0) 14 { 15 cout<<x<<endl; 16 x=x1+x2; 17 18 x1=x2; 19 x2=x; 20 } 21 keep_window_open(); 22 return 0; 23 } 24 25 catch(runtime_error) 26 { 27 cout<<"it is over."<<endl; 28 keep_window_open(); 29 return 1; 30 }
1 #include "../../st.h" 2 3 //利用两个数组储存 4 5 int main() 6 { 7 int a=1224; //设定初始数 8 int b; //我们猜测的数 9 int n,m; 10 int i,j,t; 11 int aa[4]; //存原始数的每个数字 12 int bb[4]; //我们猜测的数的每个数字 13 for(i=3;i>=0;i--) 14 { 15 aa[i]=a%10; 16 a/=10; 17 } 18 while(1) 19 { 20 cin>>b; 21 n=m=0; 22 for(i=3;i>=0;i--) 23 { 24 bb[i]=b%10; 25 b/=10; 26 } 27 28 for(i=0;i<4;i++) 29 if(aa[i]==bb[i]) 30 n++; 31 if(n==4) 32 { 33 cout<<"4 gong"<<endl; 34 break; 35 } 36 for(i=0;i<4;i++) 37 for(j=0;j<3-i;j++) 38 if(bb[j]>bb[j+1]) 39 { 40 t=bb[j]; 41 bb[j]=bb[j+1]; 42 bb[j+1]=t; 43 } 44 for(i=0;i<4;i++) 45 { 46 for(j=0;j<4;j++) 47 if(aa[i]==bb[j]){ 48 m++; 49 break; 50 } 51 } 52 cout<<m<<endl; 53 m-=n; 54 cout<<n<<" gong,"<<m<<" mu"<<endl; 55 } 56 return 0; 57 }
1 //为第六章习题8 但4个数字改为4个字母 2 #include "../../st.h" 3 4 int main() 5 { 6 char a[4]={‘a‘,‘b‘,‘b‘,‘c‘}; 7 char b; 8 int n,m; 9 int i,j,t; 10 int aa[4],bb[4]; 11 for(i=0;i<4;i++) 12 { 13 aa[i]=a[i]; 14 } 15 while(1) 16 { 17 18 n=m=0; 19 for(i=0;i<4;i++) 20 { 21 cin>>b; 22 bb[i]=b; 23 } 24 25 26 for(i=0;i<4;i++) 27 if(aa[i]==bb[i]) 28 n++; 29 if(n==4) 30 { 31 cout<<"4 gong"<<endl; 32 break; 33 } 34 for(i=0;i<4;i++) 35 for(j=0;j<3-i;j++) 36 if(bb[j]>bb[j+1]) 37 { 38 t=bb[j]; 39 bb[j]=bb[j+1]; 40 bb[j+1]=t; 41 } 42 for(i=0;i<4;i++) 43 { 44 for(j=0;j<4;j++) 45 if(aa[i]==bb[j]){ 46 m++; 47 break; 48 } 49 } 50 m-=n; 51 cout<<n<<" gong,"<<m<<" mu"<<endl; 52 } 53 54 while(1); 55 return 0; 56 }
这一章主要讲了异常,但其实并没太多的深入,可以到C++primer中深入
标签:style blog http color ar os sp for div
原文地址:http://www.cnblogs.com/yueba/p/4088090.html