日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人文社科 > 生活经验 >内容正文

生活经验

C++ OJ 中多行数据输入(大小写转换、通过移位运算实现2的n次方、多组输入,每行输入数量不一样)

發(fā)布時(shí)間:2023/11/28 生活经验 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++ OJ 中多行数据输入(大小写转换、通过移位运算实现2的n次方、多组输入,每行输入数量不一样) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. 多組輸入,輸出每行最大值

while(cin>>a>>b) 主要解決的是兩個(gè)為一組的多組數(shù)據(jù)輸入,當(dāng)一次只輸入一個(gè)數(shù)據(jù)時(shí)就用 while(cin>>a)

輸入描述:

多組輸入,每行輸入包括三個(gè)整數(shù)表示的分?jǐn)?shù)(0~100),用空格分隔。

輸出描述:

針對(duì)每行輸入,輸出為一行,即三個(gè)分?jǐn)?shù)中的最高分。

輸入:

94 98 99
100 88 60

輸出:

99
1加粗樣式00

#include <iostream>int main()
{int a;int b;int c;int max = 0;while(std::cin >> a >> b >> c){int temp = a > b ? a : b;max = temp > c ? temp : c;std::cout << max << std::endl;}return 0;
}

2. 大小寫轉(zhuǎn)換

描述

實(shí)現(xiàn)字母的大小寫轉(zhuǎn)換。多組輸入輸出。

輸入描述

多組輸入,每一行輸入大寫字母。

輸出描述

針對(duì)每組輸入輸出對(duì)應(yīng)的小寫字母。

輸入:

A
B

輸出:

a
b

#include <iostream>int main()
{char a;while (std::cin >> a) {if(a != '\n'){std::cout << char(a + 32) << std::endl;}}return 0;
}

3. 通過(guò)移位運(yùn)算實(shí)現(xiàn)2的n次方

描述

不使用累計(jì)乘法的基礎(chǔ)上,通過(guò)移位運(yùn)算(<<)實(shí)現(xiàn)2的n次方的計(jì)算。

輸入描述:

多組輸入,每一行輸入整數(shù)n(0 <= n < 31)。

輸出描述:

針對(duì)每組輸入輸出對(duì)應(yīng)的2的n次方的結(jié)果。

輸入:

2
10

輸出:

4
1024

#include <iostream>int main()
{int a;while (std::cin >> a) {int result = 1 << a;std::cout << result << std::endl;}return 0;
}

4. 輸出 N*M 格式

輸入描述:

一行,輸入兩個(gè)整數(shù)n和m,用空格分隔,第二行包含n*m個(gè)整數(shù)(范圍-231~231-1)。(1≤n≤10, 1≤m≤10)

輸出描述:

輸出規(guī)劃后n行m列的矩陣,每個(gè)數(shù)的后面有一個(gè)空格。

輸入:

2 3
1 2 3 4 5 6

輸出:

1 2 3
4 5 6

#include <iostream>int main()
{int n;int m;int a;int i = 0;std::cin >> n >> m;while(std::cin >> a){i++;if(i % m != 0){std::cout << a << " ";}if(i % m == 0){std::cout << a << " " << std::endl;}}return 0;
}

5. 多組輸入,每行輸入數(shù)量不一樣

描述

首先輸入要輸入的整數(shù)個(gè)數(shù)n,然后輸入n個(gè)整數(shù)。輸出為n個(gè)整數(shù)中負(fù)數(shù)的個(gè)數(shù),和所有正整數(shù)的平均值,結(jié)果保留一位小數(shù)。0即不是正整數(shù),也不是負(fù)數(shù),不計(jì)入計(jì)算

輸入描述:

本題有多組輸入用例。首先輸入一個(gè)正整數(shù)n,然后輸入n個(gè)整數(shù)。

輸出描述:

輸出負(fù)數(shù)的個(gè)數(shù),和所有正整數(shù)的平均值。

輸入:

5
1 2 3 4 5
10
1 2 3 4 5 6 7 8 9 0

輸出:

0 3.0
0 5.0

#include <iostream>
#include <iomanip>int main()
{int n;while(std::cin >> n){int num = 0;int positive = 0;int negative = 0;double sum = 0.0;for(int i=0; i<n; i++){std::cin >> num;if(num > 0){positive++;sum += num;}if(num < 0){negative++;}}std::cout << negative << " " << std::fixed << std::setprecision(1) << (sum * 1.0)/positive << std::endl;}return 0;
}

總結(jié)

以上是生活随笔為你收集整理的C++ OJ 中多行数据输入(大小写转换、通过移位运算实现2的n次方、多组输入,每行输入数量不一样)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。