您的位置:首页 > 编程语言

ACM编程协会第三次作业答案参考

2013-10-19 12:46 423 查看

第一题:考察最基本的排序(冒泡或者选择都可以做出来)。

这一题昨晚已经讲过,答案就不贴出来了。

第二题:杨辉三角。

主要要观察规律,形成程序即可。(题目假设最大的宽度不超过300)

代码如下:

//每日一练习--Written By C_Hu

/**************************************************************
杨辉三角的特点是:行的序数和该行的数字数目相等,且首个和末尾均为1
每一行(显然从第三行开始)的首尾间的每个数字等于上一行的相对应左右两个数字之和。
即第n+1(n>2)行的第i个数等于第n行的第i-1个数和第i个数之和。可用此性质写出整个杨辉三角。
***************************************************************/

// 下为参考代码,仅供参考。
// 编译环境,VC++ 6.0

#include <stdio.h>
void main()
{
int a[100][100],n,i,j;
// printf("请输入要求输出的层数(1-30):") ;
while (scanf("%d",&n)!=EOF && n != 0)
{
a[0][0]=1;a[1][0]=1;a[1][1]=1;  // 第一层和第二层直接赋值1

if(n==1)
printf("%d\n",a[0][0]);    // 要求输出一层时,直接输出
else
if(n==2)
{
printf("%d\n",a[0][0]);
printf("%d %d\n",a[1][0],a[1][1]);
}                         // 要求输出二层时,直接输出
else
{
for(i=2;i<n;++i)
for(j=0;j<=i;++j)
{
if(j==0||j==i)
a[i][j]=1;   // 行的序数和该行的数字数目相等,且首个和末尾均为1
else
a[i][j]=a[i-1][j-1]+a[i-1][j];
}     // 每行从第二个数字开始第n+1行的第i个数等于第n行的第i-1个数和第i个数之和
for(i=0;i<n;++i)
{
printf("%d",a[i][0]);
for(j=1;j<=i;++j)
printf(" %d",a[i][j]);
printf("\n");
}   // 输出杨辉三角
}
printf("\n");
}

}


第三题:寻找指定字符。

这个是简答的字符串匹配题。

参考代码如下:

原始题目在此地:http://blog.csdn.net/hu1020935219/article/details/12029555

/******** written by C_Shit_Hu ************/

////////////////字符串函数///////////////

/****************************************************************************/
/*
输入:输入先包含一个长度为n(n小于100)的字符串str_1(可以包含数字和字母),
然后输入一个长度为m(m<=n)指定的字符串str_2。你的任务是查找str_1中是否存在str_2,
如果存在输出最多出现的次数(不可以重复利用某一个字母或数字);如果不存在,输出0。

输出:对每组输入,输出在Str_1中要查找的目标字符串出现的次数。
*/
/****************************************************************************/

// 就是考察一个简单的字符串函数

#include <stdio.h>
#include <string.h>

char a[1001], b[1001];

int main()
{
int len, c;
while(scanf("%s", a)==1&&a[0]!='#')
{
scanf("%s", b);
len = strlen(b);
c = 0;
char *p = a;
while((p=strstr(p, b))!=0) // 一个字符串函数strstr ,希望大家尽量这里不要参考我的,你们自己写一下不用这个字符串处理函数的做法。。
{
c++;
p += len;
printf("%s\t", p) ;
}
printf("%d\n", c);
}
return 0;
}

/******************************************************/
/********************  心得体会  **********************/
/*

*/
/******************************************************/


第四题:如果观察出数学规律,,这道题可以很简单。。。。

题目传送门:http://blog.csdn.net/hu1020935219/article/details/12503049

代码再发一遍:

/***** Digital Roots********/

/******** written by C_Shit_Hu ************/

////////////////简单大数///////////////

/****************************************************************************/
/*
Problem Description:
The digital root of a positive integer is found by summing the digits of the integer.
If the resulting value is a single digit then that digit is the digital root.
If the resulting value contains two or more digits, those digits are summed and the process is repeated.
This is continued as long as necessary to obtain a single digit.
For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6.
Since 6 is a single digit, 6 is the digital root of 24.
Now consider the positive integer 39. Adding the 3 and the 9 yields 12.
Since 12 is not a single digit, the process must be repeated.
Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.

Input:
The input file will contain a list of positive integers(the length of each integer will not exceed 1000),
one per line. The end of the input will be indicated by an integer value of zero.

Output:
For each integer in the input, output its digital root on a separate line of the output.
*/
/****************************************************************************/

// 数学规律是大神!!!
#include<stdio.h>
#include<string.h>
char s[1010];
int main()
{
int sum,i,len,x;
while(gets(s)&&s[0]!='0')
{
sum=0;
len=strlen(s);
for(i=0;i<len;i++)
sum+=s[i]-'0';
x=sum%9;
if(x==0) x+=9;
printf("%d\n",x);
}
return 0;
}

/******************************************************/
/********************  心得体会  **********************/
/*
说明1.各位数字相加知道一位数字x,这个x等于原来的数除以9的余数(原数为9的倍数除外)
2.若原数为9的倍数,最后结果x+9就行了
3.一个数字除以9的余数等于该数各数字之和除以9的余数。(这点为大数运算提供了极大的方便!)

水水更健康!!!
*/
/******************************************************/


第五题:合并输出

即是如果某个字符连续出现,就线将重复出现的次数输出,为方便循环再将最后一次出现的字符输出。

代码如下:

// 字符串统计

/**************************************************/
/*
Problem Description
Given a string containing only 'A' - 'Z', we could encode it using the following method:
1. Each sub-string containing k same characters should be encoded to "kX" where "X" is the only character in this sub-string.
2. If the length of the sub-string is 1, '1' should be ignored.

Input
The first line contains an integer N (1 <= N <= 100) which indicates the number of test cases.
The next N lines contain N strings. Each string consists of only 'A' - 'Z' and the length is less than 10000.

Output
For each test case, output the encoded string in a line.
*/
/*************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAX 10010
char s[MAX];       // 定义字符数组

int main()
{
int l,i,j,N,count;
scanf("%d\n",&N);

for(i=0;i<N;i++)
{
memset(s,'\0',sizeof(s));    // 数组清零
gets(s);                     // 从键盘输入字符串并存入数组中
l=strlen(s);                 // 计算数组的实际长度(里面存储的实际内容的长度)
for(j=1,count=1;j<=l;j++)
{
if(s[j] == s[j-1])
count++;             // 如果后一个字符跟前一个相同,则计数器count,自动加一
else
{
if(count>1)
printf("%d%c",count,s[j-1]);        // 如果计数器count大于1,则输出count的值以及他所指定的字符
else  printf("%c",s[j-1]);              // 否则,也即是如果是单个的字符,直接输出该字符即可
count=1;                                // 将count归于1,这个一定记住。如果不归于1,则会把本次的计算结果
// 带到下一次,影响下一次的计算
}
}
printf("\n");                                   // 换行,计算之后要换行。
}
return 0;
}


第六题:简单的大数题目。

(什么是大数:大数在编程中表示超过32位二进制位的数.即是基本的数据类型无法表示的数据)

代码如下:(关键在于模拟手算,即是各个数位的数字相加的可能会大于十,即是要有进位的记录,并加到下一位中)。

关键部分的代码如下:(这里不用输入为0,0时候程序结束运行,而改用先输入一个N,然后是N行测试)

/******** written by C_Shit_Hu ************/

////////////////题目属性///////////////

//
#include <stdio.h>
#include <string.h>

int main(){
char str1[1001], str2[1001];  // 定义数组,开辟空间

int t, i, len_str1, len_str2, len_max, num = 1, k;
scanf("%d", &t);               // 输入t,代表t行数测试数据
getchar();                     // 吸收回车键

while(t--){
int a[1001] = {0}, b[1001] = {0}, c[1001] = {0};  // 数组清零

scanf("%s", str1);         // 读入字符串
len_str1 = strlen(str1);   // 计算长度
for(i = 0; i <= len_str1 - 1; ++i)
a[i] = str1[len_str1 - 1 - i] - '0';  // 将字符转化为数字,并存入整数数组中

scanf("%s",str2);          // 同上
len_str2 =  strlen(str2);
for(i = 0; i <= len_str2 - 1; ++i)
b[i] = str2[len_str2 - 1 - i] - '0';

if(len_str1 > len_str2)     // 取最大的数组长度
len_max = len_str1;
else
len_max = len_str2;

k = 0;
for(i = 0; i <= len_max - 1; ++i){
c[i] = (a[i] + b[i] + k) % 10;
k = (a[i] + b[i] + k) / 10;      // 关键部分。对应各个数位相加,大于十的取余,进位k计入下一位相加中
}

if(k != 0)
c[len_max] = 1;                // 最高位

printf("Case %d:\n", num);        // case 计数器
num++;
printf("%s + %s = ", str1, str2);
// 输出结果
if(c[len_max] == 1)
printf("1");
for(i = len_max - 1; i >= 0; --i){
printf("%d", c[i]);
}
printf("\n");
if(t >= 1)

printf("\n");
}
return 0;
}

/******************************************************/
/********************  心得体会  **********************/
/*

*/
/******************************************************/


附加题目:

代码如下:

/******** written by C_Shit_Hu ************/

////////////贪心算法入门题目/////////////////

// 简单的贪心算法题目
// 本题目代码可以优化,主要是冒泡排序可优化

#include <stdio.h>
#include <string.h>

int main()
{
int n, i, j;
int temp_e, temp_s;
int time, count ;
int Ti_s[110], Ti_e[110] ;
while (scanf("%d", &n) != EOF && n != 0)
{
// 输入节目表,并分别将开始和结束时间存入对应数组
for (i = 0; i<n; i++)
{
scanf("%d%d", &Ti_s[i], &Ti_e[i]) ;
}

// 将时间表按照结束时间的先后顺序排序
for (i=0; i<n; i++)
for (j=i+1; j<n; j++)
{
if (Ti_e[i] > Ti_e[j])
{
temp_e = Ti_e[j] ;
Ti_e[j] = Ti_e[i] ;
Ti_e[i] =temp_e ;

temp_s = Ti_s[j];
Ti_s[j] = Ti_s[i] ;
Ti_s[i] = temp_s ;

}
}

// 贪心算法主要实现部分,一步一步的向后选择
// 下一个的开始时间必须要小于等于上个节目的结束时间
time = 0;
count = 0;
for (i=0; i<n; i++)
{
if (Ti_s[i] >= time)
{
count++ ;         // 计数器
time = Ti_e[i] ;
}
}
printf("%d\n", count) ;  // 输出即可
}

return 0 ;
}

/******************************************************/
/********************  心得体会  **********************/
/*

*/
/******************************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: