A HDU 1042.N!(大数阶乘)
Click here come to the problem!
B HDU 1062.Text Reverse
Problem Description
Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single line with several words. There will be at most 1000 characters in a line.
Output
For each test case, you should output the text which is processed.
Sample Input
3
olleh !dlrow
m’I morf .udh
I ekil .mca
Sample Output
hello world!
I’m from hdu.
I like acm.
Hint
Remember to use getchar() to read ‘\n’ after the interger T, then you may use gets() to read a line and process it.
注意空格的处理
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int n;
scanf("%d", &n);
getchar();
while (n--){
char str[1010], ans[1010];
gets(str);
int len = strlen(str);
for (int i = 0; i < len; ){
if(str[i]!=' '){
int p, j, k = 0;
for(j = i; str[j]!=' '&&j<len; j++);
for(p = j-1; p>=i; p--)
ans[k++] = str[p];
ans[k] = '\0';
printf("%s", ans);
i = j;
}
else{
printf(" ");
i++;
}
}
printf("\n");
}
return 0;
}
C HDU 1860.统计字符
Problem Description
统计一个给定字符串中指定的字符出现的次数
Input
测试输入包含若干测试用例,每个测试用例包含2行,第1行为一个长度不超过5的字符串,第2行为一个长度不超过80的字符串。注意这里的字符串包含空格,即空格也可能是要求被统计的字符之一。当读到’#’时输入结束,相应的结果不要输出。
Output
对每个测试用例,统计第1行中字符串的每个字符在第2行字符串中出现的次数,按如下格式输出:
c0 n0
c1 n1
c2 n2
…
其中ci是第1行中第i个字符,ni是ci出现的次数。
Sample Input
I
THIS IS A TEST
i ng
this is a long test string
#
Sample Output
I 2
i 3
5
n 2
g 2
注:第2个测试用例中,空格也是被统计的字符之一。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
char str1[10], str2[100];
int co[10];
while (gets(str1)){
if (strcmp(str1, "#")==0)
break;
gets(str2);
int len1 = strlen(str1);
int len2 = strlen(str2);
memset(co, 0, sizeof(co));
for (int i = 0; i < len2; i++){
for(int j = 0; j < len2; j++){
if(str2[i]==str1[j])
co[j]++;
}
}
for (int i = 0; i < len1; i++){
printf("%c %d\n", str1[i], co[i]);
}
}
return 0;
}
D HDU 1251.统计难题(字典树)
Click here come to the problem
F HDU 1014.Uniform Generator
Click here come to the problem
G HDU 1061.Rightmost Digit
Problem Description
Given a positive integer N, you should output the most right digit of N^N.
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).
Output
For each test case, you should output the rightmost digit of N^N.
Sample Input
2
3
4
Sample Output
7
6
Hint
In the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7.
In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.
思路:找规律的一道题,在纸上把从1到9所有可能性都列出来,会发现周期最大才是4,所以下面g = m%4,减少乘的次数,既然题目要求只要个位数上的值,那每次乘完%10取个位上的数就好了,减少运算量,速度能快一些,关键就是看能否找到规律
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int n, m;
scanf("%d", &n);
while(n--){
scanf("%d", &m);
int g = m%4, s = m%10; //s是把m的个位数取出来用
if(g==0) g = 4; //注意这一点,刚开始给忘掉了,就是g==0的时候,因为周期是4,要让g = 4
int ans = 1;
for(int i = 0; i < g; i++)
ans = ans*s%10; //每次乘完再取个位数
printf("%d\n", ans);
}
return 0;
}
H HDU 1012.u Calculate e
Problem Description
A simple mathematical formula for e is
where n is allowed to go to infinity. This can actually yield very accurate approximations of e using relatively small values of n.
Output
Output the approximations of e generated by the above formula for the values of n from 0 to 9. The beginning of your output should appear similar to that shown below.
Sample Output
n e
0 1
1 2
2 2.5
3 2.666666667
4 2.708333333
#include<stdio.h>
int main()
{
double i, a, sum;
printf("n e\n");
printf("- -----------\n");
printf("0 1\n1 2\n2 2.5\n");
sum = 2.5;
for(i = 3, a = 2; i < 10; i++)
{
a = a*i;
sum += 1/a;
printf("%.lf %.9lf\n", i, sum);
}
return 0;
}