C练习_1005
题自:题目 1009: [编程入门]数字的处理与判断_C语言网
题目描述
输入
输出
点击查看代码 ```点击查看代码 ``` ``` ```
样例输入
12345
样例输出
5
1 2 3 4 5
54321
我的解答:
1 // Code file created by C Code Develop
2
3 #include "ccd.h"
4 #include "stdio.h"
5 #include "stdlib.h"
6 #include "math.h"
7
8 int main() {
9 int num;
10 while (1)
11 {
12 // scanf("%d", &num);
13 num = 12345;
14
15 if (num < 100000) break;
16 }
17
18
19 int d;
20 if(num < 10) {
21 d = 1; }
22 else if(num < 100) {
23 d = 2;
24 }
25 else if(num < 1000) {
26 d = 3;
27 }
28 else if(num < 10000) {
29 d = 4;
30 }
31 else {
32 d = 5;
33 }
34 //1
35 printf("%d\n", d);
36 //2
37 int c = num;
38 int s[5];
39 for(int i = 1; i < d + 1; i++ ) {
40
41 int z = 0;
42 z = (int) (c) / pow(10 , (d - i));
43 c = c - z*pow(10 , (d - i));
44 s[i - 1] = z;
45 // printf("s[%d]:%d ",i-1, s[i - 1]);
46 // printf("10^d-i+1:%d\n", (int) pow(10, (d - i)));
47 //printf("c:%d\n", c);
48 printf("%d", z);
49 if(i < d) {
50 printf(" ");
51 }else {
52 printf("\n");
53 }
54 }
55
56 // printf("OK");
57 int b;
58 int sum = 0;
59 for(int i = 0; i < d; i++ ) {
60 // printf("s[%d]:%d ", d - i - 1, s[d - i - 1]);
61 sum += s [d - i - 1]*pow(10, (d - i - 1));
62 // printf("10^d-i+1:%d\n", s [d - i] * pow(10, (d - i-1)));
63 // printf("c:%d\n", sum);
64 }
65 printf("%d", sum);
66
67
68 c = max(1, 6);
69 //printf("%d", c);
70 return 0;
71
72 }
优质解答:1009: [编程入门]数字的处理与判断 (C语言)字符数组与常规思路
将输入的数字当作字符串来储存,用strlen( ) 函数得到字符串长度,即为数字位数
将字符串从左到右遍历输出(带空格)
最后还有一个回车不要忘了
再将字符串从右往左遍历输出(不带空格)就好了
注意事项:
题目说数字位数不超过5,所以定义字符数组长度应不小于6
因为字符串结尾还有结尾符 '\0' 占一位
1 #include <stdio.h>
2 #include <string.h> //C语言字符串头文件,strlen() 函数包含在此内
3
4 int main()
5 {
6 char str[10]; //定义字符串长度为 10
7 scanf("%s", str); //输入数字 (作为字符串输入)
8
9 int len = strlen(str); //获取字符串长度
10 printf("%d\n", len); //输出字符串长度,即为数字位数
11 for (int i = 0; i < len; i++) //从左往右遍历输出
12 {
13 printf("%c ", str[i]);
14 }
15 printf("\n"); //输出回车
16 for (int i = len - 1; i >= 0; i--) //从右往左遍历输出
17 {
18 printf("%c", str[i]);
19 }
20 return 0;
21 }
随机推荐
- Lakehouse 还是 Warehouse?(1/2)
Onehouse 创始人/首席执行官 Vinoth Chandar 于 2022 年 3 月在奥斯汀数据委员会发表了这一重要演讲.奥斯汀数据委员会是"世界上最大的独立全栈数据会议" ...
- 机器学习策略篇:详解理解人的表现(Understanding human-level performance)
理解人的表现 人类水平表现这个词在论文里经常随意使用,但现在告诉这个词更准确的定义,特别是使用人类水平表现这个词的定义,可以帮助推动机器学习项目的进展.还记得上个博客中,用过这个词"人类水平 ...
- MySQL学习笔记-函数
MySQL-常用函数 select {函数}({参数}); select是查询用的,用来展示函数返回值. 一. 字符串函数 常用的字符串函数: 1. concat 拼接 select concat(' ...
- ansible list错误
[root@localhost ansible]# ansible all -list [WARNING]: * Failed to parse /etc/ansible/1.txt with ini ...
- redux中集成immutable.js
安装redux-immutable redux中利用combineReducers来合并reducer并初始化state,redux自带的combineReducers只支持state是原生js形式的 ...
- [ROI 2018] Innophone 题解
[ROI 2018] Innophone 看了半天网上仅有的一篇题解--才堪堪写出来 不过在LOJ上看提交,全是 KTT,看得我瑟瑟发抖(不会 题意翻译 在平面上有一些点,你需要在这个平面上任意确定一 ...
- The model backing the 'MainDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).
The model backing the 'MainDbContext' context has changed since the database was created. Consider u ...
- 将MP4(视频)转换为MP3(音频)
使用VLC Media Player 步骤1. 在计算机上启动VLC Media Player,点击「媒体」并选择「转换/储存」. 步骤2. 点击「加入」以浏览并打开MP4文件,然后点击「Conver ...
- 获取前(后)x月的日期
package com.jesims.busresume.web; import org.springframework.stereotype.Service; import java.text.Da ...
- HttpServletRequest获取header参数 sign
HttpServletRequest获取header参数 sign //从请求头中获取参数 private static Map<String, String> getHeaders(Ht ...