1 A 阵列
# include <stdio.h>
int main ( )
{
int i, j;
for ( i= 1 ; i<= 3 ; i++ )
{
if ( i% 2 == 0 ) printf ( "A " ) ;
for ( j= 0 ; j< 2 ; j++ )
{
printf ( " " ) ;
}
printf ( "A" ) ;
printf ( "\n" ) ;
}
}
2 倒三角
# include <stdio.h>
int main ( )
{
int i, j;
for ( i= 0 ; i< 4 ; i++ )
{
for ( j= 0 ; j< i- 1 ; j++ )
{
printf ( " " ) ;
}
for ( j= 4 - i; j> 0 ; j-- ) {
if ( i == 0 && j== 4 )
printf ( "*" ) ;
else printf ( " *" ) ;
}
printf ( "\n" ) ;
}
return 0 ;
}
3 星期几
# include <stdio.h>
int main ( )
{
int today;
scanf ( "%d" , & today) ;
if ( today >= 6 ) printf ( "%d" , ( today+ 2 ) % 7 ) ;
else printf ( "%d" , today+ 2 ) ;
return 0 ;
}
4 时间差
# include <stdio.h>
int main ( )
{
int cur, amin, cn;
scanf ( "%d%d" , & cur, & amin) ;
cur = cur/ 100 * 60 + cur% 100 ;
cn = cur + amin;
cn = cn/ 60 * 100 + cn% 60 ;
if ( cn< 10 ) printf ( "00%d" , cn) ;
else if ( cn< 60 ) printf ( "0%d" , cn) ;
else printf ( "%d" , cn) ;
return 0 ;
}
5 BCD
# include <stdio.h>
int main ( )
{
int d, h;
scanf ( "%d" , & d) ;
printf ( "%d" , d/ 16 * 10 + d% 16 ) ;
return 0 ;
}
6 出租车cost
# include <stdio.h>
int main ( )
{
float km;
float cost;
int min;
scanf ( "%f%d" , & km, & min) ;
if ( km <= 3 ) cost = 10 ;
else if ( km <= 10 ) cost = 10 + ( km- 3 ) * 2 ;
else cost = 10 + 14 + ( km- 10 ) * 3 ;
cost = cost + min/ 5 * 2 ;
if ( cost* 10 >= ( int ) cost* 10 + 5 ) cost = ( int ) cost + 1 ;
printf ( "%d" , ( int ) cost) ;
return 0 ;
}
7 计算天数
# include <stdio.h>
int main ( )
{
int day, moth, year, febur;
scanf ( "%d/%d/%d" , & year, & moth, & day) ;
if ( year% 400 == 0 || ( year% 4 == 0 && year% 100 != 0 ) )
{
febur = 29 ;
}
else febur = 28 ;
if ( moth> 1 )
day+= 31 ;
if ( moth> 2 )
day += febur;
if ( moth> 3 )
day += 31 ;
if ( moth> 4 )
day += 30 ;
if ( moth> 5 )
day += 31 ;
if ( moth> 6 ) day+= 30 ;
if ( moth> 7 ) day+= 31 ;
if ( moth> 8 ) day+= 31 ;
if ( moth> 9 ) day+= 30 ;
if ( moth> 10 ) day+= 31 ;
if ( moth> 11 ) day+= 30 ;
if ( moth> 12 ) day+= 31 ;
printf ( "%d" , day) ;
return 0 ;
}
8 无优先级 计算器
# include <stdio.h>
int main ( )
{
int number;
int sum = 0 ;
char operator;
scanf ( "%d" , & sum) ;
while ( 1 )
{
scanf ( "%c%d" , & operator, & number) ;
if ( operator == '+' ) {
sum += number;
}
else if ( operator == '-' ) {
sum -= number;
}
else if ( operator == '*' ) {
sum *= number;
}
else if ( operator == '/' )
{
if ( number == 0 )
{
printf ( "ERROR\n" ) ;
break ;
}
sum /= number;
}
else if ( operator == '=' )
{
printf ( "%d\n" , sum) ;
break ;
}
else
{
printf ( "ERROR\n" ) ;
break ;
}
}
}
9 求和
1奇数列
# include <stdio.h>
int main ( )
{
int input;
int sum = 0 ;
while ( 1 )
{
scanf ( "%d" , & input) ;
if ( input <= 0 ) break ;
if ( input% 2 != 0 )
{
sum += input;
}
}
printf ( "%d" , sum) ;
return 0 ;
}
2 交错列
# include <stdio.h>
int main ( )
{
int mole = 1 ;
int deno = 1 ;
double sum = 0 ;
int sign = 1 ;
int N;
scanf ( "%d" , & N) ;
for ( int i = 0 ; i< N; i++ )
{
sum += 1.0 * mole/ deno* sign;
sign*= - 1 ;
mole += 1 ;
deno += 2 ;
}
printf ( "%.3f\n" , sum) ;
return 0 ;
}
3 统计素数并求和
# include <stdio.h>
int main ( ) {
int N, M;
scanf ( "%d%d" , & N, & M) ;
int cnn= 0 ;
int sum = 0 ;
for ( ; N<= M; N++ )
{
int i;
for ( i= 2 ; i< N; i++ )
{
if ( N% i== 0 ) break ;
}
if ( i == N)
{
cnn++ ;
sum+= N;
}
}
printf ( "%d %d" , cnn, sum) ;
}
10 斐波那契 兔子
# include <stdio.h>
int main ( )
{
int N;
int cn = 2 ;
scanf ( "%d" , & N) ;
if ( N== 1 )
printf ( "1" ) ;
else if ( N== 2 )
printf ( "3" ) ;
else
{
int fn_1= 1 , fn_2= 1 , fn;
while ( fn< N)
{
fn = fn_1+ fn_2;
fn_2 = fn_1;
fn_1 = fn;
cn++ ;
}
printf ( "%d" , cn) ;
}
return 0 ;
}
11 二分法 求根
# include <stdio.h>
# include <math.h>
double c, d, e, f;
double fomula ( double x)
{
return ( c* pow ( x, 3 ) + d* pow ( x, 2 ) + e* x+ f) ;
}
int main ( )
{
double a, b;
scanf ( "%lf%lf%lf%lf" , & c, & d, & e, & f) ;
- scanf ( "%lf%lf" , & a, & b) ;
double middle = ( a+ b) / 2 ;
while ( 1 )
{
if ( fabs ( fomula ( middle) ) < 1e-7 )
{
printf ( "%.2f" , middle) ;
break ;
}
else if ( fomula ( middle) * fomula ( a) > 0 )
{
a= middle;
middle = ( a+ b) / 2 ;
}
else {
b= middle;
middle = ( a+ b) / 2 ;
}
}
return 0 ;
}
13 数字陷阱
# include <stdio.h>
int main ( )
{
int n, n0;
scanf ( "%d" , & n0) ;
int i= 0 ;
while ( 1 )
{
int number = n0;
n = 0 ;
while ( number> 0 )
{
n+= number% 10 ;
number/= 10 ;
}
i++ ;
n = n* 3 + 1 ;
printf ( "%d:%d\n" , i, n) ;
if ( n== n0) break ;
n0 = n;
}
}
14 猴子吃桃
# include <stdio.h>
int main ( )
{
int N;
int peach = 1 ;
scanf ( "%d" , & N) ;
for ( ; N> 1 ; N-- )
{
peach = 2 * ( peach + 1 ) ;
}
printf ( "%d" , peach) ;
return 0 ;
}