1081: 剔除相关数
来源:互联网 发布:iis7.5 php 500错误 编辑:程序博客网 时间:2024/04/25 19:09
题目
Description
一个数与另一个数如果含有相同数字和个数的字符,则称两数相关。现有一堆乱七八糟的整数,里面可能充满了彼此相关的数,请你用一下手段,自动地将其剔除。
Input
每组数据前有一个N(<1000),表示跟随的整数P(0
Output
按从小到大的顺序输出非相关数,若没有非相关数,则输出None
Sample Input
8
213 667 3 213 43 34 677 2
3
322 232 232
0
Sample Output
2 3 667 677
None
代码块
//根据题目可以知道,输出是从小到大的,所以要进行排序
import java.util.Scanner;public class Main{ public static void main(String[] args) { Scanner cn = new Scanner(System.in);//输入流 while (cn.hasNext()) {//多组输入 int n = cn.nextInt(); if (n == 0) return;//输入0 结束 int len = 0; int len1 = 0; int[] a = new int[1001]; int[] b = new int[1001]; int[] z = new int[1001]; for (int i = 0; i < n; i++) { a[i] = cn.nextInt(); } sort(a, n);//对输入的数进行排序 for (int i = 0; i < n - 1; i++) {//从其中找到所有的相关数,并存在一个数组中 for (int j = i + 1; j < n; j++) { if (compare(a[i], a[j])) { z[len] = a[j]; len++; break; } } } for (int i = 0; i < n; i++) {//用循环找到原来的数组中和相关数数组不相关的数,并输出 boolean flag = false; for (int j = 0; j < len; j++) { if (compare(z[j], a[i])) { flag = true; } } if (!flag) { b[len1] = a[i]; len1++; } } if (len1 == 0) { System.out.println("None"); } else { for (int i = 0; i < len1 - 1; i++) { System.out.print(b[i] + " "); } System.out.println(b[len1 - 1]); } } cn.close(); } private static boolean compare(int q, int w) { int[] x = new int[1001]; int[] y = new int[1001]; int i = 0, j = 0; while (q > 0) { x[i] = q % 10; q /= 10; i++; } while (w > 0) { y[j] = w % 10; w /= 10; j++; } sort(x, i); sort(y, j); if (i != j) { return false; } else { for (int k = 0; k < i; k++) { if (x[k] != y[k]) return false; } } return true; } private static void sort(int[] a, int n) { for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (a[i] > a[j]) { int t = a[i]; a[i] = a[j]; a[j] = t; } } } }}
0 0
- 1081: 剔除相关数
- 剔除相关数
- 1204: 剔除相关数
- zzuli oj 1204: 剔除相关数
- zzulioj 1204 剔除相关数(map和vector的应用)
- 剔除
- c++——将数组中重复的数剔除
- 将一组数从小到大排列并且剔除重复项
- C++每日一题「剔除关系数」
- [软件渲染器入门]六-应用纹理、背面剔除以及一些WebGL相关
- C++中利用CString的Format函数时,剔除浮点数后多余的零
- 华为机试2题:排序并剔除连续的数字只保留最大最小数
- 回调函数 相关
- IIS连接数相关
- 回调函数相关
- 浮点数相关
- catalan数相关
- oracle连接数相关
- 对象和调用方法
- HDU5945 Fxx and game
- 复习
- ViewPager网络请求图片并无限轮播
- 720全景制作
- 1081: 剔除相关数
- AngularJs书籍和视频资源
- python练习题——string模块
- Linux中的frame buffer
- 死锁例子
- 约瑟夫问题java实现
- git-ssh 配置和使用
- 《深入理解Java虚拟机——JVM高级特性与最佳实践》学习笔记——线程安全与锁优化
- poj2001(Trie树)