特殊的数:九度oj分析(集锦6篇)由网友“小斯”投稿提供,下面就是小编给大家整理后的特殊的数:九度oj分析,希望您能喜欢!
篇1:特殊的数:九度oj分析
前言
昨晚搞了个acm题,当时没考虑到内存限制,用了int数组,然后链表动态分配的方法,结果内存不够无法ac,今天考虑了一下,用数组唯一性的原理就可以实现了,难点在于用char数组存储数据,可以节约内存空间。
特殊的数
题目描述:
现在有n个数,其中有一些出现了一次,一些出现了两次,一些出现了很多次。现在要求你找出那些只出现一次的数,并按升序输出。
输入:
本题有多组case。
每个case有两行,第一行输入一个n,表示有n个数,1<= n <= 1000000。
第二行有n个数字。每个数字的大小范围[1, 1000000]。
输出:
每次输出有两行。
第一行输出一个整数,表示出现一次的数的个数。
第二行按升序输出出现次数为一次的数字,两个数字之间用空格隔开。
样例输入:
5
1 2 2 3 3
7
1 2 2 3 4 4 2
2
2 2
样例输出:
1
1
2
1 3
0
AC代码
key唯一性
[cpp]
#include
#include
#include
#define max 1000001
int main
{
int i, n, count, temp, j;
char a[max];
while(scanf(“%d”, &n) != EOF)
{
memset(a, 0, sizeof(a));
for(i = count = 0; i < n; i ++)
{
scanf(“%d”, &temp);
if(a[temp] == 0)
{
a[temp] += 1;
count ++;
}else if(a[temp] == 1)
{
a[temp] += 1;
count --;
}else
{
a[temp] += 1;
}
}
printf(“%d\\n”, count);
if(count)
{
for(i = j = 0; i < max; i ++)
{
if(a[i] == 1)
{
if(j == count - 1)
printf(“%d\\n”, i);
else
printf(“%d ”,i);
j ++;
}
}
}
}
return 0;
}
不考虑内存,可以用链表&&排序实现,感觉自己写的不错,也贴出来吧(这个没ac,因为内存超了限制,但是重要在学习方法)
[cpp]
#include
#include
#include
struct lnode
{
int data;
struct lnode *next;
};
int compare(const void *a, const void *b);
void createlist(struct lnode *, int);
void cleanlist(struct lnode *);
int main()
{
int num[1000001];
int i, n, j, k;
struct lnode *p;
while(scanf(“%d”, &n) != EOF)
{
//初始化数据
memset(num, 0, sizeof(num));
struct lnode *head = malloc(sizeof(struct lnode));
head->data = 0;
head->next = NULL;
//接收输入数据
for(i = 0; i < n; i ++)
{
scanf(“%d”, &num[i]);
}
//快速排序,调用系统qsort
qsort(num, n, sizeof(num[0]), compare);
for(i = j = 0; i < n; i ++)
{
if(num[i] != num[i + 1])
{
createlist(head, num[i]);
j ++;
}else
{
for(k = i; k < n; k ++)
{
if(num[i] != num[k])
{
break;
}
}
i = k - 1;
}
}
//打印输出
printf(“%d\\n”, j);
for(i = 0, p = head->next; p && i < j; p = p->next, i ++)
{
if(i == j - 1)
printf(“%d\\n”, p->data);
else
printf(“%d ”, p->data);
}
//清理链表
cleanlist(head);
}
return 0;
}
int compare(const void *a, const void *b)
{
int sign;
sign = (*(int *)a - *(int *)b) * -1;
return sign;
}
void createlist(struct lnode *head, int data)
{
struct lnode *s = malloc(sizeof(struct lnode));
s->data = data;
s->next = head->next;
head->next = s;
}
void cleanlist(struct lnode *head)
{
struct lnode *p;
for(p = head; p; p = head)
{
head = head->next;
free(p);
}
}
篇2:九度OJ―题目1010:A + B
上一篇:www.2cto.com/kf/12/363595.html
题目描述:读入两个小于100的正整数A和B,计算A+B.
需要注意的是:A和B的每一位数字由对应的英文单词给出.输入:测试输入包含若干测试用例,每个测试用例占一行,格式为“A + B =”,相邻两字符串有一个空格间隔.当A和B同时为0时输入结束,相应的结果不要输出.输出:对每个测试用例输出1行,即A+B的值.样例输入:
one + two =three four + five six =zero seven + eight nine =zero + zero =
样例输出:
39096
来源:
浙江大学计算机及软件工程研究生机试真题答疑:篇3:九度OJ―题目1001:A+B for Matrices
上一篇www.2cto.com/kf/201412/363598.html
题目描述:
This time, you are supposed to find A+B where A and B are two matrices, and then count the number of zero rows and columns.
输入:The input consists of several test cases, each starts with a pair of positive integers M and N (≤10) which are the number of rows and columns of the matrices, respectively. Then 2*M lines follow, each contains N integers in [-100, 100], separated by a space. The first M lines correspond to the elements of A and the second M lines to that of B.
The input is terminated by a zero M and that case must NOT be processed.
输出:For each test case you should output in one line the total number of zero rows and columns of A+B.
样例输入:2 21 11 1-1 -110 92 31 2 34 5 6-1 -2 -3-4 -5 -60样例输出:
15来源:浙江大学计算机及软件工程研究生机试真题答疑:
篇4:九度OJ 1041 Simple Sorting (排序,STL)
题目1041:Simple Sorting
时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:3971
解决:1483
题目描述:You are given an unsorted array of integer numbers. Your task is to sort this array and kill possible duplicated elements occurring in it.
输入:For each case, the first line of the input contains an integer number N representing the quantity of numbers in this array(1≤N≤1000). Next N lines contain N integer numbers(one number per each line) of the original array.
输出:For each case ,outtput file should contain at most N numbers sorted in ascending order. Every number in the output file should occur only once.
样例输入:68 8 7 3 7 7样例输出:
3 7 8
纯C程序:
#include
>t;“ class=”brush:java;“ coutltlt=”cout<<“ coutltlta.begin=”cout<<*a.begin;“ coutltltendl=”cout< 高人版(0内存,0ms): #include 题目描述: 实现一个加法器,使其能够输出a+b的值, 输入包括两个数a和b,其中a和b的位数不超过1000位。 可能有多组测试数据,对于每组数据, 输出a+b的值。 2 610000000000000000000 10000000000000000000000000000000样例输出: 810000000000010000000000000000000来源:华中科技大学计算机研究生机试真题答疑:解题遇到问题?分享解题心得?讨论本题请访问:t.jobdu.com/thread-7921-1-1.html #include 题目描述:给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j <= K,最大连续子序列是所有连续子序列中元素和最大的一个,例如给定序列{ -2, 11, -4, 13, -5, -2 },其最大连续子序列为{ 11, -4, 13 },最大和为20。现在增加一个要求,即还需要输出该子序列的第一个和最后一个元素。输入: 测试输入包含若干测试用例,每个测试用例占2行,第1行给出正整数K( K< 10000 ),第2行给出K个整数,中间用空格分隔。当K为0时,输入结束,该用例不被处理。 对每个测试用例,在1行里输出最大和、最大连续子序列的第一个和最后一个元素,中间用空格分隔, 如果最大连续子序列不唯一,则输出序号i和j最小的那个(如输入样例的第2、3组)。若所有K个元素都是负数,则定义其最大和为0,输出整个序列的首尾元素。 6-2 11 -4 13 -5 -210-10 1 2 3 4 -5 -23 3 7 -2165 -8 3 2 5 01103-1 -5 -23-1 0 -20样例输出: 20 11 1310 1 410 3 510 10 100 -1 -20 0 0来源:2005年浙江大学计算机及软件工程研究生机试真题答疑:篇5:九度OJ―题目1198:a+b(高精度计算)
篇6:九度OJ―题目1011:最大连续子序列