to lostinet:阿哥教训的是.我详细说一下:就是在命令行下输入四个数字(1到12之间的)然后通过穷举法算出24这个结果来,并输出算法的过程比方说我输入的是:3,3,8,8..输出是8/(3-8/3)..阿哥给个提示!最好能给我源程序看一下...
解决方案 »
- C#socket数据库服务器数据一更新服务器把更新的数据发送客户端要怎么实现的?
- 串口接收数据的问题
- 引用问题
- c#一键打开某个窗口
- 请教个操作PDF的代码!!
- 修改CS 发布DLL后上传到虚拟主机,是不是DLL的覆盖或整个网站的覆盖都一定要重启IIS??
- 请问有什么办法知道一个DLL文件里面封装了点什么方法吗?
- 请问该如何解决“试图运行项目时出错:无法在WEB服务器上启动调试.未将项目配置为进行调试。”问题?
- 求助下Dev的PropertyGridControl的绑定问题
- 特别怪异,VS里不同的复制方式File.Exists直接结果不一样,求根源,谢谢。
- 现在没人知道如何用C#来拔号上网(MODEM)吗,怎么写吗?是不是调用API? 请速告之, 万分感谢!
- 请教高手: 我写了一个邮件接收程序, 碰到一个两难抉择的问题: 把接收的附件存入数据库呢? 还是当成文件存到硬碟上呢?
#include <iostream>
#include <string>
#include <cmath>using namespace std;const double PRECISION = 1E-6;
const int COUNT_OF_NUMBER = 4;
const int NUMBER_TO_BE_CAL = 24;double number[COUNT_OF_NUMBER];
string expression[COUNT_OF_NUMBER];bool Search(int n)
{
if (n == 1) {
if ( fabs(number[0] - NUMBER_TO_BE_CAL) < PRECISION ) {
cout << expression[0] << endl;
return true;
} else {
return false;
}
} for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
double a, b;
string expa, expb; a = number[i];
b = number[j];
number[j] = number[n - 1]; expa = expression[i];
expb = expression[j];
expression[j] = expression[n - 1]; expression[i] = '(' + expa + '+' + expb + ')';
number[i] = a + b;
if ( Search(n - 1) ) return true;
expression[i] = '(' + expa + '-' + expb + ')';
number[i] = a - b;
if ( Search(n - 1) ) return true;
expression[i] = '(' + expb + '-' + expa + ')';
number[i] = b - a;
if ( Search(n - 1) ) return true;
expression[i] = '(' + expa + '*' + expb + ')';
number[i] = a * b;
if ( Search(n - 1) ) return true; if (b != 0) {
expression[i] = '(' + expa + '/' + expb + ')';
number[i] = a / b;
if ( Search(n - 1) ) return true;
}
if (a != 0) {
expression[i] = '(' + expb + '/' + expa + ')';
number[i] = b / a;
if ( Search(n - 1) ) return true;
} number[i] = a;
number[j] = b;
expression[i] = expa;
expression[j] = expb;
}
}
return false;
}void main()
{
for (int i = 0; i < COUNT_OF_NUMBER; i++) {
char buffer[20];
int x;
cin >> x;
number[i] = x;
itoa(x, buffer, 10);
expression[i] = buffer;
} if ( Search(COUNT_OF_NUMBER) ) {
cout << "Success." << endl;
} else {
cout << "Fail." << endl;
}
}
I Apologize..
函数就两个:Search和Button1_Click。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace WindowsApplication6{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private double PRECISION = 0.000001;
private int COUNT_OF_NUMBER = 4;
private int NUMBER_TO_BE_CAL = 24; private double[] number = new double[4];
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;
private string[] expression = new string[4]; private bool Search(int n) {
if (n == 1){
if(Math.Abs(number[0] - NUMBER_TO_BE_CAL)<PRECISION){
// cout << expression[0] << endl;
return true;
}
else{
return false;
}
} for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
double a, b;
string expa, expb; a = number[i];
b = number[j];
number[j] = number[n - 1]; expa = expression[i];
expb = expression[j];
expression[j] = expression[n - 1]; expression[i] = '(' + expa + '+' + expb + ')';
number[i] = a + b;
if ( Search(n - 1) ) return true;
expression[i] = '(' + expa + '-' + expb + ')';
number[i] = a - b;
if ( Search(n - 1) ) return true;
expression[i] = '(' + expb + '-' + expa + ')';
number[i] = b - a;
if ( Search(n - 1) ) return true;
expression[i] = '(' + expa + '*' + expb + ')';
number[i] = a * b;
if ( Search(n - 1) ) return true; if (b != 0) {
expression[i] = '(' + expa + '/' + expb + ')';
number[i] = a / b;
if ( Search(n - 1) ) return true;
}
if (a != 0) {
expression[i] = '(' + expb + '/' + expa + ')';
number[i] = b / a;
if ( Search(n - 1) ) return true;
} number[i] = a;
number[j] = b;
expression[i] = expa;
expression[j] = expb;
}
}
return false;
} public Form1(){
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent call
//
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing ){
if( disposing ){
if (components != null){
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent(){
this.listBox1 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.Location = new System.Drawing.Point(16, 8);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(552, 264);
this.listBox1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(240, 296);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(584, 341);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.listBox1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(){
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e) {
int count = 0;
string tmp; for(int a=1;a<13;a++){
for(int b=1;b<13;b++){
for(int c=1;c<13;c++){
for(int d=1;d<13;d++){
number[0] = Convert.ToDouble(a);
number[1] = Convert.ToDouble(b);
number[2] = Convert.ToDouble(c);
number[3] = Convert.ToDouble(d); tmp = "";
for(int i=0;i<4;i++){
expression[i] = Convert.ToString(number[i]);
tmp += expression[i];
tmp += ",";
}
tmp = tmp.Substring(0,tmp.Length - 1); if(Search(4)==true){
count++;
if(count>100)return;
listBox1.Items.Add(tmp + ":" + expression[0]);
}
}
}
}
}
}
}
}
哦,那你能够也发给我过来吗?谢谢!!!
哦,那你能够也发给我过来吗?谢谢!!!
[email protected]
阿哥,谢谢你!