2020. 絕對值排序
題目
Problem Description
輸入n(n<=100)個整數,按照絕對值從大到小排序后輸出。題目保證對於每一個測試實例,所有的數的絕對值都不相等。
Input
輸入數據有多組,每組占一行,每行的第一個數字為n,接着是n個整數,n=0表示輸入數據的結束,不做處理。
Output
對於每個測試實例,輸出排序后的結果,兩個數之間用一個空格隔開。每個測試實例占一行。
Sample Input
3 3 -4 2
4 0 1 2 -3
0
Sample Output
-4 3 2
-3 2 1 0
代碼
#include <stdio.h>
#include <stdlib.h>
int a[120];
int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
if(n == 0) break;
for(int i=0;i < n; i++)
{
scanf("%d", &a[i]);
}
//冒泡排序
for(int i=0; i < n-1; i++)
for(int j=0; j < n-i-1; j++)
{
if(abs(a[j]) < abs(a[j+1]))
{
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
//打印a[i]
for(int i = 0; i < n; i++)
{
if(i == n-1) {printf("%d\n", a[i]); break;}
printf("%d ", a[i]);
}
}
return 0;
}
思路與總結
重點:排序算法
冒泡排序
關鍵代碼如下:
//冒泡排序
for(int i=0; i < n-1; i++)
for(int j=0; j < n-i-1; j++)
{
if(abs(a[j]) < abs(a[j+1]))
//絕對值按從大到小排列,每次冒泡最小的(即把最小的放在最后)
{
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
2021. 發工資咯
題目
Problem Description
財務處的小胡老師最近就在考慮一個問題:如果每個老師的工資額都知道,最少需要准備多少張人民幣,才能在給每位老師發工資的時候都不用老師找零呢?
這里假設老師的工資都是正整數,單位元,人民幣一共有100元、50元、10元、5元、2元和1元六種。
Input
輸入數據包含多個測試實例,每個測試實例的第一行是一個整數n(n<100),表示老師的人數,然后是n個老師的工資。
n=0表示輸入的結束,不做處理。
Output
對於每個測試實例輸出一個整數x,表示至少需要准備的人民幣張數。每個輸出占一行。
Sample Input
3
1 2 3
0
Sample Output
4
代碼
#include<stdio.h>
int main()
{
int i,j,n,t,k,s[6]={100,50,10,5,2,1};
while(~scanf("%d",&n),n) //逗號表達式
{
for(i=j=k=0;i<n;i++,j=0)
{
scanf("%d",&t);
while(t)
{
if(t<s[j]) j++;
else t-=s[j],k++;
}
}
printf("%d\n",k);
}
return 0;
}
思路與總結
逗號表達式
(3+5,6+8)稱為逗號表達式,其求解過程先表達式1,后表達式2,整個表達式值是表達式2的值,如:(3+5,6+8)的值是14
2020.
題目
Problem Description
輸入一個字符串,判斷其是否是C的合法標識符。
Input
輸入數據包含多個測試實例,數據的第一行是一個整數n,表示測試實例的個數,然后是n行輸入數據,每行是一個長度不超過50的字符串。
Output
對於每組輸入數據,輸出一行。如果輸入數據是C的合法標識符,則輸出"yes",否則,輸出“no”。
Sample Input
3
12ajf
fi8x_a
ff ai_2
Sample Output
no
yes
no
代碼
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int n;
scanf("%d", &n);
getchar();
while (n--) {
char c, flag = 1;
c = getchar();
if (!(isalpha(c) || c == '_'))
flag = 0;
while ((c = getchar()) != '\n') {
if (!(isalnum(c) || c == '_'))
flag = 0;
}
printf("%s\n", flag ? "yes" : "no");
}
return 0;
}
思路與總結
按照字符流來處理,好好消化!
標識符輸入結束判斷語句:c = getchar()) != '\n'
2025. 查找最大元素
題目
Problem Description
對於輸入的每個字符串,查找其中的最大字母,在該字母后面插入字符串“(max)”。
Input
輸入數據包括多個測試實例,每個實例由一行長度不超過100的字符串組成,字符串僅由大小寫字母構成。
Output
對於每個測試實例輸出一行字符串,輸出的結果是插入字符串“(max)”后的結果,如果存在多個最大的字母,就在每一個最大字母后面都插入"(max)"。
Sample Input
abcdefgfedcba
xxxxx
Sample Output
abcdefg(max)fedcba
x(max)x(max)x(max)x(max)x(max)
代碼
#include<stdio.h>
#include<string.h>
int main()
{
char max,str[101];
int len,i;
while(~scanf("%s",str))
{
len=strlen(str);
max='a';
for(i=0;i<=len-1;i++)
{
if(str[i]>max)
{
max=str[i];
}
}
for(i=0;i<=len-1;i++)
{
printf("%c",str[i]);
if(str[i]==max)
{
printf("(max)");
}
}
printf("\n");
}
return 0;
}
思路與總結
1. 找到最大字母
2. 在最大字母后,輸出(Max)
2026. 首字母便大寫
題目
Problem Description
輸入一個英文句子,將每個單詞的第一個字母改成大寫字母。
Input
輸入數據包含多個測試實例,每個測試實例是一個長度不超過100的英文句子,占一行。
Output
請輸出按照要求改寫后的英文句子。
Sample Input
i like acm
i want to get an accepted
Sample Output
I Like Acm
I Want To Get An Accepted
代碼
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char a[105];
int i,len;
while(gets(a)!='\0')
{
len=strlen(a);
if(a[0]!=' ')
a[0]=a[0]-32;
for(i=1;i<len;i++)
{
if(a[i]!=' '&&a[i-1]==' ')
{
a[i]=a[i]-32;
}
}
puts(a);
}
return 0;
}
思路與總結
改變空格后的字母為大寫,注意第一個
語法知識
gets:
gets能夠接受空格、制表符Tab和回車等;
gets可接受回車鍵之前輸入的所有字符,並用’\0’替代 ‘\n’.回車鍵不會留在輸入緩沖區中
puts:
puts()在輸出字符串時會將’\0’自動轉換成’\n’進行輸出,也就是說,puts方法輸出完字符串后會自動換行。
2027. 統計元音
題目
Problem Description
統計每個元音字母在字符串中出現的次數。
Input
輸入數據首先包括一個整數n,表示測試實例的個數,然后是n行長度不超過100的字符串。
Output
對於每個測試實例輸出5行,格式如下:
a:num1
e:num2
i:num3
o:num4
u:num5
多個測試實例之間由一個空行隔開。
請特別注意:最后一塊輸出后面沒有空行:)
Sample Input
2
aeiou
my name is ignatius
Sample Output
a:1
e:1
i:1
o:1
u:1
a:2
e:1
i:3
o:0
u:1
代碼
# include <stdio.h>
# include <string.h>
int main()
{
int n, len;
char s[150];
while(scanf("%d", &n) != EOF && n != 0)
{
getchar();
while(n--)
{
gets(s);
int a_count = 0;int e_count = 0;int i_count = 0;int o_count = 0;int u_count = 0;
len = strlen(s);
for(int i=0;i < len; i++)
{
if(s[i] == 'a') a_count += 1;
if(s[i] == 'e') e_count += 1;
if(s[i] == 'i') i_count += 1;
if(s[i] == 'o') o_count += 1;
if(s[i] == 'u') u_count += 1;
}
printf("a:%d\n", a_count);
printf("e:%d\n", e_count);
printf("i:%d\n", i_count);
printf("o:%d\n", o_count);
printf("u:%d\n", u_count);
if(n) printf("\n");
}
}
return 0;
}
思路與總結
在做字符類型題目,注意scanf會引入一個'\n'換行符,必要時用getchar()吸收。
2028. Lowest Common Multiple Plus(最小公倍數)
題目
Problem Description
求n個數的最小公倍數。
Input
輸入包含多個測試實例,每個測試實例的開始是一個正整數n,然后是n個正整數。
Output
為每組測試數據輸出它們的最小公倍數,每個測試實例的輸出占一行。你可以假設最后的輸出是一個32位的整數。
Sample Input
2 4 6
3 2 5 7
Sample Output
12
70
代碼
//最小公倍數 = 兩整數 / 最大公約數
#include <stdio.h>
int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}
int lcm(int a,int b)
{
return a*b/gcd(a,b);
}
int main()
{
int a[100],n;
while(scanf("%d",&n)==1)
{
int i,y=1;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
//轉換成兩個比較,比較n-1輪
for(i=0;i<n-1;i++)
{
a[i+1]=lcm(a[i],a[i+1]);
}
printf("%d\n",a[i]);
}
return 0;
}
思路與總結
//最小公倍數 = 兩整數 / 最大公約數
最大公約數(輾轉相除法)--遞歸 重點
int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}
2020. Palindromes _easy version(回文串)
題目
Problem Description
“回文串”是一個正讀和反讀都一樣的字符串,比如“level”或者“noon”等等就是回文串。請寫一個程序判斷讀入的字符串是否是“回文”。
Input
輸入包含多個測試實例,輸入數據的第一行是一個正整數n,表示測試實例的個數,后面緊跟着是n個字符串。
Output
如果一個字符串是回文串,則輸出"yes",否則輸出"no".
Sample Input
4
level
abcde
noon
haha
Sample Output
yes
no
yes
no
代碼
/* HDU2029 Palindromes _easy version */
#include <stdio.h>
#include <string.h>
int main(void)
{
int n, flag, start, end;
char a[4096];
scanf("%d", &n);
while(n--) {
// 讀入字符串
scanf("%s", a);
// 判斷是否為回文
start = 0;
end = strlen(a) - 1;
flag = 1;
while(start < end) {
if(a[end] != a[start]) {
flag = 0;
break;
}
start++;
end--;
}
// 輸出結果
if(flag)
printf("yes\n");
else
printf("no\n");
}
return 0;
}
思路與總結
判斷回文,按照題意來就行
