打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C++入门练习 76题(1)
userphoto

2010.12.17

关注
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 <cstdlib>
#include 
<iostream>
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:"<<count<<endl;
      cout
<<A<<B<<C<<D<<E<<endl;
      cout
<<"  "<<D<<F<<G<<endl;
      cout
<<""<<D<<F<<G<<endl;
      cout
<<"--------"<<endl;
      cout
<<X<<Y<<Z<<D<<E<<endl;
}


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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
条件变量实现带缓冲区的生产者消费者问题
计算思维实践之路(八)
扭曲乘法 正确
STL算法——谓词讲解
剑指offer之找出数组中重复数字
Define,const,static用法总结
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服