Ch2. Loop Structure
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的更多相关文章
- Texture tiling and swizzling
Texture tiling and swizzling 原帖地址:http://fgiesen.wordpress.com If you’re working with images in your ...
- libev代码
就是贴上来: ev.c: /* * libev event processing core, watcher management */ /* this big block deduces confi ...
- Represent code in math equations
Introduce The article shows a way to use math equations to represent code's logical. Key ideas logic ...
- Lab 6-4
In this lab, we'll analyze the malware found in the file Lab06-04.exe. Questions and Short Answers W ...
- 课程五(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, ...
- (C/C++学习笔记) 八. 程序控制语句
八. 程序控制语句 ● 基础知识 算法的基本控制结构: 顺序结构(sequential structure), 选择结构(case structure), 循环结构(loop structure) c ...
- PHP学习 流程控制和数组
flow control 流程控制decision structure 判断结构loop structure 循环结构 if(condition){statement1;} if(){}else{} ...
- libev loop_init分析
尼玛 C语言学不好真是桑心呐! 看了libev的代码有一种想死的感觉,但是还是要硬着头皮看下去,一定看完! /* initialise a loop structure, must be zero-i ...
- [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 ...
随机推荐
- Object类型知识总结,你掌握了多少?
Object类型 ECMAScript中的对象其实就是一组数据和功能的集合.对象可以通过执行new操作符后跟要创建的对象类型的名称来创建.而创建Object类型的实例并为其添加属性和(或)方 ...
- js中将yyyy-MM-dd格式的日期转换
1.转换为yyyy年MM月dd日 var str = "2017-02-16"; var reg =/(\d{4})\-(\d{2})\-(\d{2})/; var date = ...
- xLua中导出Dotween
前言 在xlua的lua脚本中使用dotween,官方的文档中有提到可以导出,但未介绍详细的步骤,相信比较多的朋友有需要,刚好项目中也在使用xlua和dotween,所以做个笔记. 基础知识: xLu ...
- xmlplus 组件设计系列之四 - 列表
列表组件是极其常用的一类组件,是许多视图组件系统的必须包含的.列表可以做的很简单,只显示简洁的内容.列表也可以做的很复杂,用于展示非常丰富的内容. 组成元素 列表离不开列表项以及包含列表项的容器.下面 ...
- stl_algorithm算法之排序算法
排序算法: 注意:容器中必须重载 op< ,排序中stl标准中要求用小于来进行比较. 7.53.sort //全排序. 7.54.stable_sort //稳定排序.两个或两个以上的相邻且相等 ...
- crontab的两种配置方式
废话不多说,直接上菜了 第一种:在/etc/crontab下设置,指定用户名的 1.vim命令进入/etc/crontab 2.在最后一行加上 59 23 * * * root /root/cat ...
- 不同浏览器的margin值与padding值
IE-7: 有默认外边距margin样式的元素: dd,menu, ol, ul, blockquote, body, dd, dl, form, h1-6, ul 有默认内边距padding样式的元 ...
- 2017TSC世界大脑与科技峰会,多角度深入探讨关于大脑意识
TSC·世界大脑与科技峰会是全世界范围内的集会,多角度深入探讨关于大脑意识体验来源这一根本话题,我们是谁,现实的本质是什么,我们所处宇宙空间的本质为何.该峰会由亚利桑那大学意识研究中心主办. 会议时间 ...
- jenkins 设置自动发送邮件
1.测试邮件发送 进入jenkins系统管理-->系统设置做如下配置 1.设置系统管理员邮件地址,这是一个全局变量,意味着所有的jenkins执行的任务,最后都会通过这个邮件地址发送邮件 ...
- 原生JS-----论数据类型检测
常见的数据类型检测的方法: 一.最为基础的typeof 二.不可不知的instanceof 三.比instanceof更好的constructor 四.检测值或者表达式的结果是否为NaN 五.易用的j ...