且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

白书若干题

更新时间:2022-09-01 21:39:02

1. P32(排列) 

  用1、2、3、…、9组成3个三位数abc,def和ghi,每个数字恰好使用一次,要求abc:def:ghi=1:2:3。输出所有解。

int i,j,k;
for(i=123; i<=987/3; i++)
{
    j = 2*i;
    k = 3*i;
    //然后判断ijk是否满足条件(1到9不重不漏)    
} 


next_permutation等全部重拍,然后判断是否满足比例关系。 

2. P50(乘积的末三位)
  如题:输入若干个整数(可以是正数、负数或者零),输出它们的乘积的末三位。这些整数中会混入一些由大些字母组成的字符串,你的程序应当忽略他们。

  下面的我写的,没有处理正负号。

 1 #include <iostream>
 2 #include <string>
 3 #include <cctype>
 4 using namespace std;
 5 
 6 int solve(char *str)
 7 {
 8     int len = strlen(str);
 9     int ans = 0;//不是1 
10     int res = 1;
11     for(int i=0; i<len; i++)
12     {
13         if(isdigit(str[i]))
14         {
15             ans = ans*10 + (str[i] - '0');
16         }else
17         {
18             if(ans!=0)
19                 res *= ans%1000;
20             /*这个有一个数字后中间再有字母,那么res就成1了 
21             if(0==res)
22                 res = 1;
23                 */
24             ans = 0;//不是1 
25         }
26     }
27     return res%1000;
28 }
29 
30 int main()
31 {
32     int i,j,k;
33     char str[100];
34     int ans = 1;
35     while(fgets(str,100,stdin)!=NULL)//以ctrl + z结束,不是+c 
36     {
37         ans *= solve(str);
38         memset(str,0,sizeof(str));
39     }
40     cout<<ans<<endl;
41     //while(1);
42     return 0;
43 }

3. P50(计算器)
 编写程序,读入一行恰好包含一个加号、减号或乘号的表达式,输出它的值。这个运算符保证是二元运算符,且两个运算数均为不超过100的非负整数。运算数和运算符可以紧挨着,也可以用一个或多个空格、Tab隔开。行首末尾均可以有空格。

白书若干题白书若干题View Code
 1 //实际上可以直接用一般方法,扫描 
 2 //错误 
 3 #include <iostream>
 4 #include <cstdio>
 5 #include <cstring>
 6 #include <cctype>
 7 using namespace std;
 8 
 9 int main()
10 {
11     char str[20];
12     int a,b;
13     char ch;
14     memset(str,0,sizeof(str));
15     fgets(str,20,stdin);
16     sscanf(str,"%d%[+-*]%d",&a,&ch,&b);//不知道为什么错 
17     cout<<a<<ch<<b;
18     while(1);
19     return 0;
20     
21     
22 }
 1 #include <iostream>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     int a, b;
 7     char oper;
 8     cin >> a;
 9     //cin.get()是保留回车在输入流队列中的.而cin是丢弃回车的.
10     do{
11         //ch=cin.get();               //或者cin.get(ch);
12         cin.get(oper);          // 这题只要灵活运用 istream &get( char &ch );
13     }while(!('+' == oper || '-' == oper  || '*' == oper) );
14     cin >> b;
15     if('+' == oper){
16         cout << a + b << endl;
17     }
18     else if('-' == oper){
19         cout << a - b << endl;
20     }
21     else{  // *
22         cout << a * b << endl;
23     }
24     return 0;
25 }

4. P50(手机键盘)

  采用二维数组或者一维数组,采用一位数组的话存储第一个字母 。

5. P67(解方程组)

  编写一个 函数solve,给定浮点数a, b, c, d, e, f,求解方程组af + by = c, dx + ey = f。

  任务1:使用assert宏,让解不唯一时异常退出。  

assert(b*d!=a*e);

  任务2:解不唯一时仍然正常返回,但调用者有办法知道解的数量(无解、唯一解、无穷多组解)。

白书若干题白书若干题View Code
 1 #include<iostream>
 2  #include<cassert>
 3  using namespace std;
 4  
 5  double x, y;
 6  int solve(double a, double b, double c, double d, double e, double f)
 7  {
 8      if(b*d==a*e)
 9          return 0;
10      y = (c*d-a*f)/(b*d-a*e);
11      x = (c-b*y)/a;
12      return 1;
13  }
14  int main()
15  {
16      double a, b, c, d, e, f;
17      cout << "Please input some values!" << endl;
18      while(cin >> a >> b >> c >> d >> e >> f)
19      {
20          if(!solve(a, b, c, d, e, f))
21              cout << "无穷多解!!" << endl;
22          else
23          cout << x << "  " << y << endl;
24      }
25      return 0;
26  }

6.