拜托各位了 谢谢~
//n瓶数,m瓶盖数
int GetCount(int n,int m)
{
if (n < 2 && m < 4)
return 0;
int a = n / 2;
int b = n % 2;
int c = m / 4;
int d = m % 4;
return n+a + c + GetCount(a+c+b, d);
}
假如10块钱,买5瓶,有5个瓶盖
printf("%d\n",GetCount(5,5));
public static int fun7(int x,int y){
if(x>=2||y>=4){
return fun7(((x/2)+x%2+y/4),(y/4+y%4+x/2))+(int)(x/2)+(int)(y/4);
}
System.out.println("剩余瓶子"+x+";剩余盖子"+y);
return 0;
}
这是java代码,调用时候, 以价格i。 i/2+fun7(i/2,i/2) = 最终多少瓶
楼上的算法有漏洞,修改:
//n瓶数,m瓶盖数
#include<stdio.h>
int GetCount(int n,int m)
{
if (n < 2 && m < 4)
return n;
int a = n / 2;
int b = n % 2;
int c = m / 4;
int d = m % 4;
return n-b + GetCount(a+c+b,a+c+ d);
}
int main()
{
//假如10块钱,买5瓶,有5个瓶盖
printf("%d\n",GetCount(5,5));
}
第一个问题描述不清,不知道是不是可以借啤酒瓶和瓶盖,我的程序按照不可以编写:
#include <stdio.h>
int foo(int money)
{
int c = money / 2;
int c1 = 0;
int b = c;
int t = c;
while (b >= 2 || t >= 4)
{
c1 = b / 2;
b = b % 2 + c1;
t = t + c1;
c += c1;
c1 = t / 4;
b = b + c1;
t = t % 4 + c1;
c += c1;
}
return c;
}
int main()
{
int money = 10;
//scanf("%d", &money);
int c = foo(money);
printf("%d\n", c);
}
15
//-----------------------------第二题--------------------------
#include
#include
#include
int main()
{
char* string;
int i=0;
int MAX_SIZE = 100;
if(!(string = (char* )malloc(sizeof(char)*MAX_SIZE))){
exit(-1);
}
while(1){
gets(string);
if(strlen(string)<=MAX_SIZE)
{
for( i=strlen(string)-1; i>=0 ;i-- ){
string[i+1] = string[i]-32;
}
string[0] = ' ';
printf("%s",string);
}else{
if(!(string = (char*)malloc(sizeof(char)*(MAX_SIZE+10)))){
MAX_SIZE+=10;
exit(-1);
}
}
}
return 0;
}
#include <stdio.h>
#include <string.h>
void foo1(char * s)
{
int i;
int n = strlen(s);
for (i = n; i >= 0; i--)
s[i + 1] = s[i];
s[0] = '_';
}
void foo2(char * s)
{
int i;
int n = strlen(s);
for (i = 0; i < n; i++)
{
if (s[i] >= 'a' && s[i] <= 'z')
s[i] = s[i] - 'a' + 'A';
else if (s[i] >= 'A' && s[i] <= 'Z')
s[i] = s[i] - 'A' + 'a';
}
}
int main()
{
char s[100] = "Hello World";
printf("%s\n", s);
foo1(s);
printf("%s\n", s);
foo2(s);
printf("%s\n", s);
}
Hello World
_Hello World
_hELLO wORLD