个人学习编程(3-10) 刷题
逆序字符串
如果是对于输入的字符串
char s[100]; // 用来存储用户输入的字符串
printf("请输入一个字符串: ");
scanf("%s", s); // 获取用户输入的字符串
#include <stdio.h>
#include <vector>
#include <queue>
#include <string.h>
using namespace std;
int main() {
char s[] = "i like laoda";
int len = 0;
len = strlen(s);
int i;
char temp;
for (int i = 0; i < len/2; i++)
{
temp = s[i];
s[i] = s[len-i-1];
s[len-i-1] = temp;
}
printf("%s",s);
return 0;
}
指针解法:
#include <stdio.h>
#include <vector>
#include <queue>
#include <string.h>
using namespace std;
int main() {
char s[] = "i like laoda";
int len = 0;
char *p = s;
while (*p != '\0')
{
len++;
p++;
}
//printf("len=%d\n",len);
int i;
char temp;
for (i = 0; i < len/2; i++)
{
temp = *(s+i);
*(s+i) = *(s+len-i-1);
*(s+len-i-1) = temp;
}
printf("%s\n",s);
return 0;
}
递归算年龄
第五个人说比第四个人大两岁
第四个人说比第三个人大两岁
第三个人说比第二个人大两岁
第二个人说比第一个人大两岁
第一个说他十岁,问第五个人多大?
#include <stdio.h>
#include <vector>
#include <queue>
#include <string.h>
using namespace std;
int age(int n){
int c = 0;
if (n == 1)
{
c = 10;
}else{
c = age(n-1) + 2;
printf("c = %d\n", c);
}
return c;
}
int main() {
int age(int n);
printf("age(5)=%d\n",age(5));
return 0;
}