Advertisement
VladSmirN

Индивидуальная работа аип1 сем

Nov 28th, 2020 (edited)
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <string>
  4. using namespace std;
  5. int sampleSize = 0;
  6. int sample[100];
  7. int ticketSize = 0;
  8. int ticket[100];
  9. int ansSize = 0;
  10. int N;
  11. void push(int m[],int &n,int v){
  12.     m[n]=v;
  13.     n++;
  14. }
  15. bool has_only_digits(const string s){
  16.   return s.find_first_not_of( "0123456789" ) == string::npos;
  17. }
  18. bool validate(string s)
  19. {
  20.     if(s.size()!=6)return false;
  21.     if(s[0]=='0') return false;
  22.     ticketSize = 0;
  23.     for(int i=0; i<s.size(); ++i)
  24.     {
  25.         if(s[i] - '0' > 9 || s[i] - '0' <0 ) return false;
  26.         push(ticket,ticketSize,s[i] - '0');
  27.     }
  28.     return true;
  29. }
  30. int calculator(string s)
  31. {
  32.     s.push_back('+');
  33.  
  34.     int stnumber[100];
  35.     int stnumberSize = 0;
  36.     int stbin[100];
  37.     int stbinSize = 0;
  38.  
  39.     for(int i=0; i<s.size(); ++i)
  40.     {
  41.         if(s[i] != '+' && s[i] != '-'&& s[i] != '*' )
  42.         {
  43.             int n=s[i] - '0';
  44.             while(i+1<s.size() && s[i+1] != '+' && s[i+1] != '-'&& s[i+1] != '*')
  45.             {
  46.                 n*=10;
  47.                 n+=s[i+1]-'0';
  48.                 ++i;
  49.             }
  50.             push(stnumber,stnumberSize,n);
  51.  
  52.             continue;
  53.         }
  54.  
  55.             if(stbinSize !=0 &&( (stbin[stbinSize-1] == '*' &&  s[i] != '*') || (stbin[stbinSize-1] != '*' &&  s[i] != '*') )){
  56.              while(stnumberSize != 1){
  57.                 int a = stnumber[stnumberSize-1]  ;
  58.                 stnumberSize--;
  59.                 int b =  stnumber[stnumberSize-1];
  60.                 stnumberSize--;
  61.                 switch(stbin[stbinSize-1]){
  62.                     case '-' : push(stnumber,stnumberSize,-a+b) ;break;
  63.                     case '+' : push(stnumber,stnumberSize,a+b);break;
  64.                     case '*' : push(stnumber,stnumberSize,a*b);break;
  65.                 }
  66.                 stbinSize--;
  67.             }}
  68.             push(stbin,stbinSize,s[i]);
  69.     }
  70.  
  71.     return stnumber[stnumberSize-1];
  72. }
  73. void checkSample()
  74. {
  75.     string a;
  76.     for(int i=0; i<6; ++i)
  77.     {
  78.         a.push_back(ticket[i]+'0');
  79.  
  80.         if(sample[i] == 1)
  81.         {
  82.             a.push_back('+');
  83.         }
  84.         if(sample[i] == 2)
  85.         {
  86.             a.push_back('-');
  87.         }
  88.         if(sample[i] == 3)
  89.         {
  90.             a.push_back('*');
  91.         }
  92.  
  93.     }
  94.     if(calculator(a) == N){
  95.         ansSize++;
  96.         cout<<a<<"="<<N<<endl;
  97.     }
  98. }
  99.  
  100. void dfs()
  101. {
  102.     if(sampleSize == 5)
  103.     {
  104.         checkSample();
  105.         return ;
  106.     }
  107.     push(sample,sampleSize,0);
  108.     dfs();
  109.     sampleSize--;
  110.     push(sample,sampleSize,1);
  111.     dfs();
  112.     sampleSize--;
  113.     push(sample,sampleSize,2);
  114.     dfs();
  115.     sampleSize--;
  116.     push(sample,sampleSize,3);
  117.     dfs();
  118.     sampleSize--;
  119. }
  120. int main()
  121. {
  122.  
  123.  
  124.     setlocale(LC_ALL,"Russian");
  125.     string s  ;
  126.     while(true){
  127.         ansSize = 0;
  128.         cout<<"Введите шестизначное число:";
  129.         cin>>s;
  130.         if(!validate(s)){
  131.            cout<<"Некорректный ввод"<<endl;
  132.            continue;
  133.         }
  134.  
  135.  
  136.         string s2;
  137.         cout<<"Введите N:";
  138.         cin>>s2;
  139.         while(!has_only_digits(s2)){
  140.             cout<<"Некорректный ввод"<<endl;
  141.             cout<<"Введите N:";
  142.             cin>>s2;
  143.         }
  144.         N = atoi( s2.c_str());
  145.  
  146.  
  147.         dfs();
  148.         if(ansSize  == 0){
  149.             cout<<"Решение не существует."<<endl;
  150.             continue;
  151.         }
  152.  
  153.         cout<<"Найдено "<<ansSize<<" решений."<<endl;
  154.  
  155.     }
  156.  
  157.     return 0;
  158. }
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement