15,440
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <set>
#include <algorithm>
#include <iterator>
#define MAXLINE 128
#define PASTE(ARG) set_##ARG(left.begin(),left.end(),right.begin(),right.end(),inserter(output,output.begin()))
#define ARG_NOT_CORRECT 1
#define INPUT_NOT_VAILD 2
#define FILE_ISNOT_EXIST 4
#define DIGIT_NOT_VAILD 8
using namespace std;
void help()
{
cout << "Usage:setalgorithm infile_1 infile_2 style mode\n";
cout << "infile:file contains data in the set, only one digit or a continuous group digit each line.\n";
cout << " you can get range from a to b by the way \"a,b\" in the digit mode\n";
cout << " or treat data as normal string in the string mode\n";
cout << "style:fill this block with 'u' to get the union ,'i' to get the intersection , and 'd' to get the difference.\n";
cout << "mode: 's' to set string mode and 'd' to set digit mode\n";
}
void read_string_from_file(set<string> & data , FILE * fp)
{
data.clear();
char buffer[MAXLINE];
while(fgets(buffer , MAXLINE , fp))
{
buffer[strlen(buffer)-1] = '\0';
data.insert(string(buffer));
}
}
void read_digit_from_file(set<int> & data , FILE * fp)
{
data.clear();
char buffer[MAXLINE];
char * point;
while(fgets(buffer , MAXLINE , fp))
{
point = strchr(buffer,',');
if(NULL == point)
{
data.insert(atoi(buffer));
}
else
{
*point = '\0';
int lowerbound = atoi(buffer);
int upperbound = atoi(point+1);
if(lowerbound > upperbound)
{
exit(DIGIT_NOT_VAILD);
}
while(lowerbound <= upperbound)
{
data.insert(lowerbound++);
}
}
}
}
template <typename T>
void travel_set(T & output)
{
typename T::iterator iter;
for(iter= output.begin() ; iter != output.end() ; ++iter)
{
cout << *iter << endl;
}
}
template <typename T>
void getresult(T & left , T & right , T & output , char option)
{
switch(option)
{
case 'u':
{
PASTE(union);
break;
}
case 'i':
{
PASTE(intersection);
break;
}
case 'd':
{
PASTE(difference);
break;
}
}
travel_set(output);
}
void digit_algorithm(FILE * lfp , FILE * rfp , char option)
{
set<int> left , right , output;
read_digit_from_file(left , lfp);
read_digit_from_file(right , rfp);
getresult(left , right , output , option);
}
void string_algorithm(FILE * lfp , FILE * rfp , char option)
{
set<string> left , right , output;
read_string_from_file(left , lfp);
read_string_from_file(right , rfp);
getresult(left , right , output , option);
}
int main(int argc , char ** argv)
{
if(argc != 5)
{
help();
exit(ARG_NOT_CORRECT);
}
char option = argv[3][0];
if(option != 'u' && option != 'd' && option != 'i')
{
help();
exit(INPUT_NOT_VAILD);
}
char mode = argv[4][0];
if(mode != 's' && mode != 'd')
{
help();
exit(INPUT_NOT_VAILD);
}
FILE * lfp , * rfp;
if( (lfp = fopen(argv[1] , "r")) == NULL )
{
cerr << "can't open file:" << argv[1] << endl;
exit(FILE_ISNOT_EXIST);
}
if( (rfp = fopen(argv[2] , "r")) == NULL )
{
cerr << "can't open file:" << argv[2] << endl;
exit(FILE_ISNOT_EXIST);
}
if( mode == 's' )
{
string_algorithm(lfp , rfp , option);
}
else if( mode == 'd' )
{
digit_algorithm(lfp , rfp , option);
}
fclose(lfp);
fclose(rfp);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
struct Trip
{
int _row , _col , _digit;
};
char _maintable[10][10];
bool checkrow(int lineno , int digit)
{
digit += '0';
for(int iter = 1 ; iter != 10 ; ++iter)
{
if(_maintable[lineno][iter] == digit)
{
return false;
}
}
return true;
}
bool checkcol(int lineno , int digit)
{
digit += '0';
for(int iter = 1 ; iter != 10 ; ++iter)
{
if(_maintable[iter][lineno] == digit)
{
return false;
}
}
return true;
}
bool checkblock(int row , int col , int digit)
{
digit += '0';
int rowindex = (row-1)/3 * 3 + 1;
int colindex = (col-1)/3 * 3 + 1;
for(int r = rowindex ; r != rowindex + 3 ; ++r)
{
for(int c = colindex ; c != colindex + 3 ; ++c)
{
if(row == r && col == c)
{
continue;
}
if(_maintable[r][c] == digit)
{
return false;
}
}
}
return true;
}
inline bool checkvaild(int row , int col , int digit)
{
return checkrow(row , digit) && checkcol(col , digit) && checkblock(row , col ,digit);
}
void read_matrix()
{
for(int row = 1 ; row != 10 ; ++row)
{
fflush(stdin);
for(int col = 1 ; col != 10 ; ++col)
{
scanf("%c",&_maintable[row][col]);
}
}
}
void count(vector<Trip> & data)
{
Trip temp;
for(int row = 1 ; row != 10 ; ++row)
{
for(int col = 1 ; col != 10 ; ++col)
{
if(_maintable[row][col] == '0')
{
temp._row = row;
temp._col = col;
temp._digit = 0;
data.push_back(temp);
}
}
}
}
void output()
{
for(int row = 1 ; row != 10 ; ++row)
{
for(int col = 1 ;col != 10 ; ++col)
{
printf("%c ",_maintable[row][col]);
}
printf("\n");
}
}
void output_debug(vector<Trip> & data)
{
int n = data.size();
for(int iter = 0 ; iter != n ; ++iter)
{
printf("%d %d %d\n",data[iter]._row , data[iter]._col , data[iter]._digit);
}
}
bool travel(vector<Trip> & data)
{
int n = data.size();
int iter = 0 ;
int row , col , digit;
while(iter != n)
{
if(iter < 0)
{
return false;
}
row = data[iter]._row;
col = data[iter]._col;
digit = data[iter]._digit;
while(1)
{
digit++;
if(digit == 10)
{
data[iter]._digit = 0;
_maintable[row][col]='0';
iter--;
break;
}
if(checkvaild(row,col,digit))
{
data[iter]._digit = digit;
_maintable[row][col] = digit+'0';
iter++;
break;
}
}
}
return true;
}
bool finalcheck()
{
int digit;
for(int iter = 1 ; iter != 10 ; ++iter)
{
digit = _maintable[iter][1];
if( ! checkvaild(iter , 1 ,digit) )
{
return false;
}
}
return true;
}
int main()
{
vector<Trip> data;
bool is_vaild;
read_matrix();
count(data);
is_vaild = travel(data);
if(is_vaild)
{
if(finalcheck())
{
output();
}
else
{
printf("输入有误\n");
}
}
else
{
printf("无解\n");
}
system("pause");
return 0;
}
#include <algorithm>
#include <iostream>
#include <functional>
#include <cstring>
using namespace std;
int main() {
char *Alphabet = "abcdefghijklmnopqrstuvwxyz" ;
char *Vowels = "aeiou" ;
char *AlphaNum = "0123456789abcdef" ;
char result[45] ;
char *last ;
int lenA = strlen(Alphabet) ;
int lenV = strlen(Vowels ) ;
int lenAN = strlen(AlphaNum) ;
cout << "Alphabet = " << Alphabet << endl ;
cout << "Vowels = " << Vowels << endl ;
cout << "AlphaNum = " << AlphaNum << endl ;
cout << "\nusing non-predicate versions" << endl ;
//non-predicate set_difference
last = set_difference(Alphabet, Alphabet+lenA,
AlphaNum, AlphaNum+lenAN,
result) ;
*last = 0 ;
cout << "set_difference(Alphabet, AlphaNum) = " << result << endl ;
//non-predicate set_intersection
last = set_intersection(Alphabet, Alphabet+lenA,
AlphaNum, AlphaNum+lenAN,
result) ;
*last = 0 ;
cout << "set_intersection(Alphabet, AlphaNum) = " << result << endl ;
//non-predicate set_symmetric_difference
last = set_symmetric_difference(Alphabet, Alphabet+lenA,
Vowels , Vowels +lenV,
result) ;
*last = 0 ;
cout << "set_symmetric_difference(Alphabet, Vowels) = " << result << endl ;
//non-predicate set_union
last = set_union(Alphabet, Alphabet+lenA,
AlphaNum, AlphaNum+lenAN,
result) ;
*last = 0 ;
cout << "set_union(Alphabet, AlphaNum) = " << result << endl ;
cout << "\nusing predicate versions" << endl ;
//predicate set_difference
last = set_difference(Alphabet, Alphabet+lenA,
AlphaNum, AlphaNum+lenAN,
result , less<char>()) ;
*last = 0 ;
cout << "set_difference(Alphabet, AlphaNum) = " << result << endl ;
//predicate set_intersection
last = set_intersection(Alphabet, Alphabet+lenA,
AlphaNum, AlphaNum+lenAN,
result , less<char>()) ;
*last = 0 ;
cout << "set_intersection(Alphabet, AlphaNum) = " << result << endl ;
//predicate set_symmetric_difference
last = set_symmetric_difference(Alphabet, Alphabet+lenA,
Vowels , Vowels +lenV,
result , less<char>()) ;
*last = 0 ;
cout << "set_symmetric_difference(Alphabet, Vowels) = " << result << endl ;
//predicate set_union
last = set_union(Alphabet, Alphabet+lenA,
AlphaNum, AlphaNum+lenAN,
result , less<char>()) ;
*last = 0 ;
cout << "set_union(Alphabet, AlphaNum) = " << result << endl ;
return 0 ;
}