8.5训练

本文介绍了使用C语言和C++实现字符串反转的方法,包括利用数组遍历和字符交换技巧。此外,还探讨了四舍五入的算法实现,通过比较小数部分与阈值来决定是否进行进位操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

8.5训练

问题一:输入字符串,反转输出

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

int main(int argc,char *argv[]){
char str[1024];//在不知道输入字符串长度时
while(scanf("%s",str)!=EOF){
//printf("%s",str);
int i,n=strlen(str);
for(i=n-1;i>=0;i–) printf("%c",str[i]);
printf("\n");
}

return 0;   

}

#include
#include
using namespace std;
int main(){
string str;//c+±-可变长度字符类型
cin >> str;
for(int i = str.size()-1; i >=0;i–)
cout << str[i];
}

问题二:四舍五入

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

int main(int argc,char *argv[]){
float n;
while(scanf("%f",&n)!=EOF){
int a;
float b,k=0.5;
a=(int)n;//取整
b=n-a;//取小数部分
//printf("%f",b);
if(b<k) printf("%d\n",a);
else printf("%d\n",a+1);
}
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值