#include <iostream> using namespace std; int main() { int AMOUNT; while (cin >> AMOUNT) { if (AMOUNT >= 0 && AMOUNT <= 10) cout << AMOUNT * 6 << endl; else if (AMOUNT >= 11 && AMOUNT <= 20) cout << 60 + (AMOUNT - 10) * 2 << endl; else if (AMOUNT >= 21 && AMOUNT <= 40) cout << 80 + (AMOUNT - 20) << endl; else if (AMOUNT > 40) cout << 100 << endl; } }
代码如上;
不难,只是用多重if语句;
AC的,没错。
#include using namespace std; int main() { int AMOUNT; while (cin >> AMOUNT) { if (AMOUNT >= 0 && AMOUNT <= 10) cout << AMOUNT * 6 << endl; else if (AMOUNT >= 11 && AMOUNT <= 20) cout << 60 + (AMOUNT - 10) * 2 << endl; else if (AMOUNT >= 21 && AMOUNT <= 40) cout << 80 + (AMOUNT - 20) << endl; else if (AMOUNT > 40) cout << 100 << endl; } }
代码如上;
不难,只是用多重if语句;
AC的,没错。
可是你的變數好難判讀喔(苦笑
分享不用 &&
的寫法
#include <cstdio> int main() { unsigned n; while (scanf("%u", &n) != EOF) { if (n >= 40) puts("100"); else if (n >= 21) printf("%u\n", 80 + (n - 20)); else if (n >= 11) printf("%u\n", 60 + (n - 10) * 2); else printf("%u\n", n * 6); } }
這樣很好,但最後一次可以直接 else cout<<100<<endl;
(小分享霸了,還請多多指教~
#include using namespace std; int main() { int AMOUNT; while (cin >> AMOUNT) { if (AMOUNT >= 0 && AMOUNT <= 10) cout << AMOUNT * 6 << endl; else if (AMOUNT >= 11 && AMOUNT <= 20) cout << 60 + (AMOUNT - 10) * 2 << endl; else if (AMOUNT >= 21 && AMOUNT <= 40) cout << 80 + (AMOUNT - 20) << endl; else if (AMOUNT > 40) cout << 100 << endl; } }
代码如上;
不难,只是用多重if语句;
AC的,没错。
#include <bits/stdc++.h>
using namespace std;
int a;
int main(int argc, char** argv){
while(cin>>a)
cout<<(a<=10)*(a*6)+(a>=11&&a<=20)*((a-10)*2+60)+(a>=21&&a<40)*((a-20)+80)+(a>=40)*100<<endl;
}
無聊來嘗試cout單行解