1. Text Reverse2. Rightmost Digit
3. Decimal System
4. AC Me
5. FatMouse's Trade
1. Text Reverse
#include
#include
char str[1002];
int main(){
int n;
scanf("%d",&n);
getchar();
while(n--){
fgets(str,1002,stdin);
int len = strlen(str);
int a = 0; //left border
int b = 0; //right border
int i = 0; //position counter
while(a < len){
i = a;
while(str[i]!=' '&& str[i]!='\n'){
i++;
}
b = i;
while(i-- && i >= a){
printf("%c",str[i]);
}
printf("%c",str[b]);
a = ++b;
}
}
return 0;
}
2.
Rightmost Digit#include
void my_pow(int a, int b){
long long unsigned result = 1;
long long unsigned tmp = 1;
while(b){
tmp *= a;
tmp %= 10;
if( b&1 ){
result *= tmp;
result %= 10;
}
b>>=1;
a = tmp;
}
printf("%d\n",result%10);
}
int main(){
int n;
scanf("%d",&n);
while(n--){
int a;
scanf("%d",&a);
if(a%4){
my_pow(a,a%4);
}else{
my_pow(a,4);
}
}
return 0;
}
3.Decimal System
#include
#include
#include
using namespace std;
int main(){
int N;
char a[1000];
int i,j;
int val;
char ch;
long long unsigned sum;
while(scanf("%d",&N)!=EOF){
getchar();
sum = 0;
while(N--){
i = 0;
while((ch = getchar())!='('){
a[i++] = ch;
}
j = i;
scanf("%d",&val);
getchar(); //')'
getchar(); //'enter'
i = 0;
while(j--){
sum += (a[i++]-'0')*(long long unsigned)pow((double)val,(double)j);
}
}
printf("%d\n",sum);
getchar();
}
return 0;
}
4.AC Me
#include
#include
#define MAXLINE 100001
#define LIST 26
int count[LIST];
char line[MAXLINE];
int main(){
int i,j;
int len;
while(fgets(line,sizeof(line),stdin)){
len = strlen(line);
i = 0 ;
j = 0 ;
/* Refresh Counter[] */
memset(count,0,sizeof(count));
/* Counting */
while(i
j = line[i++]-'a';
if(j<27 && j>=0){
count[j]++;
}
}
/* Output */
i = 0;
while(i
printf("%c:%d\n",i+'a',count[i]);
i++;
}
printf("\n");
/* Empty line[] */
memset(line,-1,sizeof(line));
}//while:fgets
return 0;
}
5.FatMouse's Trade
浮点乘除运算最好在最后展开,否则精度问题会导致一直WA。
#include
#include
#include
typedef struct {
int bean;
int food;
double val; // javabean/food
}box;
void test(box* pData, int n)
{
int i;
for(i=0; i printf("bean:%d; val: %lf ",(*(pData+i)).bean,(*(pData+i)).val);
}
printf("\n");
}
int cmp(const void *a, const void *b)
{
return ((((*(box *)b).val-(*(box *)a).val)>0.0000) ? 1 : -1);
}
int main(){
int M,N;
double sum = 0;
box * pData;
while(scanf("%d %d",&M,&N) && (M>=0) && (N>=0))
{
pData = (box *)calloc(N,sizeof(box));
assert(pData != NULL);
int JavaBean,Food;
int i = 0;
while(i scanf("%d %d",&JavaBean,&Food);
(*(pData+i)).bean= JavaBean;
(*(pData+i)).food= Food;
(*(pData+i)).val = (double)JavaBean/Food;
i++;
}
qsort(pData,N,sizeof(*pData),cmp);
// test(pData,N);
sum = 0;
i = 0 ;
while( M && i {
if(M>(*(pData+i)).food)
{
sum += (*(pData+i)).bean;
M -= (*(pData+i)).food;
//printf("M:%d; javabean=%lf\n",M,(*(pData+i)).food * (*(pData+i)).val);
i++;
}else{
sum += M*(*(pData+i)).val;
M = 0;
// printf("M:%d\n",M);
}
// printf("i=%d\n",i);
}
printf("%.3lf\n",sum);
free(pData);
}
}
阅读(1240) | 评论(0) | 转发(0) |