C语言题两题

1,给出一个字符串,在有数字的地方加上M。样例:输入:10Thereare10apples.输出:Thereare20apples.2,(动态规划题)在N人中,一些崇拜甲... 1,给出一个字符串,在有数字的地方加上M。
样例:
输入:10
There are 10 apples.
输出:There are 20 apples.
2,(动态规划题)
在N人中,一些崇拜甲,另一些崇拜乙,将N人排成一列
把他们分入几个房间内,这个房间内只能全部崇拜一个人,或者崇拜不同人的人数差不超过M;并且只能将连续的人分入一个房间,不可以挑选人,求最少要多少房间?
样例:(1,2分别表示崇拜不同的人)
输入:5
1 2 2 2 1 2 2 2 2 1 1 2 1
输出:2
是Dev C++
最好再加算法思想
展开
 我来答
liulin0017
2011-08-09 · 超过15用户采纳过TA的回答
知道答主
回答量:56
采纳率:0%
帮助的人:46.8万
展开全部
第一题:
/*1,给出一个字符串,在有数字的地方加上M。
样例:
输入:10
There are 10 apples.
输出:There are 20 apples
*/

/* 检测该字母是否是数字 */
int check_num(char num)
{
if( (num >= '0') && (num <= '9') )
{
return 1;
}
else
{
return 0;
}
}

/* 数字转化为字母 */
int int_to_char( char *tmp_char, int num)
{
int tmp_num = 1;
int i = 0;
char swp;
int total = 0;

while(num != 0)
{
tmp_num = num % 10;
num /= 10;
tmp_char[total++] = tmp_num+'0';
}
tmp_char[total] = '\0';

for(i = 0; i < total/2; i++)
{
swp = tmp_char[total-1-i];
tmp_char[total-1-i] = tmp_char[i];
tmp_char[i] = swp;
}

printf(" tmp_char = %s\n",tmp_char);

return total;
}

/* 字母转化为数字 */
int char_to_int(char *num)
{
int i = 0;
int ret = 0;

while(num[i] != '\0')
{
ret = (ret*10) + (num[i] - '0');
i++;
}

printf("ret = %d\n",ret);

return ret;
}

/* 字符串倒序拷贝 a[3] ={abc};b[3]={第一拷贝'\0' 拷贝c 然后拷贝b 然后拷贝a} */
char* strcpy_B(char *src, char *dest)
{
int total_num = 0;
char *tmp_src = src;
char *tmp_dest = dest;

while( (*tmp_src++) != '\0')
{
total_num++;
}

printf("total_num = %d\n",total_num);

while(total_num >= 0)
{
*(tmp_dest+total_num) = *(src+total_num);
total_num--;
}

return tmp_dest;
}

/* 将加后的数字回写入字符串中 */
void add_num_char(char *src, int add_num, int num,int num_pos)
{
int total_num_bef = 0;
int total_num_aft = 0;
char tmp_char[7] = {0};
int i = 0;

num += add_num;

total_num_bef = int_to_char(tmp_char,num);
total_num_aft = int_to_char(tmp_char,num);

printf("total_num_bef = %d\n",total_num_bef);
printf("total_num_aft = %d\n",total_num_aft);
printf("^^tmp_char = %s\n",tmp_char);

if(total_num_aft - total_num_bef > 0)
{
strcpy(src+num_pos,src+num_pos+(total_num_aft - total_num_bef));
}

while(tmp_char[i] != '\0')
{
*(src+(num_pos+1-total_num_bef)+i) = tmp_char[i];
i++;
}

}

void main(void)
{
int add_num = 0;
char your_sen[100];
int total_num = 0;
char ch = NULL;
char tmp_charnum[6];
int tmp_intnum = 0;
int j = 0;
int cyc_i = 0;
int num_pos = 0;

memset(your_sen,0x0,sizeof(your_sen));

printf(" add num :");
scanf("%d",&add_num);

ch =getchar();

input:
printf("your sen:");
while((ch=getchar()) != '\n')
{
your_sen[total_num++] = ch;

}
your_sen[total_num] = '\0';

if(your_sen[0] == '\0')
{
printf("input error ,please input again!\n");
goto input; /* 输入错误重置 */
}

printf("your_sen = %s\n",your_sen);

for(cyc_i = 0;cyc_i <= total_num; cyc_i++)
{
if(check_num(your_sen[cyc_i]) == 1)
{
tmp_charnum[j++] = your_sen[cyc_i];
num_pos = cyc_i;
}
else
{
if(j != 0)
{
tmp_intnum = char_to_int(tmp_charnum);
printf("tmp_intnum = %d\n",tmp_intnum);
add_num_char(your_sen, add_num, tmp_intnum,num_pos);
}
j = 0;
}
tmp_charnum[j] = '\0';
}
printf("your_sen = %s\n",your_sen);
}
思想就是找到数字串 转化为 int型,加上数字 在转化为字符串放回去,中间判断下位数是否增加99+1 = 100.

输入:
add num:10
your sen: this is 100 apples

第二题:
思路是这样的
成立的条件是 崇拜甲的(kind_1)
崇拜乙的(kind_2)
kind_1 =0 (全是崇拜乙的)
kind_2 = 0(全是崇拜甲的)
abs(kind2-kind1)< M(两者之差的绝对值小于M)

M=1 12222122221
首先判断所有的是否符合条件
然后从最后一个丢掉,开始向前计算剩下的是否符合以上条件。
/*
2,(动态规划题)
在N人中,一些崇拜甲,另一些崇拜乙,将N人排成一列
把他们分入几个房间内,这个房间内只能全部崇拜一个人,或者崇拜不同人的人数差不超过M;并且只能将连续的人分入一个房间,不可以挑选人,求最少要多少房间?
样例:(1,2分别表示崇拜不同的人)
输入:5
1 2 2 2 1 2 2 2 2 1 1 2 1
输出:1
*/
#define N 100

static int house_num;

int check_kind(int kind_fir, int kind_sen, int dif_num)
{
if( (kind_fir == 0) || (kind_sen ==0) )
{
return 1;
}
else if(abs(kind_sen - kind_fir) <= dif_num)
{
return 1;
}
else
{
return 0;
}

return 0;
}

int check_rule(char *people_queue, int length, int check_pos, int dif_num, int kind_fir, int kind_sen)
{
int i = length-1;
int match_statu = 0;
int tmp_kind_fir = 0;
int tmp_kind_sen = 0;

while(i >= check_pos)
{
if(people_queue[i] == '1')
{
tmp_kind_fir++;
}
else if(people_queue[i] == '2')
{
tmp_kind_sen++;
}

match_statu = check_kind(kind_fir-tmp_kind_fir, kind_sen-tmp_kind_sen, dif_num);

if(match_statu == 1)
{
house_num++;
if( check_kind(tmp_kind_fir,tmp_kind_sen,dif_num) == 1)
{
house_num++;
i = length;
}
break;
}

i--;
}

return i;
}

void match_queue(char *people_queue, int length, int dif_num, int kind_fir, int kind_sen)
{
int left_length = length-1;
int check_pos = 0;
int i = 0;
int tmp_kind_fir = 0;
int tmp_kind_sen = 0;

while(left_length >= check_pos)
{
i = 0;

tmp_kind_fir = 0;
tmp_kind_sen = 0;

if(check_pos != 0)
{
while(i <= check_pos)
{
if(people_queue[i] == '1')
{
tmp_kind_fir++;
}
else if(people_queue[i] == '2')
{
tmp_kind_sen++;
}
i++;
}
}
check_pos = check_rule(people_queue, length, check_pos, dif_num, kind_fir-tmp_kind_fir, kind_sen-tmp_kind_sen);
}
}

int main(void)
{
int dif_num = 0;
char people_arr[100]={0};
int i = 0;
char ch = NULL;
int kind_fir = 0;
int kind_sen = 0;
int match_flg = 0;
char match_type[150] = {0};
int total_num = 0;

printf(" int the diffent between the two kind of people :");
scanf("%d",&dif_num);

ch =getchar();

printf("input two kind of people (example:12212122222):");
while( ((ch=getchar()) != '\n')
&& (ch >= '1')
&& (ch <= '2')
)
{
if(ch == '1')
{
kind_fir++;
}
else if(ch == '2')
{
kind_sen++;
}
people_arr[total_num++] = ch;

}
people_arr[total_num] = '\0';

printf("kind_fir = %d\n",kind_fir);
printf("kind_sen = %d\n",kind_sen);
printf("%s\n",people_arr);

if( abs(kind_sen - kind_fir) <= dif_num) /* 如果双方人数之差小于M,放一个房子就够了 */
{
house_num = 1;
printf("house_num = %d\n",house_num,kind_fir,kind_sen);
}
else
{
match_queue(people_arr,total_num,dif_num,kind_fir,kind_sen);
}

printf("house_num = %d\n",house_num);

return 1;
}
追问
这不是Dev C++吧,把第二题的具体思路给我就行了
yzl427
2011-08-08 · 超过11用户采纳过TA的回答
知道答主
回答量:77
采纳率:0%
帮助的人:41.5万
展开全部
第一题简单
遇到数字
s=s*10+str[i]-'0';
直到出现字母
s+=m
printf(“%d”,s);
s=0;
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式