输入输出练习
1、hdu 1000
原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=1000
解题代码:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int a , b;
Scanner cin = new Scanner(System.in);
while(cin.hasNextInt())
{
a = cin.nextInt();
b = cin.nextInt();
System.out.println(a+b);
}
}
}
2、hdu 1001
原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=1001
解题代码:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int n , sum;
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
n = cin.nextInt();
if(n % 2 == 0)
sum = n / 2 * (1 + n);
else
sum = (1 + n) / 2 * n;
System.out.println(sum);
System.out.println();
}
}
}
这道题看起来简单,其实暗藏玄机。
使用循环求解当然不会有什么问题。
问题就出在有人会用等差数列求和公式来进行求解。
使用公式直接求解:sum = (1 + n) * n / 2肯定是错误的。
因为题目要求结果是32bit,但是n*(n+1)就不能够保证是在32bit之内的,所以要先进行除以2运算。
3、hdu 1089
原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=1089
解题代码:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int a , b;
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
a = cin.nextInt();
b = cin.nextInt();
System.out.println(a+b);
}
}
}
4、hdu 1090
原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=1090
解题代码:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int n , a , b;
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
n = cin.nextInt();
for(int i = 0; i < n; i++)
{
a = cin.nextInt();
b = cin.nextInt();
System.out.println(a+b);
}
}
}
}
5、hdu 1091
原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=1091
解题代码:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int a , b;
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
a = cin.nextInt();
b = cin.nextInt();
if(a==0 && b==0) break;
System.out.println(a+b);
}
}
}
6、hdu 1092
原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=1092
解题代码:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int num, sum, n;
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
n = cin.nextInt();
if(n == 0) break;
sum = 0;
for(int i = 0; i < n; i++)
{
num = cin.nextInt();
sum += num;
}
System.out.println(sum);
}
}
}
7、hdu 1093
原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=1093
解题代码:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int num, sum, n, m;
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
m = cin.nextInt();
for(int i = 0; i < m; i++)
{
n = cin.nextInt();
sum = 0;
for(int j = 0; j < n; j++)
{
num = cin.nextInt();
sum += num;
}
System.out.println(sum);
}
}
}
}
8、hdu 1094
原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=1094
解题代码:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int num, sum, n;
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
n = cin.nextInt();
sum = 0;
for(int i = 0; i < n; i++)
{
num = cin.nextInt();
sum += num;
}
System.out.println(sum);
}
}
}
9、hdu 1095
原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=1095
解题代码:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int a , b;
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
a = cin.nextInt();
b = cin.nextInt();
System.out.println(a+b);
System.out.println();
}
}
}
10、hdu 1096
原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=1096
解题代码:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int num , sum , n , m;
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
m = cin.nextInt();
for(int i = 0; i < m; i++)
{
n = cin.nextInt();
sum = 0;
for(int j = 0; j < n; j++)
{
num = cin.nextInt();
sum += num;
}
System.out.println(sum);
if(i < m-1) System.out.println();
}
}
}
}