C++入门练习 76题(1)

来源:百度文库 编辑:神马文学网 时间:2024/04/29 15:00:53
C++入门练习 76题(1) 收藏

混csdn快两个月了, 感觉自己水平还是很饼,  刚好从网上看到一篇帖子,  "C++入门练习", 共76题, 抓耳挠腮半天, 发现自己对编程真的没有入门啊 :) 于是就想把这些题做做, 算是练练手吧, 省的天天灌水浪费时间 :)

呵呵, 下面是第一题的程序.  自己大老粗一个, 写程序很少去想什么优化算法, 完全按程序意思来...

1.  给定等式    A B C D E     其中每个字母代表一个数字,且不同数字对应不
                    D F G     同字母。编程求出这些数字并且打出这个数字的
             +      D F G     算术计算竖式。

             ───────

                X Y Z D E

我的代码 : dev c++ 编译通过

#include 
#include 
using namespace std;

#define A num[0]
#define B num[1]
#define C num[2]
#define D num[3]
#define E num[4]
#define F num[5]
#define G num[6]
#define X num[7]
#define Y num[8]
#define Z num[9]
int num[10];
//A:num[0] B:num[1] C:num[2] D:num[3] E: F: G: H: 
void display(int count);
bool split_and_validate(int adder_long, int adder_short, int result);
bool validate_last();

int main(int argc, char *argv[])
...{
       // a variable to hold the add-operate result 
                int result;
                int count = 0; // how much?
       
    for(int i=1234; i<98765; i++)        // why i and j should be this? hehe :)  because.....
                  for(int j=12; j<987; j++)
                  ...{
                                    result = i+j+j;
                                    if(split_and_validate(i,j,result) == false)
                                       continue;
                           else
         ...{
                                          if(validate_last() == true)
                                          ...{
                                                      count++;
                                                      display(count);
                                                }
                                    }
                        }                                                                                                                           
   
             system("PAUSE");
    return EXIT_SUCCESS;
}

void display(int count)
...{
     //char k = '0';
      cout<<"found result, num:"<      cout<      cout<<"  "<      cout<<"+ "<      cout<<"--------"<      cout<}

bool split_and_validate(int adder_long, int adder_short, int result)
...{
    char e1, d1, d2;
   // split the longer adder
   E = adder_long%10;
            D = adder_long/10 %10;
            C = adder_long/100 % 10;
            B = adder_long/1000 % 10;
            A = adder_long/10000 % 10;
            
            // split the shorter adder
            G = adder_short%10;
            F = adder_short/10 %10;
            d1 = adder_short/100 %10    ;         
            
            if(D != d1)
              return false;
              
            // split the result
            e1 = result % 10;
            d2 = result/10 % 10;
            
            // if the two Es are not equal, scan the next case
            if ((E == e1) && (D == d2))
            ...{
                  Z = result/100 % 10;
                  Y = result/1000 % 10;
                  X = result/10000 % 10;
                  return true;
            }
         else
            return false;
}

bool validate_last()
...{
     for(int i=0; i<10; i++)
       for(int j=i+1; j<10; j++)
         if(num[i] == num[j]) return false;
         
  return true;
}

结果:

found result, num:1
29786
  850
+ 850
--------
31486