Codeforces 1A&1B
1A.
Theatre Square in the capital city of Berland has a rectangular shape with the size n × m meters. On the occasion of the city's anniversary, a decision was taken to pave the Square with square granite flagstones. Each flagstone is of the size a × a.
What is the least number of flagstones needed to pave the Square? It's allowed to cover the surface larger than the Theatre Square, but the Square has to be covered. It's not allowed to break the flagstones. The sides of flagstones should be parallel to the sides of the Square.
Input
The input contains three positive integer numbers in the first line: n, m and a (1 ≤ n, m, a ≤ 109).
Output
Write the needed number of flagstones.
Sample test(s)
input
6 6 4
output
4
分析:感觉只是一道简单的数学题而已,但是也有一发WA。一看居然是手误,很不应该。同时采取int64来防止数据溢出。
代码如下:
#include <stdio.h> int main() { long long n,m,a,b,c; scanf("%I64d%I64d%I64d",&n,&m,&a); if(n%a) b=n/a+1; else b=n/a; if(m%a) c=m/a+1; else c=m/a; printf("%I64d",b*c); return 0; }
1B.
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.
The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23.
Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.
Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
Input
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106.
Output
Write n lines, each line should contain a cell coordinates in the other numeration system.
Sample test(s)
input
2
R23C55
BC23
output
BC23
R23C55
分析:其实这道题是10进制与26进制间的转换,所以明白这一点后就需要处理细节就好,采取字符串读取数据。额,这道题写完后再看十分简单,但是我也快用了一个小时,而且第一交也是WA,才发现flag需要每一次都重新置为0.
#include <stdio.h> #include <string.h> char s[100000]; int main() { int n,flag=0,i; //flag等于1为RC类型 scanf("%d",&n); while(n--) { int len; scanf("%s",s); len=strlen(s); if(s[0]=='R'&&s[1]>='0'&&s[1]<='9') //可能是RC型 { i=2; while(s[i]) { if(s[i]!='C') i++; else { flag=1; //确定是RC类型 break; } } } if(flag) //RC类型转化 { char c[100],r[100]; int r_n=0,c_n=0; int num=0,j; int lenc; for(j=1;j<=i-1;j++) r[r_n++]=s[j]; r[r_n]='\0'; for(j=i+1;j<len;j++) { num=10*num+(s[j]-'0'); } while(num){ if(num%26==0){ c[c_n]='Z'; num--; } else{ c[c_n]=num%26+'A'-1; } num/=26; c_n++; } //完成转化。 c[c_n]='\0'; lenc=strlen(c); for(j=lenc-1;j>=0;j--) printf("%c",c[j]); printf("%s\n",r); flag=0; } else //不是RC模式 { char r[100]; int num=0,r_n=0; i=0; while(s[i]>='A'&&s[i]<='Z'){ num=num*26+s[i]-'A'+1; i++; } while(s[i]!='\0'){ r[r_n++]=s[i]; i++; } r[r_n]='\0'; printf("R%sC%d\n",r,num); } } return 0; }