哈希算法---Equations

来源:互联网 发布:mmd 动作数据 r-18 编辑:程序博客网 时间:2024/06/04 01:37

All the problems in this contest totally bored you. And every time you get bored you like playing with quadratic equations of the form a*X 2 + b*X + c = 0. This time you are very curious to know how many real solutions an equation of this type has. 
Input
The first line of input contains an integer number Q, representing the number of equations to follow. Each of the next Q lines contains 3 integer numbers, separated by blanks, a, b and c, defining an equation. The numbers are from the interval 1000,1000−1000,1000
Output
For each of the Q equations, in the order given in the input, print one line containing the number of real solutions of that equation. Print “INF” (without quotes) if the equation has an infinite number of real solutions. 
Sample Input
31 0 01 0 -10 0 0
Sample Output
12INF


Time limit
1000 ms
Memory limit
65535 kB
OS
WindowsStatusAcceptedMemory1600kBLength621LangG++Submitted2017-04-10 21:20:58
#include<iostream>using namespace std;int main(){std::ios::sync_with_stdio(false);int Q;int a,b,c;while(cin>>Q){for(int i=0;i<Q;i++){cin>>a>>b>>c;if(a!=0&&b==0&&c==0)  //x^2=0cout<<1<<endl;else if(a==0&&b!=0&&c!=0)//x+c=0;cout<<1<<endl;else if(a==0&&b==0&&c!=0)//c=0cout<<0<<endl;else if(a==0&&b==0&&c==0)//0=0cout<<"INF"<<endl;else if(a==0&&(b==1||b==-1)&&c==0)//x=0cout<<"INF"<<endl;else if((b*b-4*a*c)>0)cout<<2<<endl;else if((b*b-4*a*c)==0)cout<<1<<endl;else if((b*b-4*a*c)<0)cout<<0<<endl;}} return 0;}

sscanf与scanf类似,都是用于输入的,只是后者以键盘(stdin)为输入源,前者以固定字符串为输入源。


失败返回0 ,否则返回格式化的参数个数
sscanf("1 2 3","%d %d %d",buf1, buf2, buf3); 成功调用返回值为3,即buf1,buf2,buf3均成功转换。
sscanf("1 2","%d %d %d",buf1, buf2, buf3); 成功调用返回值为2,即只有buf1,buf2成功转换。
(注意:此处buf均为地址)


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);
结果为:12DDWDFF
7、给定一个字符串"hello, world",仅保留"world"。(注意:“,”之后有一空格)
sscanf(“hello, world”, "%*s%s", buf);
printf("%s\n", buf);
结果为:world
P.S. %*s表示第一个匹配到的%s被过滤掉,即hello被过滤了,
如果没有空格则结果为NULL。[2] 


%[a-z] 表示匹配a到z中任意字符,贪婪性(尽可能多的匹配)
%[aB'] 匹配a、B、'中一员,贪婪性
%[^a] 匹配非a的任意字符,并且停止读入,贪婪性


可以用如下代码将字符串形式的ip地址转换为四个整数:


char * inputIp  
int ip[4];  
sscanf_s(inputIp, "%d.%d.%d.%d", &ip[0], &ip[1],&ip[2],&ip[3]);  





































0 0
原创粉丝点击