1. Write a short program that asks for your height in integer inches and then converts your height to feet and inches. Have the program use the underscore character to indicate where to type the response. Also use a const symbolic constant to repre¬sent the conversion factor.
时间: 2023-01-11 20:55:36 浏览: 153
输入身高:
```
const int CONVERSION_FACTOR = 12;
int inches, feet;
cout << "请输入身高(英寸): _";
cin >> inches;
feet = inches / CONVERSION_FACTOR;
inches = inches % CONVERSION_FACTOR;
cout << "你的身高是: " << feet << "英尺" << inches << "英寸" << endl;
```
这段代码提示用户输入身高, 然后使用常量 `CONVERSION_FACTOR` (12) 将身高从英寸转换为英尺和英寸. 使用整数除法来计算英尺, 使用整数取模来计算英寸. 最后, 输出转换后的身高.
相关问题
use c++ 2. Write a program that asks the user for three words and prints out the words in alphabetical order, then the words concatenated in alphabetical order (stored in a fourth string).
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
string word1, word2, word3;
string sorted_words, concat_words;
// ask user for three words
cout << "Enter three words: ";
cin >> word1 >> word2 >> word3;
// sort the words in alphabetical order
if (word1 <= word2 && word1 <= word3) {
sorted_words = word1;
if (word2 <= word3) {
sorted_words += " " + word2 + " " + word3;
} else {
sorted_words += " " + word3 + " " + word2;
}
} else if (word2 <= word1 && word2 <= word3) {
sorted_words = word2;
if (word1 <= word3) {
sorted_words += " " + word1 + " " + word3;
} else {
sorted_words += " " + word3 + " " + word1;
}
} else {
sorted_words = word3;
if (word1 <= word2) {
sorted_words += " " + word1 + " " + word2;
} else {
sorted_words += " " + word2 + " " + word1;
}
}
// concatenate the words in alphabetical order
if (word1 <= word2 && word1 <= word3) {
concat_words = word1;
if (word2 <= word3) {
concat_words += word2 + word3;
} else {
concat_words += word3 + word2;
}
} else if (word2 <= word1 && word2 <= word3) {
concat_words = word2;
if (word1 <= word3) {
concat_words += word1 + word3;
} else {
concat_words += word3 + word1;
}
} else {
concat_words = word3;
if (word1 <= word2) {
concat_words += word1 + word2;
} else {
concat_words += word2 + word1;
}
}
// output the sorted and concatenated words
cout << "Sorted words: " << sorted_words << endl;
cout << "Concatenated words: " << concat_words << endl;
return 0;
}
```
Sample output:
```
Enter three words: apple dog cat
Sorted words: apple cat dog
Concatenated words: applecatdog
```
Write a program that asks the user to enter ten unique numbers. The program must add each number to a list if they aren’t already contained in the list. When the list contains ten unique numbers, the program displays the contents of the list and quits. Write a program that returns the number of strings where the first and last character in a string are the same, and the length of the string is 2 or longer. For example, if this is the list you use, ['bgh', 'wer', 'yuy', '1661'] the output should be 2 because the last 2 elements are longer than 1 char, and start and end with the same char. Hint: If you have a string such as word = “abc”, you can use word[-1] to access the last element in the string.
Sure, here are two Python programs that answer your questions:
Program 1: Adding Unique Numbers
```
numbers = []
while len(numbers) < 10:
new_num = int(input("Enter a unique number: "))
if new_num not in numbers:
numbers.append(new_num)
print(numbers)
```
This program uses a while loop to repeatedly ask the user for a number until the list contains ten unique numbers. If the user enters a number that is not already in the list, it is added to the list. Once the list contains ten unique numbers, the program prints the contents of the list and exits.
Program 2: Counting Strings with Same First and Last Character
```
strings = ['bgh', 'wer', 'yuy', '1661']
count = 0
for s in strings:
if len(s) >= 2 and s[0] == s[-1]:
count += 1
print(count)
```
This program uses a for loop to iterate through each string in the list. If the string has a length of 2 or more and its first and last characters are the same, the count variable is incremented. After all the strings have been checked, the program prints the final count. In this case, the output is 2 because only two strings in the list meet the conditions.
阅读全文
相关推荐














