Algorithm practice -3
1、四个数的和为0 (C语言)
给出N个整数,你来判断一下是否能够选出4个数,他们的和为0,可以则输出"Yes",否则输出"No"。
Input : 第1行,1个数N,N为数组的长度(4 <= N <= 1000) 第2 - N + 1行:Ai(-109 <= Ai <= 109)
Output : 如果可以选出4个数,使得他们的和为0,则输出"Yes",否则输出"No"。
Sample Input :
5
-1
1
-5
2
4
Sample Output : Yes
#include <stdio.h>
int main(){
int partition (int [], int, int);
void quick_sort(int [], int, int);
int n, i, j, low, high, tmp;
scanf("%d", &n);
int arr[1000];
for( i = 0; i < n ; i ++){
scanf("%d", &arr[i]);
}
quick_sort(arr,0,n-1);
for(i = 0;i < n-3; i ++){
for(j = i + 1; j < n-2 ; j ++){
tmp = -(arr[i] + arr[j]);
low = j+1;
high = n-1;
while(low < high){
if((arr[low] + arr[high]) == tmp){
printf("Yes\n");
return 0;
}else if(arr[low] + arr[high] > tmp){
high --;
}else{
low ++;
}
}
}
}
printf("No\n");
return 0;
}
2、第m大的身份证号码 (C语言)
身份证号是我国公民的唯一识别码,它由 18 位数字或者字母组成(只可能最后一位是字母)。18 位身份证号码的含义如下:第 1~2 为省、自治区、直辖市代码:第 3~4 位为地级市、盟、自治州代码;第 5~6 位为县、县级市、区代码。第 7~14 位位出生年月日,比如 19970401 代表 1997 年 4 月 1 日;第 15~16 位为顺序号,第 17 位代表性别,男为单数,女为双数,第 18 位为校验码,0~9 和 X。作为尾号的校验码,是把前十七位数字代入统一的公式计算出来的。解答本题你不用关心是如何计算出来的。现在给你 n 个身份证号码,请你按照出生年月日的字典序(年龄从大到小)输出第 m 个人的身份证号。
一些解释:虽然造数据的人非常辛苦的制造各种各样的身份证号(并且让他们生日互不相同),但是你并不需要验证关于身份证号的任何合法性,包括省市区是否合法,出生年月日是否合法,校验值是否合法,你需要做的仅仅是输出年龄从大到小第m个人的身份证号。
Input : 输入第一行包含两个正整数 n 和 m,两数之间用一个空格分隔,接下来的 n 行每行为一个形如上述格式的身份证号码(不需要关心校验码的正确性,不影响本题解答)。(1≤n≤100,1≤m≤n)
Output : 输出仅包含一行,为题目要求的一个身份证号码。
Sample Input :
4 2
110108196004063022
13021119640203652X
420333197902112718
210222200012036512
Sample Output : 13021119640203652X
第m大的年龄,即第m小的出生年月。首先把出生年月从身份证号中提取出来,在进行排序。基于快速排序的思想,边排序边比较是否是第m小的出生年月日。为方便将出生年月和身份证号练习起来,使用结构体会方便一些。
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
struct node{
char id[18];
int birth;
};
int main(){
struct node quick_sort (struct node[], int, int,int);
int n,m,i;
scanf("%d %d", &n, &m);
struct node *arr = (struct node*) malloc( n * sizeof(struct node));
for(i = 0;i < n;i ++){
scanf("%s", &arr[i].id);
char tmp[8] = {0};
strncpy(tmp, arr[i].id + 6, 8);
arr[i].birth = atoi(tmp);
}
struct node res = quick_sort(arr, 0, n-1, m-1);
printf("%s\n", res.id);
return 0;
}
/**
*快速排序,确定枢纽的位置,左边值都比枢纽值小,右边都比枢纽大
*先判断枢纽值的位置是否为要查的位置,否则分别再向左、右排序
*@param arr_birth 要排序的数组
*@param low 要排序的数组区间左端点
*@param high 要排序的数组区间右端点
*@param n 第m位置
*@return 返回枢纽值在原数组的坐标位置
*/
struct node quick_sort (struct node arr[], int low, int high,int m){
int low_tmp = low;
int high_tmp = high;
struct node pivot = arr[low];
while( low < high ){
while(low < high && arr[high].birth >= pivot.birth){
high --;
}
arr[low] = arr[high];
while(low < high && arr[low].birth <= pivot.birth){
low ++;
}
arr[high] = arr[low];
}
arr[low] = pivot;
if(low == m ){
return arr[low];
}else if ( low < m){
return quick_sort(arr, low+1, high_tmp,m);
}else{
return quick_sort(arr, low_tmp, low-1,m);
}
}
3、排队接水 ( C )
n个人一起排队接水,第i个人需要b的时间来接水。(1<=n<=1000,0<=b<=1000),同时只能有一个人接水,正在接水的人和没有接水的人都需要等待。完成接水的人会立刻消失,不会继续等待。你可以决定所有人接水的顺序,并希望最小化所有人等待时间的总和。
Input :第一行一个整数n。接下来n行,每行一个整数表示bi
Output : 一行一个整数,表示所有人等待时间的总和的最小值
Sample Input :
3
1
2
3
Sample Output : 10
题目不难,无非就是求和。需要注意的是,第n个人的等待时间是这n个人所有时间的总和,而求这n个人的时间总和,需要将n个人的排队时间都再加一遍。
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
int main(){
int partition (int [], int, int);
void quick_sort(int [], int, int);
int n,i;
scanf("%d", &n);
int *arr = (int *) malloc( n * sizeof(int));
int *arr_time = (int *) malloc( n * sizeof(int));
for( i = 0; i < n ; i ++){
scanf("%d", &arr[i]);
}
quick_sort(arr, 0, n-1);
arr_time[0] = arr[0];
for (i = 1;i < n;i ++){
arr[i] = arr[i-1] + arr[i];
arr_time[i] = arr_time[i-1] + arr[i];
}
printf("%d\n",arr_time[n-1]);
return 0;
}
4、亲和数 ( C )
古希腊数学家毕达哥拉斯在自然数研究中发现,220的所有真约数(即不是自身的约数)之和为:1+2+4+5+10+11+20+22+44+55+110=284。而284的所有真约数为1、2、4、71、 142,加起来恰好为220。人们对这样的数感到很惊奇,并称之为亲和数。一般地讲,如果两个数中任何一个数都是另一个数的真约数之和,则这两个数就是亲和数。
你的任务就编写一个程序,判断给定的两个数是否是亲和数
Input : 输入数据第一行包含一个数M,接下有M行,每行一个实例,包含两个整数A,B; 其中 0 <= A,B <= 600000 ;
Output : 对于每个测试实例,如果A和B是亲和数的话输出YES,否则输出NO。
Sample Input :
2
220 284
100 200
Sample Output :
YES
NO
本题简单,就是求某个数的真约数,再求和判断即可
#include <stdio.h>
int main(){
int n, a, b, sum, tmp, i, j;
scanf("%d", &n);
for( i = 0; i < n ; i ++){
scanf("%d %d", &a, &b);
sum = 1, tmp = a;
for(j = 2; j < tmp;j ++){
if( a % j == 0){
sum = sum + j + a / j;
tmp = a / j;
}
}
if( sum != b){
printf("NO\n");
continue;
}
sum = 1, tmp = b;
for(j = 2; j < tmp;j ++){
if( b % j == 0){
sum = sum + j + b / j;
tmp = b / j;
}
}
if( sum == a){
printf("YES\n");
}
}
return 0;
}
5、第几天 ( C )
给定一个日期,输出这个日期是该年的第几天。
Input : 输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。
Output : 对于每组输入数据,输出一行,表示该日期是该年的第几天。
Sample Input :
1985/1/20
2006/3/12
Sample Output :
20
71
本题简单,易错点有两个,一是需要判断闰年,因为闰年的2月有29天,二是每次都需要判断一下,重置一下2月份的天数
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
/**
*闰年:能被4整除但不能被100整除 或 能被400整除
*/
int main(){
int mon_day[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char date[10] = {0};
int arr_date[3] = {0};
char *tmp = NULL;
int i, year, mon, day,sum_day;
while(scanf("%s",&date) != EOF){
i = 0, sum_day = 0;
tmp = strtok( date, "/" );
while( tmp != NULL ) {
arr_date[i] = atoi(tmp);
tmp = strtok( NULL, "/" );
i ++;
}
year = arr_date[0];
mon = arr_date[1];
day = arr_date[2];
if ((year %4 == 0 && year % 100 != 0) || ( year % 400 == 0)){
mon_day[2] = 29;
}else{
mon_day[2] = 28;
}
for (i = 1;i < mon; i ++){
sum_day += mon_day[i];
}
sum_day += day;
printf("%d\n",sum_day);
}
return 0;
}
6、按1的个数排序 (C)
有一些 01 字串,将其按 1 的个数的多少的顺序进行输出。如果 1的数量相等,则按照出现的先后顺序排序。
Input : 输入数据有若干行组成。第一行是一个数 n(1≤n≤100),代表串的个数。然后 n 行每一行是一个 01 串,每个字符串长度不超过 200 。
Output : 重新排列 01 串的顺序。使得串按题目描述的方式排序
Sample Input :
6
10011111
00001101
1010101
1
0
1100
Sample Output :
0
1
1100
00001101
1010101
10011111
本题简单,利用结构体将字符串和数字1的个数联系起来,排序的话不能使用快排,因为不稳定,由于数据量不大,本次用的是折半插入排序。
#include <stdio.h>
#include <malloc.h>
#include <string.h>
struct STR{
int index;
char str[200];
int count;
};
int main(){
void cal_count( struct STR [] ,int);
void insert_sort(struct STR [] ,int);
int n, i;
scanf("%d", &n);
struct STR *arr = (struct STR*) malloc( n * sizeof(struct STR));
for( i = 0; i < n ; i ++){
scanf("%s", &arr[i].str);
arr[i].index = i;
arr[i].count = 0;
}
cal_count(arr ,n);
insert_sort(arr,n);
for( i = 0; i < n ; i ++){
printf("%s\n",arr[i].str);
}
return 0;
}
/**
*统计数组中数字1的个数
*@param arr 要统计的数组
*@param n 数组大小
*/
void cal_count( struct STR arr[] ,int n){
int i,j;
for(i = 0;i < n; i ++){
j = 0;
while(arr[i].str[j] != '\0'){
if(arr[i].str[j] == '1'){
arr[i].count ++;
}
j ++;
}
}
}
/**
*折半插入排序,数据量不大时,性能较好且稳定
*@param arr 要排序的数组
*@param n 数组大小
*/
void insert_sort(struct STR arr[] ,int n){
int i, j, low, high, mid;
struct STR tmp;
for(i = 1 ; i < n ; i ++){
tmp = arr[i];
low = 0;
high = i - 1;
while (low <= high){
mid = (low + high) / 2;
if( arr[mid].count > tmp.count ){
high = mid - 1;
}else{
low = mid + 1;
}
}
for(j = i-1; j >= high+1; j --){
arr[j+1] = arr[j];
}
arr[high + 1] = tmp;
}
}