3.max points ona line(最多有多少个点在同一直线上)

3.max points ona line(最多有多少个点在同一直线上)

链接:http://www.lintcode.com/zh-cn/problem/max-points-on-a-line/

给出二维平面上的n个点,求最多有多少点在同一条直线上。

 

分析:中等题

任意一条直线都可以表述为

y = ax + b

假设,有两个点(x1,y1), (x2,y2),如果他们都在这条直线上则有

y1 = kx1 +b

y2 = kx2 +b

由此可以得到关系,k =(y2-y1)/(x2-x1)。即如果点c和点a的斜率为k,而点b和点a的斜率也为k,可以知道点c和点b也在一条线上。

取定一个点points[i],遍历其他所有节点, 然后统计斜率相同的点数,并求取最大值即可

 

有一些细节需要注意:

0points中重复出现的点。

1int maxNum = 0;

初始化,以防points.size() ==0的情况。

2mp[INT_MIN] = 0;

保证poins中只有一个结点,还有points中只有重复元素时,mp中没有元素。这两种极端情况。

3int duplicate = 1;

duplicate记录重复点的数量,初始化为1,是因为要把当前的点points[i]加进去。

4float k = points[i].x == points[j].x ?INT_MAX : (float)(points[j].y - points[i].y)/(points[j].x - points[i].x);

计算斜率,如果直线和y轴平行,就取INT_MAX,否则就取(float)(points[j].y - points[i].y)/(points[j].x - points[i].x)

5.(float)(points[j].y - points[i].y)/(points[j].x - points[i].x)写做(float)((points[j].y - points[i].y)/(points[j].x - points[i].x)),一定要先强制类型转换,再做除法,如果除完再强制类型转换就没有意义了(因为还是做了整数相除)~~

AC代码如下

/**
 * Definition for a point.
 * struct Point {
 *    int x;
 *    int y;
 *    Point() : x(0), y(0) {}
 *    Point(int a, int b) : x(a), y(b) {}
 * };
 */
class Solution {
public:
    /**
     * @param points an array of point
     * @return an integer
     */
    int maxPoints(vector<Point>&points) {
        // Write your code here
        map<float,int> hash;
        int len=points.size();
        if(len==1)
            return 1;
        int maxnum=0;
        for(int i=0;i<len;++i)
        {
            hash.clear();
            hash[INT_MIN]=0;
            int dup=1;
            for(int j=0;j<len;++j)
            {
                if(i==j)
                    continue;
               if(points[i].x==points[j].x&&points[i].y==points[j].y)
                {
                    ++dup;
                    continue;
                }
                floatk=points[i].x==points[j].x?INT_MAX:(float)(points[j].y-points[i].y)/(points[j].x-points[i].x);
                hash[k]++;
            }
            for(autoit=hash.begin();it!=hash.end();++it)
            {
                if(it->second+dup>maxnum)
                    maxnum=it->second+dup;
            }
        }
        return maxnum;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,我不能在这里直接生成文件。但是,我可以提供一个Python代码示例,您可以使用它来生成所需的CSV文件。 ```python import csv import random # List of possible values for each field names = ['Naruto', 'One Piece', 'Dragon Ball', 'Attack on Titan', 'Death Note', 'Fullmetal Alchemist', 'Sailor Moon', 'Bleach', 'Hunter x Hunter', 'My Hero Academia'] genres = ['Action', 'Adventure', 'Comedy', 'Drama', 'Fantasy', 'Horror', 'Mystery', 'Romance', 'Sci-Fi', 'Thriller'] types = ['TV', 'OVA', 'Movie', 'ONA'] episodes = [12, 24, 26, 50, 100] ratings = [8.0, 8.1, 8.2, 8.3, 8.4, 8.5, 8.6, 8.7, 8.8, 8.9, 9.0] members = [100000, 200000, 300000, 400000, 500000, 600000, 700000, 800000, 900000, 1000000] descriptions = ['A young ninja strives to become the strongest in his village.', 'Pirates search for the ultimate treasure.', 'A boy with incredible powers fights to protect the world.', 'Humanity battles against giant man-eating Titans.', 'A student gains the power to kill anyone whose name he writes in a notebook.', 'Two brothers seek a way to restore their bodies after a failed alchemical experiment.', 'A group of teenage girls transform into magical warriors to defend the Earth.', 'A teenager gains the ability to see ghosts and becomes a Soul Reaper.', 'A boy follows in his father\'s footsteps and becomes a Hunter.', 'A boy without powers dreams of becoming a hero in a world where everyone has them.'] pictures = ['https://example.com/naruto.jpg', 'https://example.com/onepiece.jpg', 'https://example.com/dragonball.jpg', 'https://example.com/aot.jpg', 'https://example.com/deathnote.jpg', 'https://example.com/fma.jpg', 'https://example.com/sailormoon.jpg', 'https://example.com/bleach.jpg', 'https://example.com/hxh.jpg', 'https://example.com/mha.jpg'] # Generate 1000 rows of data rows = [] for i in range(1000): row = [random.choice(names), random.choice(genres), random.choice(types), random.choice(episodes), random.choice(ratings), random.choice(members), random.choice(descriptions), random.choice(pictures)] rows.append(row) # Write data to CSV file with open('anime_data.csv', 'w', newline='') as f: writer = csv.writer(f) writer.writerow(['name', 'genre', 'type', 'episodes', 'rating', 'members', 'description', 'picture']) writer.writerows(rows) ``` 这段代码将生成一个包含1000行的CSV文件,其中每行都包含“name”,“genre”,“type”,“episodes”,“rating”,“members”,“description”和“picture”字段的值。您可以根据需要修改列表中的值或添加/删除字段。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值