8 条题解
-
1
思路1:取位输出
难点 :题目表示给定的是一个3位数字,清楚知道每一位的对应取位方式。
对应取位方式:
n%10//取出个位,例如321%10=1 n/10%10//取出十位,例如321/10=32%10=2 n/100//取出百位,例如321/100=3
参考代码:
#include <iostream> using namespace std; int main(){ int n; cin>>n;//输入三位数 cout<<n%10;//求出个位,输出 cout<<n/10%10;//求出十位,输出 cout<<n/100;//求出百位,输出 return 0; }
思路2:while 循环数位拆分
通过拆位舍位实现数字倒序输出(所有数字通用)
难点:while 循环条件,拆位以及舍位的过程
模板代码:
while(n>0){ n%10; n=n/10; }
例子:321的拆位过程
-
321%10=1 取到个位数字1 , n = 321/10=32舍弃已经取出的个位数字
-
32%10=2取到此时的个位数字2, n = 32/10=3舍弃已经取出的个位数字
-
3%10=3取到此时的个位数字3, n = 3/10=0舍弃已经取出的个位数字
注:所有数字舍位最后都会舍弃到0 ,到0也就意味着取位已经取完了可以结束循环
参考代码:
#include <iostream> using namespace std; int main(){ int n; cin>>n;//输入这个数字 while(n>0){ cout<<n%10;//输出每次取出来的个位数 n=n/10;//舍弃个位 } return 0; }
-
-
1
思路 个十百三位位置上数字保持不变,仅仅是改变个位十位百位上的数字的位置输出。
知识点 987%100表示987除以100的余数,为87; 87/10表示87除10的整数,为8;
易漏点 scanf与&(易漏写)搭配使用; int main()与return对应; int main()的 括号易漏 代码
#include<stdio.h> int main() { int n,n1,n2,n3; scanf("%d",&n); n1=n/100; n2=n%100/10; n3=n%10; printf("%d%d%d",n3,n2,n1); return 0; }```
- 1
信息
- ID
- 97
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 3
- 标签
- 递交数
- 304
- 已通过
- 162
- 上传者