//Problem 1
#include
#include
int arraycheck(int * s){
if (s[0] == s[1] && s[3] == s[4] && s[0] != s[4])
return 1;
else
return 0;
}
int main(){
int license[5];
int i,j;
int n;
// d
for (i = 100; i <= 333; i++)
{
n = i*i;
j = 0;
do{
license[j++] = n %10;
}
while((n/=10)>0);
if (arraycheck(license))
printf ("The license is dex %d and the dex number is %d \n", i*i, i);
}
// x
for (i = 256; i < 1024; i++)
{
n = i*i;
j = 0;
do{
license[j++] = n %16;
}
while((n/=16)>0);
if (arraycheck(license))
printf ("The license is hex %x and the hex number is %x \n", i*i,i);
}
// o
for (i = (int)(64*(double)sqrt(2)) - 1; i < (int)(128*(double)sqrt(2)) + 1; i++)
{
n = i*i;
j = 0;
do{
license[j++] = n %8;
}
while((n/=8)>0);
if (arraycheck(license))
printf ("The license is oct %o and the oct number is %o\n", i*i, i);
}
return 0;
}
//Press ENTER or type command to continue
//The license is dex 22500 and the dex number is 150
//The license is dex 44100 and the dex number is 210
//The license is hex 44100 and the hex number is 210
//The license is hex 55900 and the hex number is 250
//The license is hex 77499 and the hex number is 2bb
//The license is oct 22000 and the oct number is 140
//The license is oct 33144 and the oct number is 166
//The license is oct 44100 and the oct number is 210
//The license is oct 55100 and the oct number is 230
//Problem 2
#include
#include//header file for malloc
// header
typedef struct element * Element;
typedef struct element * List;
//typedef int Item;
struct element{
char item1;
char item2;
Element next;
};
// header_end
// init linked list; dummy head, NULL tail
void init(List list)
{
list->next = NULL;
}
//push element into linked list
void push(List list, Element item){
item->next = list->next;
list->next = item;
}
//print current list
void print(List list){
Element curr;
for ( curr = list->next; curr != NULL; curr=curr->next){
printf("%c %c\n",curr->item1,curr->item2);
}
}
//code char
char charcode(List list, char c)
{
Element curr=(Element)malloc(sizeof(Element));
curr=list;
while(curr != NULL)
{
if (curr->item1 == c){
return curr->item2;
free(curr);
break;
}
curr = curr ->next;
}
free(curr);
return '\0';
}
//decode char
char chardecode(List list, char c)
{
Element curr=(Element)malloc(sizeof(Element));
curr=list;
while(curr != NULL)
{
if (curr->item2 == c){
return curr->item1;
free(curr);
break;
}
curr = curr->next;
}
free(curr);
return '\0';
}
//update the linked list with array source and key
void update(List list, char *source, char *key){
int i;
for (i = 0; i < 32; i++){
// new element
Element new;
new = (Element)malloc(sizeof(Element));
new->item1 = source[i];
new->item2 = key[i];
//add new element
push(list, new);
}
}
// check if the char is among the source
int charcheck( char * source, int c)
{
int i;
for (i = 0; i < 32; i++)
if (c == source[i]){
return 1;
break;}
return 0;
}
int main()
{
// source array and key array
char source[] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ .,?!;"};
char key[] = {"?Q.W,EMRNTBXYUV!ICO PZA;SDLFKGJH"};
//Initial list
//Dummy head, NULL tail
List list;
list = (List)malloc(sizeof(List));
init(list);
//update the link list with the source and key array
update(list,source,key);
//HMI
printf("Pls select the work mode, 1 for code | 0 for decode\n");
int mode;//mode 1:code 0:decode
do{
scanf("%d",&mode);
if (mode == 1){
printf("Code mode\n");
}else if(mode == 0){
printf("Decode mode\n");
}else{
printf("%d is invalid mode, pls input the correct mode:\n",mode);
}
}
while(mode != 1 && mode != 0);
//start input
printf("Pls input the word and end with Enter\nIf you want to exit input EOF(Ctrl+D)\n");
int c;
// int caseflag = 1; // 1: Capital 0:lower
while ((c = getchar()) != EOF){
if (c >= 'a' && c <= 'z') // lower case to Capitial case
{
c = c + 'A'-'a';
// caseflag = 0;
}
switch(mode){
case 1: // code
if (charcheck(source,c))
{
if(charcode(list,c) >= 'A'&& charcode(list,c)<='Z')
putchar(charcode(list,c) + 'a'-'A');
else
putchar(charcode(list,c));
}
break;
case 0: //decode
if (charcheck(source,c))
{
if(chardecode(list,c) >= 'A'&& chardecode(list,c)<='Z')
putchar(chardecode(list,c) + 'a'-'A');
else
putchar(chardecode(list,c));
}
break;
default:
exit(1);
break;
}
}
return 0;
}
第一题
#include
void main()
{
int j,i;
for(i=1;i<99999;i++)
{
for(j=1;j<317;j++)
{
if(i/10000==i/1000%10&&i%10==i/10%10&&i==j*j&&i/10000!=i%10)
printf("符合条件的车牌有%d\n",i);
}
}
}
弄错了修改了一下