sscanf函数的用法

B - Lining Up
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Download as PDF


 Lining Up 

``How am I ever going to solve this problem?" said the pilot.

Indeed, the pilot was not facing an easy task. She had to drop packages at specific points scattered in a dangerous area. Furthermore, the pilot could only fly over the area once in a straight line, and she had to fly over as many points as possible. All points were given by means of integer coordinates in a two-dimensional space. The pilot wanted to know the largest number of points from the given set that all lie on one line. Can you write a program that calculates this number?

Your program has to be efficient!

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
The input consists of N pairs of integers, where 1 < N < 700. Each pair of integers is separated by one blank and ended by a new-line character. The list of pairs is ended with an end-of-file character. No pair will occur twice.

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line. 
The output consists of one integer representing the largest number of points that all lie on one line.

Sample Input

1

1 1
2 2
3 3
9 10
10 11

Sample Output

3

 
题意:
给出很多点的坐标,算出在一条直线的点最多的个数。
不过输入的时候,注意要用字符串的输入来判断输入是否停止。
代码:
     
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include <stack>
#include <string>
#include <set>
#include <map>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define MAXX 10000
 
struct point
{
int x, y;
bool operator < (const point &a) const
{
if (a.x == x)
return y<a.y;
return x<a.x;
}
}p[MAXX];
using namespace std;
double k[MAXX];
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // LOCAL
int T;
scanf("%d", &T);
cin.get();
cin.get();
while (T--)
{
int n=0, x, y;
char str[100];
while(gets(str))
{
if (str[0]=='\0') break;
sscanf(str,"%d%d",&p[n].x,&p[n].y);
n++;
}
int ans=0;
sort(p, p+n);
for (int i=0; i<n-1; i++)
{
for (int j=i+1; j<n; j++)
{
int maxx = 0;
int A=p[i].y-p[j].y;
int B=p[i].x-p[j].x;
int C=-p[i].x*A+p[i].y*B;
for (int k=0; k<n; k++)
if ((-B*p[k].y+A*p[k].x+C) == 0)
maxx++;
ans = max(ans, maxx);
}
}
cout<<ans<<endl;
if(T)
cout<<endl;
}
return 0;
}

方法:
先算出一条直线的一般式的A、B、C的值,然后在一个一个的点去判断是否在直线上
相关知识:
sscanf函数用法,是从字符串里读出与指定格式相符的数据。
1. 常见用法。 
  char buf[512] = ; 
  sscanf("123456 ", "%s", buf); 
  printf("%s\n", buf); 
  结果为:123456 
  2. 取指定长度的字符串。如在下例中,取最大长度为4字节的字符串。 
  sscanf("123456 ", "%4s", buf); 
  printf("%s\n", buf); 
  结果为:1234 
  3. 取到指定字符为止的字符串。如在下例中,取遇到空格为止字符串。 
  sscanf("123456 abcdedf", "%[^ ]", buf); 
  printf("%s\n", buf); 

结果为:123456 
  4. 取仅包含指定字符集的字符串。如在下例中,取仅包含1到9和小写字母的字符串。 
  sscanf("123456abcdedfBCDEF", "%[1-9a-z]", buf); 
  printf("%s\n", buf); 
  结果为:123456abcdedf 
  5. 取到指定字符集为止的字符串。如在下例中,取遇到大写字母为止的字符串。 
  sscanf("123456abcdedfBCDEF", "%[^A-Z]", buf); 
  printf("%s\n", buf); 
  结果为:123456abcdedf 
  6、给定一个字符串iios/12DDWDFF@122,获取 / 和 @ 之间的字符串,先将 "iios/"过滤掉,再将非'@'的一串内容送到buf中 
  sscanf("iios/12DDWDFF@122", "%*[^/]/%[^@]", buf); 
  printf("%s\n", buf); 
 7、给定一个字符串““hello, world”,仅保留world。(注意:“,”之后有一空格) 
  sscanf(“hello, world”, "%*s%s", buf); 
  printf("%s\n", buf); 
  结果为:world 
  %*s表示第一个匹配到的%s被过滤掉,即hello被过滤了 
  如果没有空格则结果为NULL。 
  sscanf的功能很类似于正则表达式, 但却没有正则表达式强大,所以如果对于比较复杂的字符串处理,建议使用正则表达式. 
  //------------------------------------------------------- 
  sscanf,表示从字符串中格式化输入 
  上面表示从str中,输入数字给x,就是32700 
  久以前,我以为c没有自己的split string函数,后来我发现了sscanf;一直以来,我以为sscanf只能以空格来界定字符串,现在我发现我错了。 
  sscanf是一个运行时函数,原形很简单: 
int sscanf( 
  const char *buffer, 
  const char *format [, 
  argument ] ... 
  ); 
  它强大的功能体现在对format的支持上。 
  我以前用它来分隔类似这样的字符串2006:03:18: 
  int a, b, c; 
  sscanf("2006:03:18", "%d:%d:%d", a, b, c); 
  以及2006:03:18 - 2006:04:18: 
  char sztime1[16] = "", sztime2[16] = ""; 
  sscanf("2006:03:18 - 2006:04:18", "%s - %s", sztime1, sztime2); 
  但是后来,我需要处理2006:03:18-2006:04:18 
  仅仅是取消了‘-’两边的空格,却打破了%s对字符串的界定。 


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值