码迷,mamicode.com
首页 > 其他好文 > 详细

PTA(Basic Level) 1002

时间:2020-06-05 00:55:23      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:div   ann   参数   ima   技术   tom   运算符   src   round   

1002 写出这个数 (20分)
 

读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字。

输入格式:

每个测试输入包含 1 个测试用例,即给出自然数 n 的值。这里保证 n 小于 10的100次方。

输出格式:

在一行内输出 n 的各位数字之和的每一位,拼音数字间有 1 空格,但一行中最后一个拼音数字后没有空格。

输入样例:

1234567890987654321123456789
 

输出样例:

yi san wu

思路:n<10^100,可得n各位数字之和不会超过三位数。最大为9*99=891。
#include<iostream>
#include<string>
using namespace std;
int main() {
 string n;
 cin >> n;
 int s1, s2, s3;
 int l = n.size();
 int sum = 0;
 for (int i = 0; i < l; i++) 
  sum += n[i] - ‘0‘;
 
 string d[10] = { "ling","yi","er","san","si","wu","liu","qi","ba","jiu" };
 s1 = sum / 100;
 s2 = (sum / 10) % 10;
 s3 = sum % 10;
 if (s1 == 0 && s2 == 0) cout << d[s3];
 else if (s1 == 0 && s2 != 0) cout << d[s2] << " " << d[s3];
 else cout << d[s1] << " " << d[s2] << " " << d[s3];
 return 0;
}
 
 
学习笔记:
1、
#include<iostream>
#include<string>
using namespace std;
int main() {
 string s;
 cin >> s;
 int l = s.size();
 int n = s.length();
 cout << l<<" "<<s[l-1];//输入abcdef,输出f
 cout << n << " " << s[n - 1];//得到相同的结果
 return 0;
}
 
2、
#include<iostream>
using namespace std;
int main() {
 char c[] = "abcdef";
 char* cc = c;
 char a[40] = "abcdef";
 int b[] = { 1,2,3 };
 int* bb = b;
 cout << sizeof(c) << endl << sizeof(cc) << endl << sizeof(*cc) << endl << sizeof(a) << endl;
 cout << sizeof(b) << endl << sizeof(b) << endl << sizeof(*bb);
 return 0;
}
 
技术图片
 
 
sizeof(...)是运算符,其值在编译时即计算好了,参数可以是数组、指针、类型、对象、函数等。
它的功能是:获得保证能容纳实现所建立的最大对象的字节大小。
由于在编译时计算,因此sizeof不能用来返回动态分配的内存空间的大小。
 
 

PTA(Basic Level) 1002

标签:div   ann   参数   ima   技术   tom   运算符   src   round   

原文地址:https://www.cnblogs.com/stray-yang/p/12568895.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!