新手刷题

人来人往dd 2017-01-14 10:32:16
题目描述:
第一行输入一个数,为n,第二行输入n个数,这n个数中,如果偶数比奇数多,输出NO,否则输出YES。
输入:
输入有多组数据。
每组输入n,然后输入n个整数(1<=n<=1000)。
输出:
如果偶数比奇数多,输出NO,否则输出YES。
样例输入:
5
1 5 2 4 3
样例输出:
YES


import java.util.Scanner;

public class solution {
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
while(s.hasNext()){
int countodd=0;
int counteven=0;
int n=s.nextInt();
s.nextLine();
System.out.println(n);
for( int i = 0; i < n;i ++ )
{
int m=s.nextInt();
System.out.println(m);
if(m%2!=0)
countodd++;
else
counteven++;
}
if(countodd<counteven)
System.out.println("NO\n");
else
System.out.println("YES\n");
}
}
}

一直报错。。。。
...全文
204 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
liny_ 2017-01-15
  • 打赏
  • 举报
回复
那你应该是在做网站的练习,循环输入的话,你的其实就多了几行 这样: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); while(s.hasNext()){ int countodd=0; int counteven=0; int n=s.nextInt(); for( int i = 0; i < n;i ++ ) { int m=s.nextInt(); if(m%2!=0) countodd++; else counteven++; } if(countodd<counteven) System.out.println("NO"); else System.out.println("YES"); } } }
人来人往dd 2017-01-15
  • 打赏
  • 举报
回复
引用 1 楼 liny_ 的回复:
你这个肯定不对,这样写 import java.util.Scanner; public class solution { public static void main(String[] args) { Scanner s = new Scanner(System.in); int countodd=0;//偶数个数 int counteven=0;//奇数个数 int n=s.nextInt();//总数 while(s.hasNext()){//计数 int m=s.nextInt(); if(m%2!=0){ countodd++; }else{ counteven++; } if((countodd + counteven) == n) {//输入个数达到总数退出 break; } } if(countodd<counteven) {//比较 System.out.println("NO\n"); }else{ System.out.println("YES\n"); } } }
提交也不对 这个题目要求不是要多组数据吗?
liny_ 2017-01-15
  • 打赏
  • 举报
回复
你这个肯定不对,这样写 import java.util.Scanner; public class solution { public static void main(String[] args) { Scanner s = new Scanner(System.in); int countodd=0;//偶数个数 int counteven=0;//奇数个数 int n=s.nextInt();//总数 while(s.hasNext()){//计数 int m=s.nextInt(); if(m%2!=0){ countodd++; }else{ counteven++; } if((countodd + counteven) == n) {//输入个数达到总数退出 break; } } if(countodd<counteven) {//比较 System.out.println("NO\n"); }else{ System.out.println("YES\n"); } } }

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧