码迷,mamicode.com
首页 > 编程语言 > 详细

c++程序设计原理与实践 第四章部分答案

时间:2014-11-10 21:21:53      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   os   sp   for   

bubuko.com,布布扣
 1 #include <iostream>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     int a=1,b=100;
 7     int f=1;
 8     char c=0;
 9 
10     while(f<=7 && (b-a)>=1)
11     {
12         cout<<"你的数小等于"<<(a+b)/2<<"吗?(y/n)";
13         cin>>c;
14         if(c==n)
15             a=(a+b)/2+1;
16         else
17             b=(a+b)/2;
18         f++;
19     }
20     cout<<(a+b)/2<<endl;
21     while (true);
22 }
第4题
bubuko.com,布布扣
 1 #include "../../st.h"
 2 
 3 int main()
 4 {
 5     int sum=0;
 6     int i=1;
 7     while(sum<1000)
 8     {
 9         int s=1;
10         for(int n=1;n<i;n++)
11             s*=2;
12         sum+=s;
13         i++;
14     }
15 
16     cout<<"it is "<<i-1<<endl;
17 
18     while(1);
19     return 0;
20 }
第8题
bubuko.com,布布扣
 1 #include "../../st.h"
 2 //要得到自然数n以内的全部素数,必须把不大于的所有素数的倍数剔除,剩下的就是素数。
 3 const int  MAX=101;
 4 
 5 int main()
 6 {
 7     int all[MAX];
 8     int N,i,j,s;
 9    // memset(all,1,sizeof(all));
10     for(i=0;i<MAX;i++)    //初始化
11         all[i]=1;
12 
13     for(i=2;i<MAX;i++)     //倍数关系
14         if(all[i])
15             for(j=i+i;j<=MAX;j+=i)
16                 all[j]=0;
17     while(cin>>N)
18     {
19         for(i=2;i<=N;i++)
20             if(all[i])
21                 cout<<i<<"  ";
22         cout<<endl;
23     }
24     return 0;
25 }
第13题
bubuko.com,布布扣
 1 #include "../../st.h"
 2 //学到后面这些都是小case= =
 3 int main()
 4 {
 5     vector<int> all;
 6     vector<int>    s;
 7     vector<int> n;
 8     int t;
 9     while(cin>>t)
10         all.push_back(t);
11     sort(all.begin(),all.end());
12     t=all[0];
13     int k=0;
14     for(int i=0;i<all.size();i++){
15         if(t==all[i])
16             k++;
17         else
18         {
19             s.push_back(t);
20             n.push_back(k);
21             t=all[i];
22             k=1;
23         }
24     }
25     s.push_back(t);
26     n.push_back(k);
27     int max=0;
28     for(t=0;t<n.size();t++)
29         if(max<n[t])
30             max=n[t];
31     for(t=0;t<n.size();t++)
32         if(n[t]==max)
33             cout<<"the most number is "<<s[t]<<endl;
34 
35     while(1);
36     return 0;
37 }
第16题
bubuko.com,布布扣
 1 #include "../../st.h"
 2 
 3 int main()
 4 {
 5     vector<string> names;
 6     vector<double> scores;
 7     string t;
 8     double n;
 9     int i;
10     while(1)
11     {
12         cout<<"input name: ";
13         cin>>t;
14 
15         if(t=="no"){
16             cin>>t;
17             break;
18         }
19         for(i=0;i<names.size();i++)
20             if(names[i]==t){
21                 cout<<"this is error";
22                 exit(1);
23             }
24         names.push_back(t);
25         cout<<"input scores:";
26         cin>>n;
27         scores.push_back(n);
28 
29     }
30     for(i=0;i<names.size();i++)
31         cout<<names[i]<<"-"<<scores[i]<<endl;
32     cout<<endl;
33 
34     cout<<"input the name:";
35     cin>>t;
36     int f=0;
37     for(i=0;i<names.size();i++)
38         if(t==names[i]){
39             f=1;
40             break;
41         }
42     if(f==1)
43         cout<<t<<"‘s score is "<<scores[i]<<endl;
44     else
45         cout<<"not found."<<endl;
46 
47     cout<<"input the score:";
48     cin>>n;
49     f=0;
50     for(i=0;i<scores.size();i++)
51         if(n==scores[i]){
52             f=1;
53             cout<<names[i]<<endl;
54         }
55     if(f==0)
56         cout<<"score not found."<<endl;
57 
58     while(1);
59     return 0;
60 }    
第19题
bubuko.com,布布扣
 1 //第六章习题4  一个vector取代两个vector
 2 #include "../../st.h"
 3 
 4 class Name_value{
 5 public:
 6     string name;
 7     double val;
 8 
 9     Name_value(string s,double d);
10 };
11 
12 Name_value::Name_value(string s,double d)
13 {
14     name=s;
15     val=d;
16 }
17 
18 int main()
19 {
20     vector<Name_value> nv;
21     string s;
22     double d;
23     int i;
24     while(1)
25     {
26         cout<<"input name: ";
27         cin>>s;
28 
29         if(s=="no")
30         {
31             cin>>s;
32             break;
33         }
34         for(i=0;i<nv.size();i++)
35             if(nv[i].name==s)
36             {
37                 cout<<"this is error";
38                 exit(1);
39             }
40         cout<<"input scores:";
41         cin>>d;
42         Name_value nv1(s,d);
43         nv.push_back(nv1);
44     }
45     for(i=0;i<nv.size();i++)
46         cout<<nv[i].name<<"-"<<nv[i].val<<endl;
47     cout<<endl;
48 
49     cout<<"input the name:";
50     cin>>s;
51     int f=0;
52     for(i=0;i<nv.size();i++)
53         if(s==nv[i].name){
54             f=1;
55             break;
56         }
57     if(f==1)
58         cout<<s<<"‘s score is "<<nv[i].val<<endl;
59     else
60         cout<<"not found."<<endl;
61 
62     cout<<"input the score:";
63     cin>>d;
64     f=0;
65     for(i=0;i<nv.size();i++)
66         if(d==nv[i].val){
67             f=1;
68             cout<<nv[i].name<<endl;
69         }
70     if(f==0)
71         cout<<"score not found."<<endl;
72 
73     while(1);
74 
75     return 0;
76 }
第19题改

bubuko.com,布布扣简单练习

总的来说  这一章是很容易的  理解了就OK

c++程序设计原理与实践 第四章部分答案

标签:style   blog   http   io   color   ar   os   sp   for   

原文地址:http://www.cnblogs.com/yueba/p/4088049.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!