Galou is back! 类欧几里得算法 线段树方面的一个问题的思路求教,怎么利用C语言的编写实现

roblem Description
The famous witch is back. After killing an incredible amount of monsters in order to find a hidden treasure, Zak Galou decided to buy vineyards in Burgundy and retired. Everything was calm in his new life, until the day that his farm tractor stopped working. His tractor’s engine works based on a mechanism of gears. The engine can be represented by a bidimensional grid. At most one gear can be attached to each position of the grid.

All the gears are identical and can mesh with adjacent gears. In this grid, a gear can have up to six other adjacent gears, see figure below:

Under normal utilization, when the tractor is started, some of the gears are initially activated and try to turn clockwise. When a gear tries to turn in one direction, all the adjacent gears try to turn in the opposite direction.

When Zak Galou opened his engine he noticed that it had been sabotaged (probably by a jealous treasure hunter who was not able to find the treasure). Some of the gears were removed from the engine and others have been added to it. As a consequence, some of the gears were immobile. A gear can be immobile either if it is free or if it is blocked. A gear is free when it is not an initially activated gear and no adjacent gear is trying to turn. A gear is blocked when it is trying to turn in both directions at the same time. For example, consider that there are three gears in the engine as shown in the figure below. If any of the gears is initially activated when the tractor is started, all of them will be blocked. If none of the gears are initially activated, all of them will be free.

As a part of the work of fixing his tractor, Zak Galou asks for your help to solve the following problem. Given the description of the engine and the gears that are initially activated in the clockwise direction, he wants to know for each gear, what is its state when the tractor is started:
turn clockwise, turn counter-clockwise, free or blocked.

Input
The input contains several test cases. The first line of a test case contains two integers R and C, separated by a single space, representing respectively the number of rows and columns of the engine grid (1 <= R,C <= 100). The next R lines describe the engine. The i-th line represents the i-th row of the engine and contains C characters. The character “.” indicates that there is no gear in the corresponding position, the character “*” indicates that there is a gear that is not initially activated when the engine is started and an “I” indicates that there is a gear that is initially activated when the engine is started. Notice that, for simplicity reasons, the parallelogram representing the engine grid is described in the input as if it was a rectangle with each row left aligned. The end of input is indicated by R = C = 0.

Output
For each test case, your program must output R + 1 lines. The first line must be empty; each of the following R lines must have C characters. The characters printed must represent the state of each position of the grid when the engine is started. Print a “.” if there is no gear in the position; a “(” if there is a gear turning in the clockwise direction; a “)” if there is a gear turning in the counter-clockwise direction, an uppercase “F” if there is a gear that is free and an uppercase “B” if there is a blocked gear.

Sample Input
4 3
...
.*.
.I.
...
4 4
....
.**.
.I..
..*.
0 0

Sample Output
...
.).
.(.
...

....
.BB.
.B..
..F.

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
求两个数最大公约数,欧几里得算法
求两个数最大公约数,欧几里得算法,这两种方法除第一种可以避免除数为零的情况,两者有什么区别?谢谢 public static int gcd(int p, int q) { if (q == 0) return p; int r = p % q; return gcd(q, r) ; } public static int gcd(int p, int q) { int r = p % q; if (r == 0) return q; return gcd(q, r); }
C语言 欧几里得算法运行时出现的问题
当我输入5 10 的时候正常反馈 5,反过来时候 反馈4199664 ?求解释?谢过了 我用的是Dev c++ ``` #include<stdio.h> int gcd(int a,int b) { return b ? gcd(b,a%b) : a; } int main() { int a,b; while(scanf("%d%d",&a,&b)!=EOF) printf("%d\n",a>b?gcd(a,b):gcd(b,a)); return 0; } ```
双调欧几里得旅行商问题
Description John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane and starts visiting beautiful places. To save money, John must determine the shortest closed tour that connects his destinations. Each destination is represented by a point in the plane pi = < xi,yi >. John uses the following strategy: he starts from the leftmost point, then he goes strictly left to right to the rightmost point, and then he goes strictly right back to the starting point. It is known that the points have distinct x-coordinates. Write a program that, given a set of n points in the plane, computes the shortest closed tour that connects the points according to John's strategy. Input The program input is from a text file. Each data set in the file stands for a particular set of points. For each set of points the data set contains the number of points, and the point coordinates in ascending order of the x coordinate. White spaces can occur freely in input. The input data are correct. Output For each set of data, your program should print the result to the standard output from the beginning of a line. The tour length, a floating-point number with two fractional digits, represents the result. An input/output sample is in the table below. Here there are two data sets. The first one contains 3 points specified by their x and y coordinates. The second point, for example, has the x coordinate 2, and the y coordinate 3. The result for each data set is the tour length, (6.47 for the first data set in the given example). Sample Input 3 1 1 2 3 3 1 4 1 1 2 3 3 1 4 2 Sample Output 6.47 7.89
向量和欧几里得的距离的计算,求最短的距离,给定的点,采用C语言算法怎么实现
Problem Description The government in a foreign country is looking into the possibility of establishing a subway system in its capital. Because of practical reasons, they would like each subway line to start at the central station and then go in a straight line in some angle as far as necessary. You have been hired to investigate whether such an approach is feasible. Given the coordinates of important places in the city as well as the maximum distance these places can be from a subway station (possibly the central station, which is already built), your job is to calculate the minimum number of subway lines needed. You may assume that any number of subway stations can be built along a subway line. Figure 1: The figure above corresponds to the first data set in the example input. Input The first line in the input file contains an integer N, the number of data sets to follow. Each set starts with two integers, n and d (1 <= n <= 500, 0 <= d < 150). n is the number of important places in the city that must have a subway station nearby, and d is the maximum distance allowed between an important place and a subway station. Then comes n lines, each line containing two integers x and y (-100 <= x, y <= 100), the coordinates of an important place in the capital. The central station will always have coordinates 0, 0. All pairs of coordinates within a data set will be distinct (and none will be 0, 0). Output For each data set, output a single integer on a line by itself: the minimum number of subway lines needed to make sure all important places in the city is at a distance of at most d from a subway station. Sample Input 2 7 1 -1 -4 -3 1 -3 -1 2 3 2 4 2 -2 6 -2 4 0 0 4 -12 18 0 27 -34 51 Sample Output 4 2
运筹学方面的一个问题,这里计算的是欧几里得距离,怎么采用C语言,不太会
Problem Description Roy has just moved into a new apartment. Well, actually the apartment itself is not very new, even dating back to the days before people had electricity in their houses. Because of this, Roy's apartment has only one single wall outlet, so Roy can only power one of his electrical appliances at a time. Roy likes to watch TV as he works on his computer, and to listen to his HiFi system (on high volume) while he vacuums, so using just the single outlet is not an option. Actually, he wants to have all his appliances connected to a powered outlet, all the time. The answer, of course, is power strips, and Roy has some old ones that he used in his old apartment. However, that apartment had many more wall outlets, so he is not sure whether his power strips will provide him with enough outlets now. Your task is to help Roy compute how many appliances he can provide with electricity, given a set of power strips. Note that without any power strips, Roy can power one single appliance through the wall outlet. Also, remember that a power strip has to be powered itself to be of any use. Input Input will start with a single integer 1 <= N <= 20, indicating the number of test cases to follow. Then follow N lines, each describing a test case. Each test case starts with an integer 1 <= K <= 10, indicating the number of power strips in the test case. Then follow, on the same line, K integers separated by single spaces, O1 O2 . . . OK, where 2 <= Oi <= 10, indicating the number of outlets in each power strip. Output Output one line per test case, with the maximum number of appliances that can be powered. Sample Input 3 3 2 3 4 10 4 4 4 4 4 4 4 4 4 4 4 10 10 10 10 Sample Output 7 31 37
可以来给我看看程序么,有些不太懂
UpdateData(TRUE); int x[4],y[4],t[4],q,i; x[1]=1; x[2]=0; y[1]=0; y[2]=1; x[3]=m_Qn; y[3]=m_e; while(y[3]!=1) { if(y[3]==0) { AfxMessageBox("密钥不存在"); break; } q=x[3]/y[3];//算法的核心 for(i=1;i<4;i++) { t[i]=x[i]-q*y[i]; x[i]=y[i]; y[i]=t[i]; }//算法的核心 } if(y[3]==1) {//判断退出while的原因 if(y[2]<0) //这个判断语句是我自己添的,使m_d为大于零的数且不影响要求的。 { y[2]=m_Qn+y[2]; } m_d=y[2]; } UpdateData(FALSE); 中间的循环看不懂,是怎样利用欧几里得算法的啊??????
。。。。。。。。。。紧急求援!!!
package discuz; import java.io.File; import weka.clusterers.SimpleKMeans; import weka.core.DistanceFunction; import weka.core.EuclideanDistance; import weka.core.Instances; import weka.core.converters.ArffLoader; public class kmeans { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Instances ins = null; Instances tempIns = null; SimpleKMeans KM = null; DistanceFunction disFun = null; try{ /* * 1.读入样本 */ File file= new File("D://weka//Weka-3-6//data//contact-lenses.arff"); ArffLoader loader = new ArffLoader(); loader.setFile(file); ins = loader.getDataSet(); /* * 2.初始化聚类器 * 在3.6版本可以通过setDistanceFunction(DistanceFunction df) * 函数设置聚类算法内部的距离计算方式 * 而在3.5版本里面默认的采用了欧几里得距离 */ KM = new SimpleKMeans(); //设置聚类要得到的类别数量 KM.setNumClusters(2); /* * 3.使用聚类算法对样本进行聚类 */ KM.buildClusterer(ins); /* * 4.打印聚类结果 */ tempIns = KM.getClusterCentroids(); System.out.println("CentroIds: " + tempIns); }catch(Exception e){ e.printStackTrace(); } } } 为啥会出现这个呀?怎么解决呀啊? java.io.FileNotFoundException: \iris.2.arff (系统找不到指定的文件。) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(Unknown Source) at java.io.FileInputStream.<init>(Unknown Source) at java.io.FileReader.<init>(Unknown Source) at weka.classifiers.bayes.net.ADNode.main(ADNode.java:270)
求各位大佬棒棒我!!!
程序的加减乘除法都不对,我要哭了,帮我看看,谢谢啦 我怀疑我的构造函数不正确,.... #include<iostream.h> #include<stdlib.h> #include<cstdio> class Num { public: Num(int num, int denom); //初始化数据 friend ostream & operator<<(ostream &cout, Num &n); //重载<<运算符 friend istream & operator>>(istream &cin, Num &n); //重载>>运算符 friend Num operator + (Num n1, Num n2); //重载+运算符 friend Num operator - (Num n1, Num n2); //重载-运算符 friend Num operator * (Num n1, Num n2); //重载*运算符 friend Num operator / (Num n1, Num n2); //重载/运算符 friend bool operator == (Num n1, Num n2); //重载真假运算符,c++中用bool代表真假 void huajian(); //定义化简函数 Num huajian1(); friend void realPrintf(Num &n); //定义了一个将有理数转换为实数的函数 private: int denom; int num; }; //Num(int num, int denom); void realPrintf(Num &n) //将有理数输出为实数 { double m = double(n.num) / double(n.denom); //强制转换为实型数,再进行除法运算 cout << m << endl; } Num::Num(int n, int d) { num = n; if (denom != 0) //分母不能为0 { denom = d; // 指向被调用成员函数所属对象 } else denom = 1; huajian(); } void Num::huajian() //化简有理数 { if (denom < 0) //确保分母为正数 { num = -num; denom =- denom; } int x = abs(num); //用欧几里得算法求出分子分母的最大公约数 int y = abs(denom); while (y > 0) { int temp = x % y; x = y; y = temp; } num /= x; denom /= x; //Num N(num /= x, denom /= x); //return N; } Num Num::huajian1() { if (denom < 0) //确保分母为正数 { num = -num; denom =- denom; } int x = abs(num); //用欧几里得算法求出分子分母的最大公约数 int y = abs(denom); while (y > 0) { int temp = x % y; x = y; y = temp; } Num N(num /= x, denom /= x); return N; } Num operator + (Num n1, Num n2) { int a = n1.num; int b = n1.denom; int c = n2.num; int d = n2.denom; int e = a * d + b * c; int f = b * d; Num N(e, f); return N; } Num operator - (Num n1, Num n2) { n2.num = -n2.num; return operator + (n1, n2); } Num operator * (Num n1, Num n2) { int a = n1.num; int b = n1.denom; int c = n2.num; int d = n2.denom; int e = a * c; int f = b * d; Num N(e, f); return N; } Num operator / (Num n1, Num n2) { int temp = n2.num; n2.num = n2.denom; n2.denom = temp; return operator * (n1, n2); } ostream & operator<<(ostream &cout, Num &n) { if (n.num / n.denom == 0) { cout << n.num / n.denom; } else { cout << n.num << "\\" << n.denom; } return cout; } istream & operator>>(istream &cin, Num &n) { cin >> n.num >> n.denom; if (n.denom != 0) { return cin; } else { cout << "输入有误"; exit(0); } } bool operator == (Num n1, Num n2) //判断两个数是否相等,c++中用bool代表真和假 { //return double(Num.n1) == (double)N.n2 ? true : false; if ( n1.num == n2.num && n2.denom == n1.denom) { //cout << "两数相等" << endl; return true; } else { //cout << "两个有理数不相等" << endl; return false; } } int main() { Num n1(1, 9); Num n2(1, 2); Num n3(5, 8); char m; char p; int k; cout << "***************有理数计算器***************" << endl; cout << "***********是否进入系统?(Y / N)**********" << endl; //提示是否进入有理数计算器的系统 cin >> m; if ( m == 'N' && m == 'n') { cout << "不进入系统" << endl; } else { cout << "请输入两个有理数" << endl; } cout << "请输入第一个数:" << endl; cin >> n1; cout << "请输入第二个数:" << endl; cin >> n2; do{ cout << "************ 1、有理数加法运算************" << endl; cout << "************ 2、有理数减法运算************" << endl; cout << "************ 3、有理数乘法运算************" << endl; cout << "************ 4、有理数除法运算************" << endl; cout << "************ 5、有理数的判断**************" << endl; cout << "************ 6、有理数的化简**************" << endl; cout << "************ 7、有理数变成实数************" << endl; cout << "************ 8、重新输入有理数************" << endl; cout << "************ 9、程序结束******************" << endl; cout << "************ 请输入选择:" ; cin >> k; switch (k) { case 1: n3 = n1 + n2; cout << n3 << endl; break; case 2: n3 = n1 - n2; cout << n3 << endl; break; case 3: n3 = n1 * n2; cout << n3 << endl; break; case 4: n3 = n1 / n2; cout << n3 << endl; break; case 5: if (n1 == n2) { cout << "两个有理数相等" << endl; } else { cout << "两个有理数不相等" << endl; } break; case 6: n1 = n1.huajian1(); n2 = n2.huajian1(); cout << n1 << endl; cout << n2 << endl; break; case 7: cout << "n1 = " ; realPrintf(n1); cout << "n2 = " ; realPrintf(n2); break; case 8: cout << "请输入第一个数:" << endl; cin >> n1; cout << "请输入第二个数:" << endl; cin >> n2; break; case 9: break; default: if (k != 1, 2, 3, 4, 5, 6, 7, 8, 9) { cout << "输入有误" << endl; break; } else { cout << "谢谢使用有理数计算器" << endl; break; } } cout << "**********是否继续选择步骤操作**********" << endl; do { cin >> p; } while (p != 'y'&& p != 'Y' && p != 'n' && p != 'N'); } while (m == 'y' || m == 'Y'); //system("pause"); return 0; }
大家帮忙看看我这段代码逻辑是不是有问题?
#include "stdafx.h" #include "KNN.h" #include<math.h> #include <fstream> #include <iostream> #include <algorithm> //设置K值 void KNN::setK() { // cout << "请输入K:"; // cin >> K; } //读入训练集数据,100组数据,108维,10类 bool KNN::ReadTraining(char *filename) { ifstream fin; fin.open(filename,ios::in); if( fin.fail() ) { cerr << "File open error1." << endl; return false; } for(int i=0; i<100; i++) { Traindata m_tmp; for(int j=0; j<108; j++) { double dm_tmp; fin >> dm_tmp; m_tmp.first.push_back(dm_tmp); } // int nm_tmp; if(i<10) m_tmp.second = 0; else if(i<20) m_tmp.second = 1; else if(i<30) m_tmp.second = 2; else if(i<40) m_tmp.second = 3; else if(i<50) m_tmp.second = 4; else if(i<60) m_tmp.second = 5; else if(i<70) m_tmp.second = 6; else if(i<80) m_tmp.second = 7; else if(i<90) m_tmp.second = 8; else if(i<100) m_tmp.second = 9; training_data.push_back( m_tmp ); } return true; } //读入测试集数据,8组6维数据 bool KNN::ReadTest(char *filename) { ifstream fin(filename); if( fin.fail() ) { cerr << "File open error." << endl; return false; } for(int i=0; i<8; i++) { vector<double> m_tmp; test_data.push_back(m_tmp); for(int j=0; j<108; j++) { double dm_tmp; fin >> dm_tmp; test_data[i].push_back( dm_tmp ); } } return true; } double KNN:: computeEuclidDistance(vector<double>&a, vector<double> &b) { double distance = 0.0; for(int i=0; i<108; i++) { distance += ( a[i]-b[i] ) * ( a[i]-b[i] ); } return distance; } //KNN分类函数 int KNN::classify(vector<double> & testData) { vector< Dis > distances; //计算testData到训练集每个点的欧几里得距离 for(int i=0; i<test_data.size(); i++) { Dis m_tmp; m_tmp.first = computeEuclidDistance(testData, training_data[i].first); //记录分类号 m_tmp.second = training_data[i].second; distances.push_back(m_tmp); } //对计算出的欧几里得距离排序,从小到大 sort(distances.begin(),distances.end()); //对于前K个,即距离最近的k个点,返回包含点数最多的分类号,即该点的分类 vector<int> poll( 10, 0 ); for(i=0; i<5; i++)//K=5 { int vot = distances[i].second; poll[vot] ++; } int res = -1; int vots = -1; for(i=1; i<=10; i++) { if(vots < poll[i]) { vots = poll[i]; res = i; } } CString str,str1; for(i=0;i<8;i++) { // if(poll[i]=8) str.Format("%d ",poll[i]); str1+=str; } ::MessageBox(NULL,str1,"识别结果",NULL); return res; } //打印测试集判断结果 void KNN::PrintTestResult(char *filename) { ofstream fout(filename); for(int i=0; i<test_data.size(); i++) { int clsf = classify(test_data[i]); for(int j=0; j<test_data[i].size(); j++) { fout << test_data[i][j] << "\t"; } fout << clsf << endl; } fout.close(); cout << "结果数据保存在文件out1.txt中,请查看。\n" ; }
大佬帮我看看我的程序为什么不正确
程序一直不对,好难受啊谢谢大家 #include<iostream> #include<stdlib.h> #include<cstdio> using namespace std; class Num { public: Num(int num, int denom); //初始化数据 friend ostream & operator<<(ostream &cout, Num &n); //重载<<运算符 friend istream & operator>>(istream &cin, Num &n); //重载>>运算符 friend Num operator + (Num n1, Num n2); //重载+运算符 friend Num operator - (Num n1, Num n2); //重载-运算符 friend Num operator * (Num n1, Num n2); //重载*运算符 friend Num operator / (Num n1, Num n2); //重载/运算符 friend bool operator == (Num n1, Num n2); //重载真假运算符,c++中用bool代表真假 Num huajian(); //定义化简函数 Num huajian1(); friend void realPrintf(Num &n); //定义了一个将有理数转换为实数的函数 private: int denom; int num; }; //Num(int num, int denom); void realPrintf(Num &n) //将有理数输出为实数 { double m = double(n.num) / double(n.denom); //强制转换为实型数,再进行除法运算 cout << m << endl; } Num::Num(int num, int denom) { if (denom != 0) //分母不能为0 { this->denom = denom; // 指向被调用成员函数所属对象 } else denom = 1; huajian(); } Num Num::huajian() //化简有理数 { if (denom < 0) //确保分母为正数 { num = -num; denom =- denom; } int x = abs(num); //用欧几里得算法求出分子分母的最大公约数 int y = abs(denom); while (y > 0) { int temp = x % y; x = y; y = temp; } num /= x; denom /= x; //Num N(num /= x, denom /= x); //return N; } Num Num::huajian1() { if (denom < 0) //确保分母为正数 { num = -num; denom =- denom; } int x = abs(num); //用欧几里得算法求出分子分母的最大公约数 int y = abs(denom); while (y > 0) { int temp = x % y; x = y; y = temp; } Num N(num /= x, denom /= x); return N; } Num operator + (Num n1, Num n2) { int a = n1.num; int b = n1.denom; int c = n2.num; int d = n2.denom; int e = a * d + b * c; int f = b * d; Num N(e, f); return N; } Num operator - (Num n1, Num n2) { n2.num = -n2.num; return operator + (n1, n2); } Num operator * (Num n1, Num n2) { int a = n1.num; int b = n1.denom; int c = n2.num; int d = n2.denom; int e = a * c; int f = b * d; Num N(e, f); return N; } Num operator / (Num n1, Num n2) { int temp = n2.num; n2.num = n2.denom; n2.denom = temp; return operator * (n1, n2); } ostream & operator<<(ostream &cout, Num &n) { if (n.num / n.denom == 0) { cout << n.num / n.denom; } else { cout << n.num << "\\" << n.denom; } return cout; } istream & operator>>(istream &cin, Num &n) { cin >> n.num >> n.denom; if (n.denom != 0) { return cin; } else { cout << "输入有误"; exit(0); } } bool operator == (Num n1, Num n2) //判断两个数是否相等,c++中用bool代表真和假 { //return double(Num.n1) == (double)N.n2 ? true : false; if ( n1.num == n2.num && n2.denom == n1.denom) { //cout << "两数相等" << endl; return true; } else { //cout << "两个有理数不相等" << endl; return false; } } int main() { Num n1(1, 9); Num n2(1, 2); Num n3(5, 8); char m; char p; int k; cout << "***************有理数计算器***************" << endl; cout << "***********是否进入系统?(Y / N)**********" << endl; //提示是否进入有理数计算器的系统 cin >> m; if ( m == 'N' && m == 'n') { cout << "不进入系统" << endl; } else { cout << "请输入两个有理数" << endl; } cout << "请输入第一个数:" << endl; cin >> n1; cout << "请输入第二个数:" << endl; cin >> n2; do{ cout << "************ 1、有理数加法运算************" << endl; cout << "************ 2、有理数减法运算************" << endl; cout << "************ 3、有理数乘法运算************" << endl; cout << "************ 4、有理数除法运算************" << endl; cout << "************ 5、有理数的判断**************" << endl; cout << "************ 6、有理数的化简**************" << endl; cout << "************ 7、有理数变成实数************" << endl; cout << "************ 8、重新输入有理数************" << endl; cout << "************ 9、程序结束******************" << endl; cout << "************ 请输入选择:" ; cin >> k; switch (k) { case 1: n3 = n1 + n2; cout << n3 << endl; break; case 2: n3 = n1 - n2; cout << n3 << endl; break; case 3: n3 = n1 * n2; cout << n3 << endl; break; case 4: n3 = n1 / n2; cout << n3 << endl; break; case 5: if (n1 == n2) { cout << "两个有理数相等" << endl; } else { cout << "两个有理数不相等" << endl; } break; case 6: n1 = n1.huajian1(); n2 = n2.huajian1(); cout << n1 << endl; cout << n2 << endl; break; case 7: cout << "n1 = " ; realPrintf(n1); cout << "n2 = " ; realPrintf(n2); break; case 8: cout << "请输入第一个数:" << endl; cin >> n1; cout << "请输入第二个数:" << endl; cin >> n2; break; case 9: break; default: if (k != 1, 2, 3, 4, 5, 6, 7, 8, 9) { cout << "输入有误" << endl; break; } else { cout << "谢谢使用有理数计算器" << endl; break; } } cout << "**********是否继续选择步骤操作**********" << endl; do { cin >> p; } while (p != 'y'&& p != 'Y' && p != 'n' && p != 'N'); } while (m == 'y' || m == 'Y'); //system("pause"); //return 0; }
求多个二维数字的欧几里得的距离,分别求出每个距离长度,采用的C 语言的编程?
Problem Description PM Room defines a sequence A = {A1, A2,..., AN}, each of which is either 0 or 1. In order to beat him, programmer Moor has to construct another sequence B = {B1, B2,... , BN} of the same length, which satisfies that: Input The input consists of multiple test cases. The number of test cases T(T<=100) occurs in the first line of input. For each test case: The first line contains a single integer N (1<=N<=100000), which denotes the length of A and B. The second line consists of N integers, where the ith denotes Ai. Output Output the minimal f (A, B) when B is optimal and round it to 6 decimals. Sample Input 4 9 1 1 1 1 1 0 0 1 1 9 1 1 0 0 1 1 1 1 1 4 0 0 1 1 4 0 1 1 1 Sample Output 1.428571 1.000000 0.000000 0.000000
求C++大佬帮我看看,试了很多次还是没办法实现6.7.8的菜单功能
#include<iostream> #include<stdlib.h> #include<cstdio> using namespace std; class Rational { public: Rational(int numerator, int denominator); friend Rational operator+(Rational r1,Rational r2); friend Rational operator-(Rational r1,Rational r2); friend Rational operator*(Rational r1,Rational r2); friend Rational operator/(Rational r1,Rational r2); friend istream &operator>>(istream &in, Rational& r); friend ostream &operator<<(ostream &out, Rational& r); friend void equeal(Rational &r1,Rational &r2); friend void printReal(Rational &r); private:; void normalize(); int numerator; int denominator; }; void equeal(Rational &r1,Rational &r2) { if((r1.denominator ==r2.denominator)&& (r1.numerator == r2.numerator)) cout<<"两数相等"<<endl; else cout<<"两数不相等"<<endl; } void printReal(Rational &r) { double n = r.numerator/r.denominator; cout<<n<<endl; } Rational::Rational(int num, int denom) { numerator = num; denominator = denom; normalize(); } //标准化 void Rational::normalize() { //求出分子和分母的最大公约数,用欧几里得算法 int a = abs(numerator); int b = abs(denominator); while (b>0) { int t = a%b; a = b; b = t; } numerator /= a; denominator /= a; } Rational operator+(Rational r1,Rational r2) { int a = r1.numerator; int b = r1.denominator; int c = r2.numerator; int d = r2.denominator; int e = a*d + b*c; int f = b*d; Rational R(e,f); return R; } Rational operator-(Rational r1,Rational r2) { r2.numerator = -r2.numerator; return operator+(r1,r2); } Rational operator*(Rational r1,Rational r2) { int a = r1.numerator; int b = r1.denominator; int c = r2.numerator; int d = r2.denominator; int e = a*c; int f = b*d; Rational R(e, f); return R; } Rational operator/(Rational r1,Rational r2) { int t = r2.numerator; r2.numerator = r2.denominator; r2.denominator = t; return operator*(r1,r2); } istream &operator>>(istream &in, Rational& r) { in>>r.numerator>>r.denominator; if(r.denominator!=0) return in; else {cout<<"输入有误"; exit(0);} } ostream &operator<<(ostream &out, Rational& r) { if (r.numerator%r.denominator == 0) out<<r.numerator/r.denominator; else out<<r.numerator<<"\\"<<r.denominator; return out; } int main() { Rational r1(7,8); Rational r2(3,4); Rational r3(9,7); int n, peace = 0; char q; cout<<"please input numerator and denominator.\n"; cout<<"first number:"; cin>>r1; cout<<"last number:"; cin>>r2; do { cout<<" 1.改变分子和分母r.\n"; cout<<" 2.有理数加法运算\n"; cout<<" 3.有理数减法运算\n"; cout<<" 4.有理数乘法运算\n"; cout<<" 5.有理数除法运算\n"; cout<<" 6.判断有理数相等\n"; cout<<" 7.化简有理数\n"; cout<<" 8.有理数变成实数\n"; cout<<" 9.结束程序\n"; cout<<"Please input a choose:"; cin>>n; switch(n) { case 1: cout<<"first number:"; cin>>r1; cout<<"last number:"; cin>>r2; break; case 2: r3 = r1 + r2; cout<<r3<<endl; break; case 3: r3 = r1 - r2; cout<<r3<<endl; break; case 4: r3 = r1 * r2; cout<<r3<<endl; break; case 5: r3 = r1 / r2; cout<<r3<<endl; break; case 6: equeal(r1,r2); break; case 7: cout<<r1<<endl; cout<<r2<<endl; break; case 8: cout<<"r1 = "; printReal(r1); cout<<"r2 = "; printReal(r2); break; case 9: peace=1; break; default : if(n!=1,2,3,4,5,6,7,8,9) cout<<"输入有误"<<endl; break; if(peace) { cout<<"谢谢使用"; break; } } printf("\n 要继续选择吗(Y/N)?\n"); do{ cin>>q; }while(q!='Y'&&q!='y'&& q!='N'&&q!='n'); }while(q=='Y'|| q=='y'); return 0; } 为什么我的equeal函数没办法实现,为什么感觉好像没有调用私有的数据,希望可以帮我实现菜单功能的6,7,8。谢谢大家
求最近的M点
描述 每到饭点,就又到了一日几度的小L纠结去哪吃饭的时候了。因为有太多太多好吃的地方可以去吃,而小L又比较懒不想走太远,所以小L会先找到距离他最近的M家餐馆然后再做筛选。 小L现在所在的位置和每家餐馆的位置用同一笛卡尔坐标系中的点表示,而点与点之间的距离为欧几里得距离,对于点p = (p1, p2,..., pn)和点q = (q1,q2,..., qn),两者的距离定义如下 现给出在K维空间中小L所处的位置的坐标以及n个餐馆的位置,请帮助小L完成他的需求。 输入 第1行包含两个整数n和K,1≤n≤5000,1≤K≤5。 接下来n行,每行包含K个数,表示每个餐馆的坐标。 接下来1行,包含一个数t,1≤t≤10000,表示小L询问的数目。 每次询问包括两行。第1行包含K个数,表示小L所在的坐标。第2行包含一个数M,1≤M≤10。 所有坐标值不会超过10000。 输入数据包含多组数据,请逐个处理直到文件结束。 输出 对于每一个询问,输出m+1行: 第1行输出:”the closest M points are:”,其中M在输入中给出。 接下来M行输出距离最近的M家餐馆的坐标,按照由近及远的顺序输出。 输出数据保证答案唯一。保证从小L的位置到最近的M+1家餐馆位置各不相同,这说明如下输入数据: 2 2 1 1 3 3 1 2 2 1
星系碰撞
Problem Description 据预测,大约在100亿年后,狮子座星系将与银河系发生碰撞,两个星系的碰撞将会合并两个星系,但是没有2个星球会相撞。现在某科学家得到两个星系合并后的结果,一些二维平面上的点,但是不知道那些星球属于银河系,已知如果两个星球属于同一个星系,那么他们之间的距离大于5光年,这边的距离指的是欧几里得距离,即(x1,y1)与(x2,y2)的距离为sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))。现在想请你帮忙把合并后的结果分成2个集合,一个属于银河系,一个属于狮子座星系,由于集合划分的方案可能有多种,现在想知道最多有多少个星球可能属于银河系。(可以所有星球都属于银河系) 例如:如下图有6个点,你可以有以下4中划分{{1, 2, 4, 5}, {3, 6}}; {{1, 2, 3, 4}, {5, 6}}; {{1, 4,5}, {2, 3, 6}}; {{1, 3, 4}, {2, 5, 6}} ,那么可以采用第一种划分{1,2,4,5} 都属于银河系,答案为4. Input 包含多组数据每组数据输入第一行 一个整数N 表示星球个数(1<=N<=50000),接下去N 行 每行2个整数 x和y 表示星球的坐标(1<=x,y<=500000),没有重合的点。 Output 输出一行一个整数表示最多有多少个星球属于银河系。如果没办法进行划分那么输出-1。 Sample Input 61 39 111 75 713 54 4 Sample Output 4
Java学习的正确打开方式
在博主认为,对于入门级学习java的最佳学习方法莫过于视频+博客+书籍+总结,前三者博主将淋漓尽致地挥毫于这篇博客文章中,至于总结在于个人,实际上越到后面你会发现学习的最好方式就是阅读参考官方文档其次就是国内的书籍,博客次之,这又是一个层次了,这里暂时不提后面再谈。博主将为各位入门java保驾护航,各位只管冲鸭!!!上天是公平的,只要不辜负时间,时间自然不会辜负你。 何谓学习?博主所理解的学习,它是一个过程,是一个不断累积、不断沉淀、不断总结、善于传达自己的个人见解以及乐于分享的过程。
程序员必须掌握的核心算法有哪些?
由于我之前一直强调数据结构以及算法学习的重要性,所以就有一些读者经常问我,数据结构与算法应该要学习到哪个程度呢?,说实话,这个问题我不知道要怎么回答你,主要取决于你想学习到哪些程度,不过针对这个问题,我稍微总结一下我学过的算法知识点,以及我觉得值得学习的算法。这些算法与数据结构的学习大多数是零散的,并没有一本把他们全部覆盖的书籍。下面是我觉得值得学习的一些算法以及数据结构,当然,我也会整理一些看过...
大学四年自学走来,这些私藏的实用工具/学习网站我贡献出来了
大学四年,看课本是不可能一直看课本的了,对于学习,特别是自学,善于搜索网上的一些资源来辅助,还是非常有必要的,下面我就把这几年私藏的各种资源,网站贡献出来给你们。主要有:电子书搜索、实用工具、在线视频学习网站、非视频学习网站、软件下载、面试/求职必备网站。 注意:文中提到的所有资源,文末我都给你整理好了,你们只管拿去,如果觉得不错,转发、分享就是最大的支持了。 一、电子书搜索 对于大部分程序员...
linux系列之常用运维命令整理笔录
本博客记录工作中需要的linux运维命令,大学时候开始接触linux,会一些基本操作,可是都没有整理起来,加上是做开发,不做运维,有些命令忘记了,所以现在整理成博客,当然vi,文件操作等就不介绍了,慢慢积累一些其它拓展的命令,博客不定时更新 free -m 其中:m表示兆,也可以用g,注意都要小写 Men:表示物理内存统计 total:表示物理内存总数(total=used+free) use...
比特币原理详解
一、什么是比特币 比特币是一种电子货币,是一种基于密码学的货币,在2008年11月1日由中本聪发表比特币白皮书,文中提出了一种去中心化的电子记账系统,我们平时的电子现金是银行来记账,因为银行的背后是国家信用。去中心化电子记账系统是参与者共同记账。比特币可以防止主权危机、信用风险。其好处不多做赘述,这一层面介绍的文章很多,本文主要从更深层的技术原理角度进行介绍。 二、问题引入 假设现有4个人...
程序员接私活怎样防止做完了不给钱?
首先跟大家说明一点,我们做 IT 类的外包开发,是非标品开发,所以很有可能在开发过程中会有这样那样的需求修改,而这种需求修改很容易造成扯皮,进而影响到费用支付,甚至出现做完了项目收不到钱的情况。 那么,怎么保证自己的薪酬安全呢? 我们在开工前,一定要做好一些证据方面的准备(也就是“讨薪”的理论依据),这其中最重要的就是需求文档和验收标准。一定要让需求方提供这两个文档资料作为开发的基础。之后开发...
网页实现一个简单的音乐播放器(大佬别看。(⊙﹏⊙))
今天闲着无事,就想写点东西。然后听了下歌,就打算写个播放器。 于是乎用h5 audio的加上js简单的播放器完工了。 演示地点演示 html代码如下` music 这个年纪 七月的风 音乐 ` 然后就是css`*{ margin: 0; padding: 0; text-decoration: none; list-...
Python十大装B语法
Python 是一种代表简单思想的语言,其语法相对简单,很容易上手。不过,如果就此小视 Python 语法的精妙和深邃,那就大错特错了。本文精心筛选了最能展现 Python 语法之精妙的十个知识点,并附上详细的实例代码。如能在实战中融会贯通、灵活使用,必将使代码更为精炼、高效,同时也会极大提升代码B格,使之看上去更老练,读起来更优雅。
数据库优化 - SQL优化
以实际SQL入手,带你一步一步走上SQL优化之路!
2019年11月中国大陆编程语言排行榜
2019年11月2日,我统计了某招聘网站,获得有效程序员招聘数据9万条。针对招聘信息,提取编程语言关键字,并统计如下: 编程语言比例 rank pl_ percentage 1 java 33.62% 2 cpp 16.42% 3 c_sharp 12.82% 4 javascript 12.31% 5 python 7.93% 6 go 7.25% 7 p...
通俗易懂地给女朋友讲:线程池的内部原理
餐盘在灯光的照耀下格外晶莹洁白,女朋友拿起红酒杯轻轻地抿了一小口,对我说:“经常听你说线程池,到底线程池到底是个什么原理?”
《奇巧淫技》系列-python!!每天早上八点自动发送天气预报邮件到QQ邮箱
将代码部署服务器,每日早上定时获取到天气数据,并发送到邮箱。 也可以说是一个小型人工智障。 知识可以运用在不同地方,不一定非是天气预报。
经典算法(5)杨辉三角
杨辉三角 是经典算法,这篇博客对它的算法思想进行了讲解,并有完整的代码实现。
英特尔不为人知的 B 面
从 PC 时代至今,众人只知在 CPU、GPU、XPU、制程、工艺等战场中,英特尔在与同行硬件芯片制造商们的竞争中杀出重围,且在不断的成长进化中,成为全球知名的半导体公司。殊不知,在「刚硬」的背后,英特尔「柔性」的软件早已经做到了全方位的支持与支撑,并持续发挥独特的生态价值,推动产业合作共赢。 而对于这一不知人知的 B 面,很多人将其称之为英特尔隐形的翅膀,虽低调,但是影响力却不容小觑。 那么,在...
腾讯算法面试题:64匹马8个跑道需要多少轮才能选出最快的四匹?
昨天,有网友私信我,说去阿里面试,彻底的被打击到了。问了为什么网上大量使用ThreadLocal的源码都会加上private static?他被难住了,因为他从来都没有考虑过这个问题。无独有偶,今天笔者又发现有网友吐槽了一道腾讯的面试题,我们一起来看看。 腾讯算法面试题:64匹马8个跑道需要多少轮才能选出最快的四匹? 在互联网职场论坛,一名程序员发帖求助到。二面腾讯,其中一个算法题:64匹...
面试官:你连RESTful都不知道我怎么敢要你?
干货,2019 RESTful最贱实践
刷了几千道算法题,这些我私藏的刷题网站都在这里了!
遥想当年,机缘巧合入了 ACM 的坑,周边巨擘林立,从此过上了"天天被虐似死狗"的生活… 然而我是谁,我可是死狗中的战斗鸡,智力不够那刷题来凑,开始了夜以继日哼哧哼哧刷题的日子,从此"读题与提交齐飞, AC 与 WA 一色 ",我惊喜的发现被题虐既刺激又有快感,那一刻我泪流满面。这么好的事儿作为一个正直的人绝不能自己独享,经过激烈的颅内斗争,我决定把我私藏的十几个 T 的,阿不,十几个刷题网...
为啥国人偏爱Mybatis,而老外喜欢Hibernate/JPA呢?
关于SQL和ORM的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行了一番讨论,感触还是有一些,于是就有了今天这篇文。 声明:本文不会下关于Mybatis和JPA两个持久层框架哪个更好这样的结论。只是摆事实,讲道理,所以,请各位看官勿喷。 一、事件起因 关于Mybatis和JPA孰优孰劣的问题,争论已经很多年了。一直也没有结论,毕竟每个人的喜好和习惯是大不相同的。我也看...
白话阿里巴巴Java开发手册高级篇
不久前,阿里巴巴发布了《阿里巴巴Java开发手册》,总结了阿里巴巴内部实际项目开发过程中开发人员应该遵守的研发流程规范,这些流程规范在一定程度上能够保证最终的项目交付质量,通过在时间中总结模式,并推广给广大开发人员,来避免研发人员在实践中容易犯的错误,确保最终在大规模协作的项目中达成既定目标。 无独有偶,笔者去年在公司里负责升级和制定研发流程、设计模板、设计标准、代码标准等规范,并在实际工作中进行...
SQL-小白最佳入门sql查询一
不要偷偷的查询我的个人资料,即使你再喜欢我,也不要这样,真的不好;
项目中的if else太多了,该怎么重构?
介绍 最近跟着公司的大佬开发了一款IM系统,类似QQ和微信哈,就是聊天软件。我们有一部分业务逻辑是这样的 if (msgType = "文本") { // dosomething } else if(msgType = "图片") { // doshomething } else if(msgType = "视频") { // doshomething } else { // doshom...
Nginx 原理和架构
Nginx 是一个免费的,开源的,高性能的 HTTP 服务器和反向代理,以及 IMAP / POP3 代理服务器。Nginx 以其高性能,稳定性,丰富的功能,简单的配置和低资源消耗而闻名。 Nginx 的整体架构 Nginx 里有一个 master 进程和多个 worker 进程。master 进程并不处理网络请求,主要负责调度工作进程:加载配置、启动工作进程及非停升级。worker 进程负责处...
【图解经典算法题】如何用一行代码解决约瑟夫环问题
约瑟夫环问题算是很经典的题了,估计大家都听说过,然后我就在一次笔试中遇到了,下面我就用 3 种方法来详细讲解一下这道题,最后一种方法学了之后保证让你可以让你装逼。 问题描述:编号为 1-N 的 N 个士兵围坐在一起形成一个圆圈,从编号为 1 的士兵开始依次报数(1,2,3…这样依次报),数到 m 的 士兵会被杀死出列,之后的士兵再从 1 开始报数。直到最后剩下一士兵,求这个士兵的编号。 1、方...
吐血推荐珍藏的Visual Studio Code插件
作为一名Java工程师,由于工作需要,最近一个月一直在写NodeJS,这种经历可以说是一部辛酸史了。好在有神器Visual Studio Code陪伴,让我的这段经历没有更加困难。眼看这段经历要告一段落了,今天就来给大家分享一下我常用的一些VSC的插件。 VSC的插件安装方法很简单,只需要点击左侧最下方的插件栏选项,然后就可以搜索你想要的插件了。 下面我们进入正题 Material Theme ...
如何防止抄袭PCB电路板
目录 1、抄板是什么 2、抄板是否属于侵权 3、如何防止抄板 1、抄板是什么 抄板也叫克隆或仿制,是对设计出来的PCB板进行反向技术研究;目前全新的定义:从狭义上来说,抄板仅指对电子产品电路板PCB文件的提取还原和利用文件进行电路板克隆的过程;从广义上来说,抄板不仅包括对电路板文件提取、电路板克隆、电路板仿制等技术过程,而且包括对电路板文件进行修改(即改板)、对电子产品外形模具进行三维...
“狗屁不通文章生成器”登顶GitHub热榜,分分钟写出万字形式主义大作
一、垃圾文字生成器介绍 最近在浏览GitHub的时候,发现了这样一个骨骼清奇的雷人项目,而且热度还特别高。 项目中文名:狗屁不通文章生成器 项目英文名:BullshitGenerator 根据作者的介绍,他是偶尔需要一些中文文字用于GUI开发时测试文本渲染,因此开发了这个废话生成器。但由于生成的废话实在是太过富于哲理,所以最近已经被小伙伴们给玩坏了。 他的文风可能是这样的: 你发现,...
程序员:我终于知道post和get的区别
是一个老生常谈的话题,然而随着不断的学习,对于以前的认识有很多误区,所以还是需要不断地总结的,学而时习之,不亦说乎
《程序人生》系列-这个程序员只用了20行代码就拿了冠军
你知道的越多,你不知道的越多 点赞再看,养成习惯GitHub上已经开源https://github.com/JavaFamily,有一线大厂面试点脑图,欢迎Star和完善 前言 这一期不算《吊打面试官》系列的,所有没前言我直接开始。 絮叨 本来应该是没有这期的,看过我上期的小伙伴应该是知道的嘛,双十一比较忙嘛,要值班又要去帮忙拍摄年会的视频素材,还得搞个程序员一天的Vlog,还要写BU...
加快推动区块链技术和产业创新发展,2019可信区块链峰会在京召开
11月8日,由中国信息通信研究院、中国通信标准化协会、中国互联网协会、可信区块链推进计划联合主办,科技行者协办的2019可信区块链峰会将在北京悠唐皇冠假日酒店开幕。   区块链技术被认为是继蒸汽机、电力、互联网之后,下一代颠覆性的核心技术。如果说蒸汽机释放了人类的生产力,电力解决了人类基本的生活需求,互联网彻底改变了信息传递的方式,区块链作为构造信任的技术有重要的价值。   1...
Python 植物大战僵尸代码实现(2):植物卡片选择和种植
这篇文章要介绍的是: - 上方植物卡片栏的实现。 - 点击植物卡片,鼠标切换为植物图片。 - 鼠标移动时,判断当前在哪个方格中,并显示半透明的植物作为提示。
Java世界最常用的工具类库
Apache Commons Apache Commons有很多子项目 Google Guava 参考博客
程序员把地府后台管理系统做出来了,还有3.0版本!12月7号最新消息:已在开发中有github地址
第一幕:缘起 听说阎王爷要做个生死簿后台管理系统,我们派去了一个程序员…… 996程序员做的梦: 第一场:团队招募 为了应对地府管理危机,阎王打算找“人”开发一套地府后台管理系统,于是就在地府总经办群中发了项目需求。 话说还是中国电信的信号好,地府都是满格,哈哈!!! 经常会有外行朋友问:看某网站做的不错,功能也简单,你帮忙做一下? 而这次,面对这样的需求,这个程序员...
网易云6亿用户音乐推荐算法
网易云音乐是音乐爱好者的集聚地,云音乐推荐系统致力于通过 AI 算法的落地,实现用户千人千面的个性化推荐,为用户带来不一样的听歌体验。 本次分享重点介绍 AI 算法在音乐推荐中的应用实践,以及在算法落地过程中遇到的挑战和解决方案。 将从如下两个部分展开: AI算法在音乐推荐中的应用 音乐场景下的 AI 思考 从 2013 年 4 月正式上线至今,网易云音乐平台持续提供着:乐屏社区、UGC...
【技巧总结】位运算装逼指南
位算法的效率有多快我就不说,不信你可以去用 10 亿个数据模拟一下,今天给大家讲一讲位运算的一些经典例子。不过,最重要的不是看懂了这些例子就好,而是要在以后多去运用位运算这些技巧,当然,采用位运算,也是可以装逼的,不信,你往下看。我会从最简单的讲起,一道比一道难度递增,不过居然是讲技巧,那么也不会太难,相信你分分钟看懂。 判断奇偶数 判断一个数是基于还是偶数,相信很多人都做过,一般的做法的代码如下...
为什么要学数据结构?
一、前言 在可视化化程序设计的今天,借助于集成开发环境可以很快地生成程序,程序设计不再是计算机专业人员的专利。很多人认为,只要掌握几种开发工具就可以成为编程高手,其实,这是一种误解。要想成为一个专业的开发人员,至少需要以下三个条件: 1) 能够熟练地选择和设计各种数据结构和算法 2) 至少要能够熟练地掌握一门程序设计语言 3) 熟知所涉及的相关应用领域的知识 其中,后两个条件比较容易实现,而第一个...
相关热词 c# 二进制截断字符串 c#实现窗体设计器 c#检测是否为微信 c# plc s1200 c#里氏转换原则 c# 主界面 c# do loop c#存为组套 模板 c# 停掉协程 c# rgb 读取图片
立即提问