Ex

Input some integers and output their min, max and average values (keep three decimal places). It is guaranteed that each input number is a integer less than 1000.

Input contains many datasets, the first line of each dataset is the number of integers n, and the second line contains the n numbers. And the end mark of input is n=0, the program should overlook this dataset. There should be empty row between contiguous datasets.

Sample input:

8

2 8 3 5 1 7 3 6

4

-4 6 10 0

Sample ouput:

Case 1: 1 8 4.375

Case 2: -4 10 3.00

#include<stdio.h>
#define inf 1000000
int main()
{
int x, n, min, max=, s=, kase=;
while(scanf("%d", &n) == && n, min=inf, max=-inf){
int s=;
for(int i=;i<n;i++){
scanf("%d",&x);
s += x;
if( x< min) min = x;
if( x> max) max = x;
}
if(kase) printf("\n");
printf("Case %d: %d %d %.3f\n", ++kase, min, max, (double)s/n);
}
return ;
}

 

2-1 daffodil

Print all the daffodil numbers from 100-999. If the triple digits ABC meets the condition ABC=A*A*A+B*B*B+C*C*C, then it is a daffodil number. For example, 153=1*1*1+5*5*5+3*3*3, so 153is a daffodil number.

#include<stdio.h>
#include<math.h>
int main()
{
int n;
for(n=; n<=; n++)
if (n == pow(n/,)+pow(n/%,)+pow(n%,))
printf("%d\n", n);
else
continue;
return ;
}

2-2 hanxin

Hanxin let soldiers stand three people per line, five people per line and seven people per line, and he only needs to check  the end line to know the total number of people.

 

Input many datasets, and each dataset contains three non-negetive integer a,b,c, which represents the end line number of people (a<3, b<5, c<7), output the minimium  number of people (or report No answer). Given the total population is not less than 10 and no more than 100. Input ends at the end of file.

For example,

input:

2 1 6

2 1 3

output:

Case 1: 41

Case 2: No answer

#include<stdio.h>
int main()
{
int a,b,c, kase=;
scanf("%d%d%d", &a, &b, &c);
bool flag=false;
int n = ;
while( flag == false && n<=){
if((n% == a) && (n% == b) && (n% == c))
{
flag = true;
}
else
n++;
}
if (flag == true)
printf("%d\n", n);
else
printf("No answer\n");
return ;
}

2-3 triangle

Input the positive integer n<=20, output a inverted triangle. 

For example, n=5:

*********

 ******* 

   ****

     *

 

#include<stdio.h>
int main()
{
int n;
scanf("%d", &n);
for(int i=; i<= n; i++){
for(int j=; j<i;j++){
printf(" ");
}
for(int k=; k<*(n-i)-;k++){
printf("*");
}
printf(" \n");
} return ;
}

2-4 susequence

Input two integers n<m<10^6, output 1/n^2+1/(1+n)^2+...+1/m^2, keep 5 decimal places. Input consists of many datasets, the end mark is n=m=0. 

Hint: Mind the trap.

Sample input:

2 4

65536 655360

0 0

Sample output:

Case 1: 0.42361

Case 2: 0.00001

#include<stdio.h>
int main()
{
int n, m =;
double s=;
scanf("%d%d", &n, &m);
for(int i = ; i <= m-n; i++){
s +=1.0/(n+i)/(n+i);
}
printf("%.5f", s);
return ;
}

Trap: When the input number, n or m is very large, their product will be very large and overflow. Thus it n or m should be divided twice. 

2-5 decimal

Input integer a, b,c and output the decimal format of a/b, and rounded to c decimal places. a,b <=10^6, c<=100.

Input

1 6 4

Output

0.1667

#include<stdio.h>
int main()
{
int a=,b=,c=;
int d=a%b; int s;
printf("0.");
for(int i=;i<c;i++)
{
d=d*;
s=d/b;
d=d%b;
printf("%d",s);
}
}

2-6 permutation

Form three three-digit numbers abc, def and ghi from 1,2,3,~, 9. Each figure should only be used once. abc: def: ghi=1:2:3. Output all solutions in order "abc def ghi". One solution each line. Don't think too hard on it.

#include<stdio.h>
int main()
{
int m;
for(m=;m<=;m++)
{
if((m/) != (m/%)&& (m/)!= (m%)&& (m%)!= (m/%)
&& ((m/)*(m/%)* (m%)* (*m/%) *(*m%)*(*m/)
* (*m/) * (*m/%) * (*m%) == ********)
) printf("%d %d %d\n",m,*m,*m);
}
return ;
}

Prob 1

Suppose we need to output 2,4,6,8,...,2n, one figure each line. Alter the following program to achieve this goal.

 

#include<stdio.h>
int main()
{
int n;
scanf("%d", &n);
for(int i=;i<=n;i++)
printf("%d\n",i);
return ;
}

Task 1

Alter line 7 not line 6. 

#include<stdio.h>
int main()
{
int n;
scanf("%d", &n);
for(int i=;i<=n;i++)
printf("%d\n",*i);
return ;
}

Alter line 6 not line 7. 

#include<stdio.h>
int main()
{
int n;
scanf("%d", &n);
for(int i=;i<=*n;i=i+)
printf("%d\n",i);
return ;
}

Prob 1

What's the result of the following program?

#include<stdio.h>
int main()
{
double i;
for(i=; i!= ; i+=0.1)
printf("%.1f\n", i); return ;
}

A lot of numbers...because i is double and 10 is an integer, and double can not be rounded to integer....

Ch2. Loop Structure的更多相关文章

  1. Texture tiling and swizzling

    Texture tiling and swizzling 原帖地址:http://fgiesen.wordpress.com If you’re working with images in your ...

  2. libev代码

    就是贴上来: ev.c: /* * libev event processing core, watcher management */ /* this big block deduces confi ...

  3. Represent code in math equations

    Introduce The article shows a way to use math equations to represent code's logical. Key ideas logic ...

  4. Lab 6-4

    In this lab, we'll analyze the malware found in the file Lab06-04.exe. Questions and Short Answers W ...

  5. 课程五(Sequence Models),第一 周(Recurrent Neural Networks) —— 2.Programming assignments:Dinosaur Island - Character-Level Language Modeling

    Character level language model - Dinosaurus land Welcome to Dinosaurus Island! 65 million years ago, ...

  6. (C/C++学习笔记) 八. 程序控制语句

    八. 程序控制语句 ● 基础知识 算法的基本控制结构: 顺序结构(sequential structure), 选择结构(case structure), 循环结构(loop structure) c ...

  7. PHP学习 流程控制和数组

    flow control 流程控制decision structure 判断结构loop structure 循环结构 if(condition){statement1;} if(){}else{} ...

  8. libev loop_init分析

    尼玛 C语言学不好真是桑心呐! 看了libev的代码有一种想死的感觉,但是还是要硬着头皮看下去,一定看完! /* initialise a loop structure, must be zero-i ...

  9. [Algorithm] 1. A+B Problem

    Description Write a function that add two numbers A and B. Clarification Are a and b both 32-bit int ...

随机推荐

  1. JS函数与BOM

    [函数的声明及调用]1.函数声明的格式:function 函数名(参数1,参数2,....){//函数体return结果;}函数名(参数1的值,参数2的值,....)>>>函数的调用 ...

  2. 关于特殊文件权限:suid、sgid和sticky-bit

    用 ls –l 命令时,能看到三个八进制数字,表示文件的权限.其实文件的权限应该用4个八进制文件来表示,没有显示的那个是第一位,用来设定一些特殊的权限,这个八进制数字的三个位是:SUID.SGID.s ...

  3. 【正常向】CODEVS上分黄金

    白银上分黄金失败=.= 在之前有很认真的写了一波排序,所以排序并不是很怂,还是那个理,现阶段学习的都是比较简单的排序,都是所谓的冒泡排序啊,桶排序这类,至于插排和选择排序,再往后又是什么快拍就很尬了. ...

  4. struts2中struts.xml 放置路径的问题

    在搭建struts2项目时如果在web.xml中不指定struts.xml文件的路径,struts2会默认到/WEB-INF/classes中寻找加载其配置文件的,但我就是想把struts的配置文件放 ...

  5. AspNetCore-MVC实战系列(四)之结尾

    AspNetCore - MVC实战系列目录 . 爱留图网站诞生 . git源码:https://github.com/shenniubuxing3/LovePicture.Web . AspNetC ...

  6. 【2017-05-05】timer控件、三级联动、帐号激活权限设置

    一.Timer控件 Timer实际就是一个线程控件. 属性:Enabled    是否被启用 Interval     多长时间执行一次控件中的代码 事件: Tick     事件中放要执行的代码. ...

  7. python3 selenium 切换窗口的几种方法

    第一种方法: 使用场景: 打开多个窗口,需要定位到新打开的窗口 使用方法: # 获取打开的多个窗口句柄windows = driver.window_handles# 切换到当前最新打开的窗口driv ...

  8. 基本排序算法<一>

    一 选择排序 原理:选择排序很简单,他的步骤如下: 从左至右遍历,找到最小(大)的元素,然后与第一个元素交换. 从剩余未排序元素中继续寻找最小(大)元素,然后与第二个元素进行交换. 以此类推,直到所有 ...

  9. Java 7 Fork/Join 框架

    在 Java7引入的诸多新特性中,Fork/Join 框架无疑是重要的一项.JSR166旨在标准化一个实质上可扩展的框架,以将并行计算的通用工具类组织成一个类似java.util中Collection ...

  10. ConcurrentHashMap源码解析

    转自:http://www.iteye.com/topic/344876 ConcurrentHashMap是Java 5中支持高并发.高吞吐量的线程安全HashMap实现. 实现原理 锁分离 (Lo ...