map去重輸出即可
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str;
getline(cin, str);
map<char, int> myMap;
char stemp;
for(int i=0; i<str.size(); i++){
stemp = tolower(str[i]);
myMap[stemp] += 1;
}
for(auto it=myMap.begin(); it!=myMap.end(); it++){
cout << it->first << ":" << it->second << endl;
}
return 0;
}
判斷兩條線段是否交點
思路:
1.下面給出的代碼不僅可以判斷是否相交,如果需要的話,還可以輸出交點line_x,line_y
2. double值由於精度原因不能用雙等號進行判斷是否相等,應該用
3. (兩個double差值<1e-8 && 兩個double差值>-1e-8) 1e-8代表10的-8次方
#include <bits/stdc++.h>
using namespace std;
struct Point{
double x, y;
};
bool between(double a, double X0, double X1)
{
double temp1= a-X0;
double temp2= a-X1;
if((temp1<1e-8&&temp2>-1e-8) || (temp2<1e-6&&temp1>-1e-8)){
return true;
}else{
return false;
}
}
// 判斷兩條直線段是否有交點,有則計算交點的座標
// p1,p2是直線一的端點座標
// p3,p4是直線二的端點座標
//fabs()fabs是取絕對值
//abs()也可取絕對值
//但abs是取絕對值後再取整,而fabs是取絕對值。
bool foo(Point p1, Point p2, Point p3, Point p4)
{
double line_x,line_y; //交點 如果需要的話 可以返回交點值
if ( (fabs(p1.x-p2.x)<1e-6) && (fabs(p3.x-p4.x)<1e-6) ){//如果兩線段平行 並且 垂直於y軸
return false;
}else if ( (fabs(p1.x-p2.x)<1e-6) ){ //如果直線段p1p2垂直與y軸
if (between(p1.x,p3.x,p4.x)){
double k = (p4.y-p3.y)/(p4.x-p3.x);
line_x = p1.x;
line_y = k*(line_x-p3.x)+p3.y;
if (between(line_y,p1.y,p2.y)){
return true;
}else{
return false;
}
}else{
return false;
}
}else if ( (fabs(p3.x-p4.x)<1e-6) ){ //如果直線段p3p4垂直與y軸
if (between(p3.x,p1.x,p2.x)){
double k = (p2.y-p1.y)/(p2.x-p1.x);
line_x = p3.x;
line_y = k*(line_x-p2.x)+p2.y;
if (between(line_y,p3.y,p4.y)){
return true;
}else{
return false;
}
}else{
return false;
}
}else{//如果線段均不垂直於X Y軸
double k1 = (p2.y-p1.y)/(p2.x-p1.x);
double k2 = (p4.y-p3.y)/(p4.x-p3.x);
if (fabs(k1-k2)<1e-6){//斜率相等 平行
return false;
}else{
line_x = ((p3.y - p1.y) - (k2*p3.x - k1*p1.x)) / (k1-k2);
line_y = k1*(line_x-p1.x)+p1.y;
}
if (between(line_x,p1.x,p2.x)&&between(line_x,p3.x,p4.x)){
return true;
}else{
return false;
}
}
}
int main()
{
Point lineOneStart, lineOneEnd, lineTwoStart, lineTwoEnd;
cin >> lineOneStart.x >> lineOneStart.y >> lineOneEnd.x >> lineOneEnd.y
>> lineTwoStart.x >> lineTwoStart.y >> lineTwoEnd.x >> lineTwoEnd.y;
if(foo(lineOneStart, lineOneEnd, lineTwoStart, lineTwoEnd))
cout << "true" << endl;
else
cout << "false" << endl;
return 0;
}
沒時間搞了。。。。