汉字统计
Problem Description
统计给定文本文件中汉字的个数。
Input
输入文件首先包含一个整数n,表示测试实例的个数,然后是n段文本。
Output
对于每一段文本,输出其中的汉字的个数,每个测试实例的输出占一行。
[Hint:]从汉字机内码的特点考虑~
Sample Input
2
WaHaHa! WaHaHa! 今年过节不说话要说只说普通话WaHaHa! WaHaHa!
马上就要期末考试了Are you ready?
Sample Output
14
9
#include
#include
#include
using namespace std;
int main(){
int n;
cin>>n;
getchar();
while(n--){
string str;
getline(cin,str);
int sum=0;
for(int i=0;i
if(str[i]<0||str[i]>127)
sum++;
cout<
}
}
进制转换
Problem Description
输入一个十进制数N,将它转换成R进制数输出。
Input
输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16, R<>10)。
Output
为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)。
Sample Input
7 2
23 12
-4 3
Sample Output
111
1B
-11
#include
#include
using namespace std;
int main(){
int n,r;
while(scanf("%d%d",&n,&r)!=EOF){
char ans[40];
int a_size=0;
int k= n;
do{
//计算该位数字
int f = abs(n);
int x = f%r;
//将数字转换成字符
ans[a_size++] = (x<10)? x+'0':x-10+'A';
n /= r;
}while(n);
if(k<0)
ans[a_size++] = '-';
for(int i=a_size-1;i>=0;i--)
printf("%c",ans[i]);
printf("\n");
}
}
杨辉三角
Problem Description
还记得中学时候学过的杨辉三角吗?具体的定义这里不再描述,你可以参考以下的图形:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Input
输入数据包含多个测试实例,每个测试实例的输入只包含一个正整数n(1<=n<=30),表示将要输出的杨辉三角的层数。
Output
对应于每一个输入,请输出相应层数的杨辉三角,每一层的整数之间用一个空格隔开,每一个杨辉三角后面加一个空行。
Sample Input
2 3
Sample Output
1
1 1
1
1 1
1 2 1
#include
#include
using namespace std;
int main(){
int n;
while(scanf("%d",&n)!=EOF){
int **arr = new int*[n];
for(int i=0;i
arr[i] = new int[n];
for(int i=0;i
for(int j=0;j
if(j==0||j==i)
arr[i][j]=1;
else
arr[i][j]=arr[i-1][j]+arr[i-1][j-1];
}
for(int i=0;i
for(int j=0;j
cout<
cout<
}
cout<
}
}