# 初入C++的世界
最一開始會想接觸程式設計是因為從小耳濡目染,覺得作為一位資訊工程師會有很好的成就,能有很高的收入遍對於這門課程很有興趣。
## 用while迴圈打出數列
```c=
#include<iostream>
using namespace std;
int main()
{
int i;
i = 1;
while( i<=n )
{
cout << i << " ";
i = i+1;
}
return 0;
}
```
___
## 用while迴圈找出因數
```c=
#include<iostream>
using namespace std;
int main()
{
int n, i;
cin >> n;
i = 1;
while( i <= n )
{
if( n%i == 0 )
{
cout << i << " ";
}
i = i+1;
}
cout << endl;
return 0;
}
```
---
## 找總共有幾位數
```c=
#include<iostream>
using namespace std;
int main()
{
int n;
int ans;
while( cin >> n )
{
ans = 0;
while( n>0 )
{
n = n / 10;
ans = ans+1;
}
cout << ans << endl;
}
return 0;
}
```
## 解題目 - Feynman
```c=
#include<iostream>
using namespace std;
int main()
{
int N;
int i;
int ans;
while( cin >> N )
{
if( N == 0 )
break;
ans = 0;
i = 0;
while( i < N )
{
ans = ans + (N-i)*(N-i);
i = i+1;
}
cout << ans << endl;
}
return 0;
}
```
---
## 轉換時間
```c=
#include<iostream>
using namespace std;
int main()
{
int N;
int h, m, s;
cin >> N;
h = N/3600;
N = N%3600;
m = N/60;
N = N%60;
s = N;
cout << h << " hr " << m << " min " << s << " sec" << endl;
return 0;
}
```
---
## 如果在使用 printf() 時要指定整數、浮點數、字元等進行顯示,則要配合格式指定字(format specifier),以下列出幾個可用的格式指定碼:
### %c 以字元方式輸出
### %d 10 進位整數輸出
### %o 以 8 進位整數方式輸出
### %u 無號整數輸出
### %x, %X 將整數以 16 進位方式輸出
### %f 浮點數輸出
### %e, %E 使用科學記號顯示浮點數
### %g, %G 浮點數輸出,取 %f 或 %e(%f 或 %E),看### h3#示精簡
### %% 顯示 %
### %s 字串輸出
### %lu long unsigned 型態的整數
### %p 指標型態
---
## 您可以在輸出浮點數時指定精度,例如若為浮點數
```c=
printf("example:%.2f\n", 19.234);
```
### .2 指定小數點後取兩位,執行結果會輸出
```c=
example:19.23
```
### 若在 % 之後指定負號,例如 %-6.2f,則表示靠左對齊,沒有指定則靠右對齊
```c=
#include <stdio.h>
int main(void) {
printf("example:%6.2f\n", 19.234);
printf("example:%-6.2f\n", 19.234);
return 0;
}
```
### 若事先無法決定字元寬度,則可以使用 *(常用)
```c=
#include <stdio.h>
int main(void) {
printf("%*d\n", 1, 1);
printf("%*d\n", 2, 1);
printf("%*d\n", 3, 1);
return 0;
}
```
### 如果打算取得使用者的輸入,則可以使用標準輸入的 scanf() 函式,並搭配格式指定字與 & 取址運算子指定給變數,例如:
### scanf類似int 用於宣告
```c=
#include <stdio.h>
int main(void) {
int input;
printf("請輸入數字:");
scanf("%d", &input);
printf("您輸入的數字:%d\n", input);
return 0;
}
```
### scanf() 在接受輸入時,可以接受多個值,也可以指定輸入的格式
```c=
#include <stdio.h>
int main(void) {
int number1, number2;
printf("請輸入兩個數字,中間使用空白區隔):");
scanf("%d %d", &number1, &number2);
printf("您輸入的數字:%d %d\n", number1, number2);
printf("請再輸入兩個數字,中間使用-號區隔):");
scanf("%d-%d", &number1, &number2);
printf("您輸入的數字:%d-%d\n", number1, number2);
return 0;
```
### 用for迴圈做出長方形
```c=
#include<iostream>
using namespace std;
int main()
{
int X, Y;
int i, j;
cin >> X >> Y;
for( i=1 ; i<=Y ; i=i+1 )
{
for( j=1 ; j<=X ; j=j+1 )
{
cout << "*";
}
cout << endl;
}
return 0;
}
```
### 製造直角三角形
```c=
#include<iostream>
using namespace std;
int main()
{
int N;
int i, j;
cin >> N;
for( i=1 ; i<=N ; i=i+1 )
{
for( j=1 ; j<=i ; j=j+1 )
{
cout << "*";
}
cout << endl;
}
return 0;
}
```
### 製造金字塔
```c=
#include<iostream>
using namespace std;
int main()
{
int N;
int i, j;
cin >> N;
for( i=1 ; i<=N ; i=i+1 )
{
for( j=1 ; j<=N-i ; j=j+1 )
{
cout << " ";
}
for( j=1 ; j<=2*i-1 ; j=j+1 )
{
cout << "*";
}
cout << endl;
}
return 0;
}
```
#### 打出99乘法表
```c=
#include<iostream>
using namespace std;
int main()
{
int i, j;
for( i=1 ; i<=9 ; i=i+1 )
{
for( j=1 ; j<=9 ; j=j+1 )
{
cout << i << "*" << j << "=" << i*j << " ";
}
cout << endl;
}
return 0;
}
```
### b004牛牛吃草
```c=
#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
int main()
{
double D,L,s,l;
double PI=2*acos(0);
while(cin>>D>>L)
{
l=L/2;
s=sqrt((L/2)*(L/2)-(D/2)*(D/2));
printf("%.3lf\n", PI*l*s);
}
}
```
### c776屋瓦
```c=
#include <iostream>
using namespace std;
int main()
{
int n,m;
while(cin>>n>>m)
{
cout<<n*m*3+n*2+m*1<<endl;
}
return 0;
}
```
### d549矩形中的幾何
```c=
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double a,b,c;
while(cin>>a>>b>>c)
{
cout << fixed << setprecision(2) << sqrt(a*a + c*c - b*b) << endl;
}
return 0;
}
```
### d053巧克力切切
```c=
#include <iostream>
using namespace std;
int main()
{
int m,n;
while(cin>>m>>n)
{
cout<<m*n-1<<endl;
}
}
```
### d277 矩陣對角線
```c=
#include <iostream>
using namespace std;
int main()
{
int a;
while(cin>>a)
{
cout<<a-(a%2)<<endl;
}
}
```
### b681 1. 山洞探險
```c=
#include <iostream>
using namespace std;
int main()
{
int L;
while(cin>>L)
{
if(L>0)
{
cout<<L*2-1<<endl;
}
else
{
cout<<(-L)*2<<endl;
}
}
return 0;
}
```
### 127 牧場面積
```c=
#include <iostream>
using namespace std;
int main()
{
long long int L;
while(cin>>L)
{
if(L%4==0)
{
cout<<(L/4)*(L/4)<<endl;
}
else
{
cout<<(L/4)*(L/4+1)<<endl;
}
}
return 0;
}
```
### d096 00913 - Joana and the Odd Numbers
```c=
#include <iostream>
using namespace std;
int main()
{
long long int n,x;
while(cin>>n)
{
x=2*(((n+1)/2)*((n+1)/2))-1;
cout<<(x-4)+(x-2)+x<<endl;
}
return 0;
}
```
### d057 11494 - Queen
```c=
int x1, y1, x2, y2, a, b, w;
while(cin>>x1>>y1>>x2>>y2,x1>0 || y1>0 || x2>0 || y2>0)
{
if(xy[0] == xy[2] && xy[1] == xy[3]){ //x=a且y=b
printf("0\n");
}
else if(xy[0] == xy[2] || xy[1] == xy[3]){ //x=a或y=b
printf("1\n");
}
else if(xy[3]-xy[2]-xy[1]+xy[0] == 0){ //y-x-(b-a)=0
printf("1\n");
}
else if(xy[3]+xy[2]-xy[1]-xy[0] == 0){ //y+x-(a+b)=0
printf("1\n");
}
else{
printf("2\n");
}
return 0;
}
```
### a005回家作業
```c=
#include <iostream>
using namespace std;
int main()
{
int t,a,b,c,d;
cin>>t;
while(t--)
{
cin>>a>>b>>c>>d;
if(d-c==c-b)
{
cout << a <<" " << b<<" " << c<<" " << d <<" " << b - a + d<<endl;
}
else
{
cout << a<<" " << b<<" " << c<<" " << d<<" " << d * b<<endl;
}
}
return 0;
}
```
### d095 00579 - ClockHands
```c=
#include<bits/stdc++.h>
using namespace std;
int main()
{
double a,b;
while(scanf("%lf:%lf",&a,&b)==2)
{
if(a==0&&b==0) break;
a=(a*30)+b/12*6;
b*=6;
if(abs(a-b)>180) printf("%.3f\n",360-abs(a-b));
else printf("%.3f\n",abs(a-b));
}
}
```
### c461 apcs 邏輯運算子(Logic Operators)
```c=
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b,c;
while(scanf("%d %d %d",&a,&b,&c)!=EOF){
if(!!a==!!b&&!!a==0){
if(c==0)printf("AND\nOR\nXOR");
if(c==1)printf("IMPOSSIBLE\n");
}
else if(!!a==!!b&&!!a==1){
if(c==0)printf("XOR\n");
if(c==1)printf("AND\nOR\n");
}
else{
if(c==0)printf("AND\n");
if(c==1)printf("OR\nXOR\n");
}
}
}
```
### c299 1. 連號或不連號
```c=
#include <iostream>
using namespace std;
//大到小
int main(){
int fun, r;
while( cin >> fun ){
r = 1;
int l[fun];
//輸入
for( int t = 0 ; t < fun ; t ++ ){
cin >> l[t];
}
//排序
for( int t = 0 ; t < fun - 1 ; t ++ ){
for( int t2 = 0 ; t2 < fun - 1 ; t2 ++ ){
if( l[t2] < l [t2 + 1] ){
int c = l[t2];
l[t2] = l[t2 + 1];
l[t2 + 1] = c;
}
}
}
//判斷
for( int t = 0 ; t < fun - 1 ; t ++ ){
if( l[t] - 1 != l[t + 1] ){
r = 0;
}
}
//輸出最大和最小
cout << l[fun - 1] << " " << l[0];
//對嗎?
if( r == 1 ){
cout << " yes";
} else {
cout << " no";
}
}
}
```
### d186 11461 - Square Numbers
```c=
//你這裡就放
#include <bits/stdc++.h>
//我覺得你應該會比較輕鬆
typedef long long ll;
using namespace std;
int main(int argc, const char * argv[])
{
ios_base::sync_with_stdio(false);
cin.tie(0);
ll a,b;
while(cin>>a>>b)
{
if(a==0 && b==0) break;
ll sum = 0;
for(ll j = sqrt(a);j <= sqrt(b);j++)
{
if(pow(j, 2)>=a && pow(j, 2)<=b)
sum += 1;
}
cout<<sum<<'\n';
}
return 0;
}
```
### a248 新手訓練 ~ 陣列應用
```c=
#include <iostream>
using namespace std;
int main() {
int a, b, N;
while ( cin >> a >> b >> N ) {
cout << a / b << ".";
a %= b;
while ( N-- ) {
a *= 10;
cout << a / b;
a %= b;
}
cout << endl;
}
return 0;
}
```
### a038 數字翻轉
```c=
#include <iostream>
using namespace std ;
int main()
{
long long int in ;
while (cin >>in )
{
if (in==0 )
{
cout << 0 ;
}
while (in%10==0&&in)
{
in/=10;
}
while (in)
{
cout <<in%10 ;
in/=10 ;
}
cout <<endl ;
}
}
```
### a536 11689 - Soda Surpler
```c=
#include<bits/stdc++.h>
using namespace std;
int main(){
int t,e,f,c,sum,colas;
while(cin>>t){
for(int i=0;i<t;i++){
cin>>e>>f>>c;
sum=0;
colas = e+f;
while(colas/c){
sum += colas/c;
colas =colas/c+colas%c;
}
cout<<sum<<endl;
}
}
}
```
### d189 11150 - Cola
```c=
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,num;
while(cin>>n)
{
num=n;
num+=n/3;
n=n%3+n/3;
while(n>=3)
{
num+=n/3;
n=n%3+n/3;
//cout<<n<<' '<<num<<endl;
}
if(n>1) num+=1;
cout<<num<<endl;
}
}
```
### d122 Oh! My Zero!!
```c=
#include
int main()
{ long int n;
int a=0;
while(scanf("%d",&n)!=EOF){
while(n>4){
n-=n%5;
a+=n/5;
n/=5;
}
printf("%d\n",a);
a=0;
}
}
```
### c420 Bert的三角形 (3)
```c=
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n-i;j++)
{
cout<<"_";
}
for(int j=1;j<=i*2-1;j++)
{
cout<<"*";
}
for(int j=1;j<=n-i;j++)
{
cout<<"_";
}
cout<<endl;
}
return 0;
}
```
### c013 00488-Triangle Wave
```c=
#include<iostream>
using namespace std;
int main()
{
long long int a, b, c, d, e, f, g;
while (cin >> a) {
cout << "\n";
for (b=0;b<a;b++) {
cin >> c >> d;
for (g=0;g<d;g++) {
for (e=0;e<c+1;e++) {
for (f=0;f<e;f++) {
cout << e;
}
cout << "\n";
}
e -= 2;
for (;e>0;e--) {
for (f=0;f<e;f++) {
cout << e;
}
cout << "\n";
}
}
cout << "\n";
}
}
}
```