OJ入门

HDUOJ 1089

输入不说明有多少个Input Block,以EOF为结束标志。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

//1089
#include <cstdio>
#include <iostream>
using namespace std;
int a, b;
int main()
{
while (cin >> a >> b)
// while (~scanf("%d%d", &a, &b))
// while (scanf("%d%d", &a, &b) != EOF)
{
printf("%d\n", a + b);
}
return 0;
}

scanf函数返回值就是读出的变量个数,如:scanf( “%d %d”, &a, &b ); 如果只有一个整数输入,返回值是1,如果有两个整数输入,返回值是2,如果一个都没有,则返回值是-1。
EOF是一个预定义的常量,等于-1。

~取反,其实就是取相反数再-1。

HDUOJ 1090

输入一开始就会说有N个Input Block,下面接着是N个Input Block。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

//1090
#include <cstdio>
using namespace std;
int n, a, b;
int main()
{
scanf("%d", &n);
while (n--)
{
scanf("%d%d", &a, &b);
printf("%d\n", a + b);
}
return 0;
}

python 输入1

以空格为界一行输入多个数字+多组输入

利用异常处理机制实现
可以输入多行,当输入最后一行并回车后,按组合键ctrl+z,表示EOF,即End of File。此时,try语句里,input()函数会遇到EOF的异常。Python的异常处理机制将捕获到此异常,执行except语句,此语句为break,因此,立即跳出while循环。

try的工作原理是,当开始一个try语句后,python就在当前程序的上下文中作标记,这样当异常出现时就可以回到这里,try子句先执行,接下来会发生什么依赖于执行时是否出现异常。

1
2
3
4
5
6
7
8

while True:
try:
a, b = map(int, input().split())
print(a + b)
except:
break;

python 输入2

输入一开始就会说有N个Input Block,下面接着是N个Input Block。

因为input()默认为字符串,注意强制类型转换int()

1
2
3
4
5
6
7

n = int(input())
for i in range(n):
a = int(input())
b = int(input())
print(a + b)

C

scanf(“”),printf(“”);

1
2
3
4
5
scanf("");

printf("%5.2lf",m); //保留两位小数,两位整数。若整数小于五位则右对齐,左边补空格
printf("%05.2lf",m);//保留两位小数,两位整数。若整数小于五位则左边补零

scanf
函数返回值就是读出的变量个数,如:scanf( “%d %d”, &a, &b );
如果只有一个整数输入,返回值是1,如果有两个整数输入,返回值是2,如果一个都没有,则返回值是-1。

getchar(),putchar();

getchar()主要是从标准输入流读取一个字符。有返回值为读取的字符
getchar()可以吃回车和空格等空白字符

putchar(ch)主要是把字符ch写到标准流stdout中去

gets()(建议fgets())

gets();新标准C11已废弃
出于安全的考虑,可以将gets()替换成fgets()函数
fgets()函数会读到有换行符的位置或者规定的数组结束,但是不同于gets()的另一点就是,fgets()会将换行符也存入数组中,上面的代码后三行就是过滤了换行符。

1
2
3
4
5
char str[200];
fgets(str,200,stdin);
while(str[i]!='\n')
i++;
str[i]='\0'

cin,cout

1
2
cin >> a;
cout << a;

cin.getline()

cin.getline()函数是处理数组字符串的,其原型为cin.getline(char * , int),第一个参数为一个char指针,第二个参数为数组字符串长度。
与cin不同的特性,cin会以空格和回车来结束输入,而cin.getline(),会把空格也输入字符串

1
2
3
4
5
6
7
8
9
10
11
#include<iostream>
#include <stdio.h>
#include <string>
using namespace std;
int main()
{
char a[30];
cin.getline(a, 9);//第10位存放字符串结束符'\0'
cout << a;
return 0;
}

快读

花里胡哨

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ll read()
{
ll x = 0, f = 1;
char ch = getchar();//字符串
while (ch < '0' || ch > '9')
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}

易错1

  • 格式错误,有的题目要求最后不能有多余的空格(pta上好多题都要求这个)
1
2
for(int i  = 0; i < n; i++)
printf(i == n ? "%d\n" : "%d " );
  • scanf(" %c", &ch);
    对于scanf()而言,%c是个较为特殊的说明符。 %c前没空格,scanf()将读取标准输入流中的第一个字符,%c前有空格,scanf()则读取标准输入流中第一个非空白字符,屏蔽了空白字符。
    前面加一个getchar()吃掉回车也是可以的。
1
2
3
4
int i;
char ch;
scanf("%d", &i);
scanf(" %c", &ch);
  • 注意数据类型的范围,超出范围会溢出
1
2
3
4
5
6
7
8
9
10
#include<cstdio>
using namespace std;
int n = 1e10;
int b = 1e9;
int main()
{
printf("%d", n);
printf("%d", b);
return 0;
}

python

input

1
2
3

name = input('please enter your name: ')#括号里可以填提示输入信息

input的返回值默认是字符串,需要返回int型时需要使用强制类型转换,int(input())的形式

input().split

这个可以实现一行以空格为界输入多个数字

split()通过指定分隔符对字符串进行切片,并返回切片结果。
str.split(str=“”, num=string.count(str)).
参数
str – 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
num – 分割次数。默认为 -1, 即分隔所有。
返回值:返回分割后的字符串列表。
map接收一个函数和一个可迭代对象(如列表)作为参数,用函数处理每个元素,然后返回新的列表。

1
2
3
a, b, c = map(int, input().split())

a, b, c, d = map(int, input().split())

print

在Python中总是默认换行的,让 print 不换行print(x,end = ‘’ )

格式化输出有两种
一种是类似于C语言printf的方式

%

另一种是类似于C#的方式

format()
基本是前文格式控制,.format(,)中分别表示输出内容。数字(0, 1, …)即代表format()里面的元素

推荐用format
1
2
3
4
print('常量 PI 的值近似为:%5.3f' % math.pi)#字段宽5,精度3  

print('{}×{}={}'.format(i, j, i*j),end="\t")
print('{0} 和 {1}'.format('Google', 'Runoob'))#这里的0和1分别对应第一个和第二个

py易错

  • 格式错误

  • input的返回值默认为字符串

1
2
3
c = input()
b = input()
print(c + b)
  • 以下代码有一个缺陷就是不能单行输入多个数用空格分隔,
1
2
3
c = int(input())
b = int(input())
print(c + b)

有那种以数字中间空格为分界的题目还是要用split()

1
2
b, c = map(int, input().split())
print(c + b)