這一周加入了院足球隊(duì)项钮,好久都沒(méi)有感受到在賽場(chǎng)上奔騰的感覺(jué)了。不過(guò)正好趕上清明丈秩,因此也算有充足的時(shí)間來(lái)碼代碼盯捌。這個(gè)月一共碼了20題,排名終于沖進(jìn)了四萬(wàn)名蘑秽。
ID 2015 偶數(shù)求和
#include<stdio.h>
int main()
{
int n, m, k;
int i,j;
int a[101];
a[0] = 0;
int sum, sum1;
int count;
while (scanf("%d", &n) != EOF)
{
for (i = 1; i <= n; i++)
{
a[i] = 2 * i;
}
scanf("%d", &m);
count = 0;
for (i = 1; i <= n; i = i + m)
{
sum = 0;
for (j = 0; j < m; j++)
{
sum = sum + a[i + j];
}
sum = sum / m;
printf("%d", sum);
if ((i + j - 1) < n)
{
printf(" ");
}
if ((i + j - 1) == n)
{
printf("\n");
}
count++;
if ((n - m * count) < m) break;
}
sum1 = 0;
if ((n - m * count) < m && (n - m * count) > 0)
{
k = n - m * count;
for (i = 0; i < k; i++)
{
sum1 = sum1 + a[m*count + i + 1];
}
sum1 = sum1 / k;
printf("%d\n", sum1);
}
}
}
ID 2016 數(shù)據(jù)的交換輸出
#include<stdio.h>
#define N 100
int main() {
int arr[N];
int i,x,n;
while(scanf("%d",&n),n!=0) {
for(i=0; i<n; i++)
scanf("%d",&arr[i]);
int k=0,j=arr[0];
for(i=1; i<n; i++) {
if(j>arr[i]) {
j=arr[i];
k=i;
}
}
x=arr[0];
arr[0]=j;
arr[k]=x;
for(i=0; i<n; i++) {
if(i==n-1)
printf("%d",arr[i]);
else
printf("%d ",arr[i]);
}
printf("\n");
}
return 0;
}
ID 2017 字符串統(tǒng)計(jì)
#include<stdio.h>
#include<string.h>
main()
{
int n,num;
char str;
scanf("%d",&n);
getchar();
while(n--)
{ num=0;
while(scanf("%c",&str),str!='\n')//
{
if(str>='0'&&str<='9') num++;
}
printf("%d\n",num);
}
return 0;
}
ID 2019 數(shù)列有序
#include<stdio.h>
int main() {
int i,j,k,m,n;
while(scanf("%d %d",&n,&m),n!=0||m!=0) {
int a[120];
for(i=0; i<n; i++)
scanf("%d",&a[i]);
a[n]=m;
for(j=n-1; j>=0; j--) {
if(a[j]>a[j+1]) {
k=a[j];
a[j]=a[j+1];
a[j+1]=k;
} else
break;
}
for(i=0; i<=n; i++) {
if(i==0)
printf("%d",a[i]);
else
printf(" %d",a[i]);
}
printf("\n");
}
return 0;
}
這一題我一開始采取的方法是找到它的位置饺著,然后插入,并在此之前將它位置之后的所有元素向后挪一個(gè)來(lái)給它空出位置肠牲。跟現(xiàn)在的這個(gè)方法相比幼衰,我那樣處理明顯是麻煩了。而且我用前者也確實(shí)超時(shí)了缀雳。
ID 2021 發(fā)工資嘍
#include<stdio.h>
int main()
{
int n;
int a[101], b[101];
int i;
int hundreds, tens, nums;
int money;
int sum;
while (scanf("%d", &n), n != 0)
{
sum = 0;
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
for (i = 0; i < n; i++)
{
money = 0;
b[i] = 0;
hundreds = a[i] / 100;
tens = (a[i] % 100) / 10;
nums = (a[i] % 100) % 10;
if (hundreds > 0)
{
money = money + hundreds;
}
if (tens >= 5)
{
money++;
tens = tens - 5;
money = money + tens;
}
else
{
money = money + tens;
}
if (nums >= 5)
{
money++;
nums = nums - 5;
money = money + nums / 2;
money = money + nums % 2;
}
else
{
money = money + nums / 2;
money = money + nums % 2;
}
b[i] = money;
}
for (i = 0; i < n; i++)
{
sum = sum + b[i];
}
printf("%d\n", sum);
}
return 0;
}
我這題的思路是這樣的渡嚣,將每個(gè)老師的工資看成一個(gè)個(gè)體,儲(chǔ)存在一個(gè)數(shù)組中肥印。然后將每個(gè)老師的工資分成三部分识椰,分別是百位以前的數(shù)字(因?yàn)槿嗣駧抛畲竺骖~100),十位上的數(shù)字竖独,個(gè)位上的數(shù)字裤唠。然后再做處理,最后相加莹痢。
ID 2022 海選女主角
#include <stdio.h>
#include <math.h>
int main() {
int n, m, i, j, a, b;
double d, max;
while(~scanf("%d%d", &m, &n)) {
max = 0;
a = 0;
b = 0;
for(i=0; i<m; i++)
for(j=0; j<n; j++) {
scanf("%lf", &d);
if(fabs(d) > fabs(max)) {
a = i;
b = j;
max = d;
}
}
printf("%d %d %.0lf\n", a+1, b+1, max);
}
return 0;
}
這題用到了一個(gè)求絕對(duì)值的函數(shù)种蘸,并且注意分?jǐn)?shù)使用浮點(diǎn)型數(shù)據(jù)表示。
ID 2023 求平均成績(jī)
#include<stdio.h>
int main() {
int n,m;
double a1[50][5],av1[50],av2[5],k;
while(scanf("%d %d",&n,&m)==2) {
int x,y=0;
for(int pe=0; pe<n; pe++) {
for(int su=0; su<m; su++)
scanf("%lf",&a1[pe][su]);
}
for(int i=0; i<n; i++) {
k=0;
for(int j=0; j<m; j++)
k+=a1[i][j];
av1[i]=k/m;
}
for(int i=0; i<m; i++) {
k=0;
for(int j=0; j<n; j++)
k+=a1[j][i];
av2[i]=k/n;
}
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
if(a1[i][j]>=av2[j])
x=1;
else {
x=0;
break;
}
}
if(x==1)
y++;
}
for(int i=0; i<n; i++) {
if(i==n-1)
printf("%.2lf\n",av1[i]);
else
printf("%.2lf ",av1[i]);
}
for(int j=0; j<m; j++) {
if(j==m-1)
printf("%.2lf\n",av2[j]);
else
printf("%.2lf ",av2[j]);
}
printf("%d\n",y);
printf("\n");
}
return 0;
}
ID 2024 合法標(biāo)識(shí)符
#include<stdio.h>
#include<string.h>
int main()
{
char a[50];
int t;
scanf("%d\n",&t);
while(t--)
{
gets(a);
if(!isalpha(a[0])&&a[0]!='_')
{
printf("no\n");
continue;
}
int flag=1;
int l=strlen(a);
for(int i=1;i<l;i++)
{
if(a[i]!='_'&&!isalnum(a[i]))
{
flag=0;
break;
}
else
{
flag=1;
}
}
if(flag==1)
{
printf("yes\n");
}
else if(flag==0)
{
printf("no\n");
}
}
return 0;
}
ID 2026 首字母變大寫
#include<stdio.h>
#include<string.h>
int main()
{
char a[120];
int k, i;
while (gets_s(a,120) != NULL)
{
k = strlen(a);
a[0] = a[0] - 32;
for (i = 0; i < k; i++)
{
if (a[i] == ' ')
{
a[i + 1] = a[i + 1] - 32;
}
}
puts(a);
}
return 0;
}
get_s()函數(shù)是新標(biāo)準(zhǔn)里面的一個(gè)函數(shù)竞膳,它避免了get()函數(shù)數(shù)據(jù)溢出導(dǎo)致覆蓋堆棧中原有數(shù)據(jù)的問(wèn)題航瞭。
ID 2027 統(tǒng)計(jì)元音
#include<stdio.h>
#include<string.h>
int main()
{
char a[120];
int n, i, k;
int count1, count2, count3, count4, count5;
scanf("%d", &n);
getchar();
while (n--)
{
count1 = count2 = count3 = count4 = count5 = 0;
gets_s(a, 120);
k = strlen(a);
for (i = 0; i < k; i++)
{
if (a[i] == 'a')
{
count1++;
}
if (a[i] == 'e')
{
count2++;
}
if (a[i] == 'i')
{
count3++;
}
if (a[i] == 'o')
{
count4++;
}
if (a[i] == 'u')
{
count5++;
}
}
printf("a:%d\ne:%d\ni:%d\no:%d\nu:%d\n", count1, count2, count3, count4, count5);
if (n)
{
printf("\n");
}
}
return 0;
}
我做這題的時(shí)候呢,提交了很多次都無(wú)法AC坦辟,這里我得到了一些寶貴的教訓(xùn)刊侯,分享給大家。首先锉走,WA(wrong answer)和PE(Presentation Error)是完全不同的兩個(gè)意思滨彻。不少人出現(xiàn)了WA就在格式上看了一遍又一遍,我告訴大家挪蹭,WA基本不是格式錯(cuò)誤亭饵,PE一定是格式錯(cuò)誤。這題我犯了兩個(gè)低級(jí)錯(cuò)誤梁厉,第一個(gè)我把getchar()放在了循環(huán)里面辜羊,第二個(gè)他說(shuō)最后一個(gè)輸出后沒(méi)有空行,并不意味著不用換行,只是說(shuō)不用換行后再空一行八秃。
ID 2028 求最小公倍數(shù)
#include<stdio.h>
#include<string.h>
int main()
{
long long n;
long long i;
long long a[10001];
long long num1, num2;
long long remainder;
long long temp;
long long lcm;
long long x, y;
while (scanf("%d", &n) != EOF)
{
num1 = num2 = 0;
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
lcm = a[0];
for (i = 1; i < n; i++)
{
num1 = lcm;
num2 = a[i];
if (num1 < num2)
{
temp = num2;
num2 = num1;
num1 = temp;
}
x = num1;
y = num2;
while (y != 0)
{
temp = x % y;
x = y;
y = temp;
}
lcm = num1 * num2 / x;
}
printf("%d\n", lcm);
}
return 0;
}
我求多個(gè)數(shù)的最小公倍數(shù)的方法是碱妆,先求兩個(gè)數(shù)的最小公倍數(shù),然后將這個(gè)求得的數(shù)再與第三個(gè)數(shù)求最小公倍數(shù)昔驱。注意int類型數(shù)據(jù)不夠存疹尾。
ID 2030 漢字統(tǒng)計(jì)
#include<stdio.h>
#include<string.h>
int main()
{
int n;
scanf("%d", &n);
getchar();
while (n--)
{
char a[110000];
gets_s(a,110000);
int l, num = 0;
l = strlen(a);
for (int i = 0; i<l; i++)
{
if (a[i]<' ')
num++;
}
printf("%d\n", num / 2);
}
return 0;
}
不少人拿到這題不知道該怎么做,其實(shí)很簡(jiǎn)單骤肛。漢字在unicode編碼中首位都是1航棱,而在數(shù)據(jù)存儲(chǔ)中首位是1的數(shù)字表示是負(fù)數(shù),又因?yàn)闈h字占兩個(gè)字符萌衬,所以要除以二饮醇。
ID 2032 楊輝三角
#include <stdio.h>
int main() {
int s = 1, n;
int i, j;
while(scanf("%d", &n)!=EOF) {
printf("1\n");
for (i = 2; i <= n; s = 1, i++) {
printf("1 ");
for (j = 1; j <= i - 2; j++)
printf("%d ", (s = (i - j) * s / j));
printf("1\n");
}
printf("\n");
}
return 0;
}
用數(shù)組的方式來(lái)打印楊輝三角大家肯定都會(huì),可是我們也可以通過(guò)發(fā)現(xiàn)它們數(shù)據(jù)間的關(guān)系秕豫,找出遞推公式直接輸出朴艰,這樣是不是更符合ACM的精神呢?
ID 2033 時(shí)間相加
#include<stdio.h>
#include<string.h>
int main()
{
int n;
int a[100][6];
int i;
int hour, minute, second;
while (scanf("%d", &n) != EOF)
{
for (i = 0; i < n; i++)
{
scanf("%d %d %d %d %d %d", &a[i][0], &a[i][1], &a[i][2], &a[i][3], &a[i][4], &a[i][5]);
}
for (i = 0; i < n; i++)
{
hour = minute = second = 0;
hour = a[i][0] + a[i][3];
if (a[i][1] + a[i][4] > 59)
{
hour = hour + 1;
minute = a[i][1] + a[i][4] - 60;
}
else
{
minute = a[i][1] + a[i][4];
}
if (a[i][2] + a[i][5] > 59)
{
minute = minute + 1;
second = a[i][2] + a[i][5] - 60;
}
else
{
second = a[i][2] + a[i][5];
}
printf("%d %d %d\n", hour, minute, second);
}
}
return 0;
}
so easy!!!
ID 2035 求數(shù)后三位
#include<stdio.h>
int main()
{
int m, n;
int temp;
int i;
while (scanf("%d %d", &m, &n), m != 0, n != 0)
{
temp = 1;
for (i = 0; i < n; i++)
{
temp = (temp * m) % 1000;
}
printf("%d\n", temp);
}
return 0;
}
這一題思路很簡(jiǎn)單混移,可是如果直接相乘最后得出結(jié)果祠墅,數(shù)據(jù)太大,會(huì)輸出超時(shí)歌径。這里需要用到一個(gè)數(shù)學(xué)技巧毁嗦,就是每次得到的數(shù)都對(duì)一千除余,只要保留了這個(gè)數(shù)的后三位就能保證最后得到的數(shù)的后三位是正確的回铛。
ID 2039 三角形
#include<stdio.h>
int main()
{
int n;
int i, j;
float a[3];
float max;
int index;
float temp;
scanf("%d", &n);
for (i = 0; i < n; i++)
{
max = 0;
for (j = 0; j < 3; j++)
{
scanf("%f", &a[j]);
if (a[j] > max)
{
max = a[j];
index = j;
}
}
temp = a[0];
a[0] = a[index];
a[index] = temp;
if (a[1] + a[2] > a[0])
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
return 0;
}
輸入要注意是浮點(diǎn)型才能AC!
ID 2040 親和數(shù)
#include<stdio.h>
#include<math.h>
int main()
{
int n;
long num1, num2;
int i;
long sum1, sum2;
while (scanf("%d", &n) != EOF)
{
while (n--)
{
sum1 = sum2 = 0;
scanf("%d %d", &num1, &num2);
for (i = 2; i <= sqrt(num1); i++)
{
if (num1%i == 0)
{
sum1 = sum1 + i + num1/i;
}
}
sum1++;
for (i = 2; i <= sqrt(num2); i++)
{
if (num2%i == 0)
{
sum2 = sum2 + i + num2 / i;
}
}
sum2++;
if ((sum1 == num2) && (sum2 == num1))
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
}
return 0;
}
ID 2041 超級(jí)樓梯
#include<stdio.h>
#include<math.h>
int main()
{
int n,m;
int a[50];
int i;
while (scanf("%d", &n) != EOF)
{
while (n--)
{
a[0] = 1;
a[1] = 1;
scanf("%d", &m);
for (i = 2; i <= 40; i++)
{
a[i] = a[i - 1] + a[i - 2];
}
printf("%d\n", a[m-1]);
}
}
return 0;
}
這題看著沒(méi)頭緒狗准,其實(shí)它背后的數(shù)學(xué)知識(shí)是斐波那契數(shù)列。知道怎么做了吧茵肃,這個(gè)數(shù)列可是ACM最喜歡考的知識(shí)點(diǎn)了腔长。給大家科普一下,知道這個(gè)數(shù)列為什么這么出名嘛验残?因?yàn)樗倪f推公式的一個(gè)比值是黃金分割點(diǎn)捞附。
ID 2042 過(guò)路費(fèi)
#include<stdio.h>
#include<math.h>
int main()
{
int n,m;
int i;
int initial_value, final_value;
while (scanf("%d", &n) != EOF)
{
while (n--)
{
scanf("%d", &m);
initial_value = 3;
for (i = 0; i < m; i++)
{
final_value = 2 * (initial_value - 1);
initial_value = final_value;
}
printf("%d\n", final_value);
}
}
return 0;
}
水題。
ID 2045 著色問(wèn)題
#include<stdio.h>
int main()
{
int n;
int i;
__int64 coefficient1, coefficient2;
__int64 count;
__int64 temp;
while (scanf("%d", &n)!=EOF)
{
if (n == 1)
{
printf("3\n");
}
else if (n == 2||n == 3)
{
printf("6\n");
}
else
{
coefficient1 = coefficient2 = 2;
count = 0;
for (i = 4; i <= n; i++)
{
count = (coefficient1 * 2 + coefficient2) * 3;
temp = coefficient1;
coefficient1 = coefficient2;
coefficient2 = 2 * temp + coefficient2;
}
printf("%I64d\n", count);
}
}
return 0;
}
這題需要找到背后的一個(gè)數(shù)學(xué)規(guī)律您没。我實(shí)在不知道如何形容鸟召,給大家看看我的做題筆記吧。氨鹏。欧募。也是夠蠢的,寫了這么多才發(fā)現(xiàn)規(guī)律喻犁。