一道C++ 的编程题目:输出所有满足要求的算式
由4个不同的数字,组成的一个乘法算式,它们的乘积仍然由这4个数字组成。比如:210x6=1260,8x473=3784,27x81=2187都符合要求。如果满足乘法交换律...
由4个不同的数字,组成的一个乘法算式,它们的乘积仍然由这4个数字组成。
比如:210 x6 = 1260,8 x473 = 3784,27 x81 = 2187都符合要求。
如果满足乘法交换律的算式算作同一种情况,那么一共有多少种满足要求的算式呢? 展开
比如:210 x6 = 1260,8 x473 = 3784,27 x81 = 2187都符合要求。
如果满足乘法交换律的算式算作同一种情况,那么一共有多少种满足要求的算式呢? 展开
展开全部
您好,这样的:
//#include "stdafx.h"//vc++6.0加上这一行.
#include <iostream>
using namespace std;
typedef struct abc{
char p;
unsigned x;
int n;
abc *next;
}*NODE,node;
void freenode(NODE p){
if(p->next) freenode(p->next);
else delete p;
}
NODE CreateLink(void){
char ch;
NODE head,p;
if((p=head=new node)==NULL){
cout << "Establish a head node failure...\n";
return NULL;
}
cout << "Enter commands & data('#' end)...\n";
while(1){
cin >> ch;
if(ch=='#') break;
if((p->next=new node)==NULL){
cout << "Failed to create node...\n";
freenode(head);
return NULL;
}
p->next->p=ch;
cin >> p->next->x >> p->next->n;
p=p->next;
}
p->next=NULL;
p=head;
head=head->next;
delete p;
return head;
}
void Processing(NODE p){
for(;p;p=p->next){
switch(p->p){
case 'L':
cout << (p->x & ((1<<p->n)-1)) << endl;
break;
case 'C':
cout << (p->x & (~((1<<p->n)-1))) << endl;
break;
case 'S':
cout << (p->x | ((1<<p->n)-1)) << endl;
break;
case 'R':
cout << (p->x ^ ((1<<p->n)-1)) << endl;
break;
case 'B':
cout << (p->x ^ (~(1<<p->n)-1)) << endl;
break;
case 'G':
cout << ((p->x & (1<<p->n)) || 0) << endl;
break;
case 'Z':
cout << (p->x & (~(1<<p->n))) << endl;
break;
case 'A':
cout << (p->x | (1<<p->n)) << endl;
break;
case 'E':
cout << (p->x ^ (1<<p->n)) << endl;
break;
default:
cout << "Error...\n";
break;
}
}
}
void main(void){
NODE p;
Processing(p=CreateLink());
freenode(p);
}
//#include "stdafx.h"//vc++6.0加上这一行.
#include <iostream>
using namespace std;
typedef struct abc{
char p;
unsigned x;
int n;
abc *next;
}*NODE,node;
void freenode(NODE p){
if(p->next) freenode(p->next);
else delete p;
}
NODE CreateLink(void){
char ch;
NODE head,p;
if((p=head=new node)==NULL){
cout << "Establish a head node failure...\n";
return NULL;
}
cout << "Enter commands & data('#' end)...\n";
while(1){
cin >> ch;
if(ch=='#') break;
if((p->next=new node)==NULL){
cout << "Failed to create node...\n";
freenode(head);
return NULL;
}
p->next->p=ch;
cin >> p->next->x >> p->next->n;
p=p->next;
}
p->next=NULL;
p=head;
head=head->next;
delete p;
return head;
}
void Processing(NODE p){
for(;p;p=p->next){
switch(p->p){
case 'L':
cout << (p->x & ((1<<p->n)-1)) << endl;
break;
case 'C':
cout << (p->x & (~((1<<p->n)-1))) << endl;
break;
case 'S':
cout << (p->x | ((1<<p->n)-1)) << endl;
break;
case 'R':
cout << (p->x ^ ((1<<p->n)-1)) << endl;
break;
case 'B':
cout << (p->x ^ (~(1<<p->n)-1)) << endl;
break;
case 'G':
cout << ((p->x & (1<<p->n)) || 0) << endl;
break;
case 'Z':
cout << (p->x & (~(1<<p->n))) << endl;
break;
case 'A':
cout << (p->x | (1<<p->n)) << endl;
break;
case 'E':
cout << (p->x ^ (1<<p->n)) << endl;
break;
default:
cout << "Error...\n";
break;
}
}
}
void main(void){
NODE p;
Processing(p=CreateLink());
freenode(p);
}
追问
好像是用链表,这方面我不太懂,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询